CentOS7 安裝MySQL5.7企業版(商業版)及修改my.cnf預設datadir、socket路徑後無法啟動的處理方法
一、清理環境
檢視是否已安裝
#rpm -qa |grep -i mysql
#rpm -qa |grep -i mariadb --(centos7預設系統自帶的檢視版本號並進行刪除)
#rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --刪除mariadb
# yum remove mariadb-libs-5.5.56-2.el7.x86_64 -- 刪除mariadb
# yum install perl perl-devel //安裝依賴包,根據各系統環境而定
二、開始安裝
#--上傳mysql rpm安裝包到伺服器(安裝包可以去oracle官網下載)
# rpm -ivh mysql-commercial-common-5.7.28-1.1.el7.x86_64.rpm
# rpm -ivh mysql-commercial-libs-5.7.28-1.1.el7.x86_64.rpm
# rpm -ivh mysql-commercial-libs-compat-5.7.28-1.1.el7.x86_64.rpm
# rpm -ivh mysql-commercial-client-5.7.28-1.1.el7.x86_64.rpm
# rpm -ivh mysql-commercial-server-5.7.28-1.1.el7.x86_64.rpm
-----or--下載安裝MySQL5.7社群版-----------------
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-common-5.7.28-1.el7.x86_64.rpm wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-libs-5.7.28-1.el7.x86_64.rpm wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-client-5.7.28-1.el7.x86_64.rpm wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-server-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
---------------------------------------------------------------
三、啟動&開機自啟
# systemctl enable mysqld
# systemctl start mysqld
# systemctl status mysqld
四、獲取隨機root密碼
# grep password /var/log/mysqld.log //日誌檔案查詢臨時密碼
# mysql -u root -p
Enter password:
五、修改密碼
修改當前登入使用者
ALTER USER USER() IDENTIFIED BY 'Abc123@#$';
六、使用者管理
建立新使用者
mysql> use mysql;
mysql> create user 'admin'@'%' identified by 'Abc123@#$';
Query OK, 0 rows affected (0.00 sec)
授權
grant select ,insert,update on db1.t1 to "admin"@'%' identified by "Abc123@#$";
grant all privileges on db1.* to "admin"@'%' identified by "Abc123@#$";
grant all privileges on *.* to "admin"@'%' identified by "Abc123@#$";
取消授權
revoke all on db1.t1 from 'admin'@"%";
revoke all on db1.* from 'admin'@"%";
revoke all privileges on *.* from 'admin'@'%';
3)設定使用者 root 可以在任意 IP 下被訪問
grant all privileges on *.* to root@"%" identified by "Abc123@#$";
七、my.cnf引數配置調整MySQL資料儲存目錄datadir、socket
# systemctl stop mysqld
# cp -rp /var/lib/mysql /data --複製資料檔案到新的目錄
# chown -R mysql:mysql /data/mysql
# vi /etc/my.cnf
-----my.cnf修改------
[client]
port=3306
#socket=/var/lib/mysql/mysql.sock
socket=/data/mysql/mysql.sock #修改socket目錄
[mysqld]
port=3306
#datadir=/var/lib/mysql
datadir=/data/mysql #修改資料檔案目錄
#socket=/var/lib/mysql/mysql.sock
socket=/data/mysql/mysql.sock #修改socket目錄
------end------
[root@Centos7 tools]# systemctl start mysqld
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
[root@Centos7 tools]# /usr/bin/mysql start
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/data/mysql/mysql.sock' (2)
調整目錄後,MySQL無法啟動了,目錄許可權處理過,還是啟動不了
方法一、
臨時關閉SELinux:
# setenforce 0
永久關閉SELinux:
# vi /etc/sysconfig/selinux
然後將配置SELinux=enforcing改為SELinux=disabled
# sestatus ##檢視SELinux狀態
# systemctl start mysqld --關閉SELinux後MySQL可以正常啟動
######datadir、socket路徑修改後因為SELinux安全問題導致無法啟動,在網上找了幾天也沒有人有說清楚,說到需關閉SELinux就已經不錯了。
其實真正的問題就是datadir、socket、log修改後的新路徑資料夾許可權只有MySQL使用者和使用者組,沒有其他的使用者,所以受SELinux影響無法寫入導致啟動失敗。
方法二、
# ls -ldZ /data/mysql ##檢視目錄的SElinux 標籤
# yum provides semanage ###安裝
# yum -y install policycoreutils-python.x86_64 ###安裝
# semanage fcontext -a -t mysqld_db_t "/data/mysql(/.*)?"
# restorecon -Rv /data/mysql
# ls -ldZ /data/mysql ##再檢視目錄的SElinux 標籤
# systemctl start mysqld ##處理後,MySQL啟動正常
八、防火牆埠開放
# firewall-cmd --zone=public --add-port=3306/tcp --permanent
# firewall-cmd --reload
九、通過客戶端遠端連線測試。OK完成