MySQL 5.7.26 二進制版本安裝(免安裝綠色版)
下載地址
https://downloads.mysql.com/archives/community/
https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz

PS:下載一些國外站點軟體,用迅雷還是比較管用
下載并上傳軟體至/opt/software
[root@mysql01 ~]# mkdir -p /opt/software
[root@mysql01 ~]# cd /opt/software/
[root@mysql01 software]# yum install -y lrzsz #檔案拖拽軟體
[root@mysql01 software]# rz -E
rz waiting to receive.
[root@mysql01 software]# ll
總用量 629756
-rw-r--r-- 1 root root 644869837 4月 18 23:48 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
解壓軟體
[root@mysql01 software]# tar -xvf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
[root@mysql01 software]# mkdir /application
[root@mysql01 software]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /application/mysql
[root@mysql01 software]# cd /application/mysql/
[root@mysql01 mysql]# ls
bin COPYING docs include lib man README share support-files
處理原始環境,洗掉系統自帶mariadb-libs,創建mysql用戶
[root@mysql01 ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.64-1.el7.x86_64
[root@mysql01 ~]# yum remove mariadb-libs.x86_64 -y
[root@mysql01 ~]# useradd -s /sbin/nologin mysql
[root@mysql01 ~]# id mysql
uid=1001(mysql) gid=1001(mysql) 組=1001(mysql)
設定環境變數
[root@mysql01 ~]# vim /etc/profile
export PATH=/application/mysql/bin:$PATH
[root@mysql01 ~]# source /etc/profile
查看MySQL版本
[root@mysql01 ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using EditLine wrapper
[root@mysql01 ~]# mysql --version
mysql Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using EditLine wrapper
創建資料路徑并授權
1.添加一塊新磁盤模擬資料盤
2.格式化并掛載
[root@mysql01 ~]# fdisk -l #查看磁盤、磁區資訊
[root@mysql01 ~]# mkfs.xfs /dev/sdb
[root@mysql01 ~]# blkid #查看磁盤UUID
/dev/sdb: UUID="5b995ceb-96be-4408-9125-51b931c5c543" TYPE="xfs"
[root@mysql01 ~]# vim /etc/fstab
UUID="5b995ceb-96be-4408-9125-51b931c5c543" /data xfs defaults 0 0
[root@mysql01 ~]# mount -a #是將/etc/fstab的所有內容重新加載
3.對MySQL軟體和資料目錄進行授權
[root@mysql01 ~]# chown -R mysql.mysql /application/*
[root@mysql01 ~]# chown -R mysql.mysql /data
4.初始化資料(創建系統資料)
# 5.6 版本 初始化命令 /application/mysql/scripts/mysql_install_db
# 5.7 版本
[root@mysql01 ~]# mkdir -p /data/mysql/data #創建初始化資料路徑
[root@mysql01 ~]# chown -R mysql.mysql /data
方法一
[root@mysql01 ~]# mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data
2020-04-19T14:27:50.401324Z 1 [Note] A temporary password is generated for root@localhost: dr7uTgZ/q!JI
5.7說明:
--initialize 引數:
1. 對于密碼復雜度進行定制:12位,4種
2. 密碼過期時間:180
3. 給root@localhost用戶設定臨時密碼
方法二
--initialize-insecure 引數:
無限制,無臨時密碼
[root@mysql01 ~]# rm -rf /data/mysql/data/* #先洗掉方法一初始化資訊
[root@mysql01 ~]# mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data
2020-04-19T15:24:41.446386Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
5.組態檔準備
cat >/etc/my.cnf <<EOF
[mysqld]
user=mysql
basedir=/application/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=6
port=3306
[mysql]
socket=/tmp/mysql.sock
EOF
6.啟動資料庫
方法一:
sys-v #centos6中使用
[root@mysql01 ~]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@mysql01 ~]# service mysqld restart
ERROR! MySQL server PID file could not be found!
Starting MySQL.Logging to '/data/mysql/data/mysql01.err'.
.. SUCCESS!
[root@mysql01 ~]# netstat -lnp | grep 3306 #通過埠查看是否啟動
tcp6 0 0 :::3306 :::* LISTEN 4982/mysqld
方法二:
systemd #centos7中使用
[root@mysql01 ~]# /etc/init.d/mysqld stop #先關閉方法一中啟動的MySQL
Shutting down MySQL.. SUCCESS!
[root@mysql01 ~]# cat >/etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
EOF
[root@mysql01 ~]# systemctl start mysqld.service
[root@mysql01 ~]# netstat -nlp | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 5134/mysqld
7.如何分析MySQL資料庫無法啟動情形
查看日志:
在哪?
/data/mysql/data/主機名.err
[ERROR] 背景關系
可能情況:
/etc/my.cnf 路徑不對等
/tmp/mysql.sock檔案修改過 或 洗掉過
資料目錄權限不是mysql
引數改錯了
8.修改資料庫密碼
[root@mysql01 ~]# mysqladmin -uroot -p password
Enter password: #輸入舊密碼,第一次使用密碼為空
New password: #輸入新密碼
Confirm new password: #再次確認新密碼
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
9.管理員用戶密碼忘記了
--skip-grant-tables #跳過授權表
--skip-networking #跳過遠程登錄
1)關閉資料庫
[root@mysql01 ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!
2)啟動資料庫到維護模式
[root@mysql01 ~]# mysqld_safe --skip-grant-tables --skip-networking &
3)登錄并修改密碼
[root@mysql01 ~]# mysql
mysql> select user,host from mysql.user; #查看用戶資訊
mysql> select user,host,authentication_string from mysql.user; #查看用戶和密碼欄位資訊
mysql> alter user root@'localhost' identified by '123456'; #關閉認證后無法使用這條命令
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges; #手動加載重繪授權表
mysql> alter user root@'localhost' identified by '123456'; #再次執行命令重置密碼成功
Query OK, 0 rows affected (0.00 sec)
mysql> exit #退出資料庫
Bye
4)停止資料庫,再正常啟動 登錄驗證修改密碼是否成功
[root@mysql01 ~]# /etc/init.d/mysqld stop
Shutting down MySQL..2020-04-20T15:55:40.277521Z mysqld_safe mysqld from pid file /data/mysql/data/mysql01.pid ended
SUCCESS!
[1]+ 完成 mysqld_safe --skip-grant-tables --skip-networking
[root@mysql01 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@mysql01 ~]# mysql -uroot -p
Enter password: #輸入修改后密碼驗證
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/40024.html
標籤:MySQL
