1. 程式人生 > >CentOS7.4安裝MySQL踩坑記錄

CentOS7.4安裝MySQL踩坑記錄

pre table tro end warning 更新 file auth lock

CentOS7.4安裝MySQL踩坑記錄

time: 2018.3.19


CentOS7.4安裝MySQL時網上的文檔雖然多但是不靠譜的也多, 可能因為版本與時間的問題, 所以記錄下自己踩坑的過程, 如果你發現進坑了, 歡迎參考本篇文章:)

  • 第一次嘗試遇到的問題:

    Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2)

嘗試卸載重新安裝, 參考, 步驟:

1.查看yum是否安裝過mysql
yum list installed mysql*
yum list installed | grep mysql*


沒有顯示結果, 說明yum中沒有安裝mysql(對yum與rpm並不是很了解, 如有錯誤歡迎指出)
2.刪除配置文件與文件夾
rm -rf /var/lib/mysql
rm /etc/my.cnf
3.查看rpm中的安裝並卸載
rpm -qa | grep -i mysql
以下根據上面命令顯示的列表修改

rpm -e mysql-community-server-5.7.21-1.el7.x86_64 mysql-community-common-5.7.21-1.el7.x86_64 mysql-community-libs-5.7.21-1.el7.x86_64 mysql57-community-release-el7-11.noarch mysql-community-client-5.7.21-1.el7.x86_64  mysql-community-libs-compat-5.7.21-1.el7.x86_64 --nodeps

3.清除余項
whereis mysql
刪除上面命令顯示的路徑
rm -rf /usr/share/mysql/
4.刪除配置
rm -rf /usr/my.cnf
rm -rf /root/.mysql*# 無結果

# 筆者機器上無結果
chkconfig --list | grep -i mysql
chkconfig --del mysqld
systemctl list-dependencies | grep -i mysql

5.重新安裝

下載mysql源並安裝到rpm:

wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
rpm -ivh mysql57-community-release-el7-11.noarch.rpm

更新yum並安裝mysql(時間較長):

# 更新yum軟件包
yum check-update
# 更新系統
yum update
# 安裝mysql
yum install mysql mysql-server

註意事項
更新yum後可能需要重新編輯/usr/bin/yum文件頭(因為筆者將默認的python更改為python3), 編輯後再次安裝即可, 出現的錯誤如下:

[root@centos ~]# yum install mysql mysql-server
  File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax

安裝完成後配置

未跳過grant-tables授權表時啟動MySQL會出現:
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)

這裏需要修改root的密碼, 這條命令是給mysql加上一個啟動參數--skip-grant-tables, 顧名思義,就是在啟動mysql時不啟動grant-tables授權表, 用於忘記管理員密碼後的修改
/usr/local/mysql/bin/mysqld_safe --skip-grant-tables --user=mysql &
但是, MySQL 5.7.6 版本開始默認是不安裝mysqld_safe, 以下為新方法:
1.停止 mysql 服務
service mysqld stop
2.設置 mysqld 選項 --skip-grant-tables參數:
systemctl set-environment MYSQLD_OPTS=‘--skip-grant-tables‘
3.重新啟動mysql
systemctl start mysqld
4.執行 mysql -u root 登錄mysql並更改密碼

[root@centos ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> UPDATE mysql.user
    -> SET authentication_string = PASSWORD(‘toor‘), password_expired = ‘N‘
    -> WHERE User = ‘root‘ AND Host = ‘localhost‘;
Query OK, 1 row affected, 1 warning (0.65 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

password_expired = ‘N‘過期狀態設置為No, flush privileges; 刷新權限記錄, 詳見

5.設置完密碼後去掉--skip-grant-tables參數, 重啟mysql即可用設置的密碼登錄root用戶
systemctl unset-environment MYSQLD_OPTS
systemctl restart mysqld
mysql -uroot -p

由於筆者代碼運行在服務器系統上, 故不此設置mysql遠程訪問

CentOS7.4安裝MySQL踩坑記錄