6) 重置root用户密碼 從MySQL 5.7後,第一次啟動MySQL,會生成root用户的隨機密碼,可以執行以下命令查看 grep 'temporary password' /var/log/mysqld.log example: [root@60-250-98-236 yum.repos.d]# grep 'temporary password' /var/log/mysqld.log 2018-03-16T15:19:38.975505Z 1 [Note] A temporary password is generated for root@localhost: yPdTsk(jk1Zu 2018-03-19T07:27:21.313947Z 1 [Note] A temporary password is generated for root@localhost: Nn&j?Ty.y1_X 這個密碼是臨時密碼,必須修改為正式密碼,否則使用這個密碼登錄MySQL Server後,執行任何命令,都會顯示以下錯誤 登入MySQL Server: mysql -u root -p 以上若發生 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 原因是mysql这个文件的权限问题,需要把这个文件的权限改为当前用户 sudo chown -R root:root /var/lib/mysql 我們可以執行以下命令,修改root用户密碼 mysql> alter user user() identified by 'pass0'; 但是會顯示以下錯誤 ... 這是因為MySQL默認使用的validate_password_policy是MEDIUM的緣故,MySQL的官網介紹validate_password_policy有以下三種: Policy, Tests Performed 0 or LOW, Length //只檢查密碼長度 1 or MEDIUM, Length; numeric, lowercase/uppercase, and special characters //檢查密碼長度,是否包含數字,大小寫字母,特殊字符 2 or STRONG, Length; numeric, lowercase/uppercase, and special characters; dictionary file //檢查密碼長度,是否包含數字,大小寫字母,特殊字符,以及是否包含字典文檔 引用地址 https://dev.mysql.com/doc/refman/5.7/en/validate-password-options-variables.html#sysvar_validate_password_policy MySQL安裝後默認的validate_password_policy為MEDIUM,我們重新設置的密碼不符合這個policy的要求,所以命令執行失敗。 如果要修改密碼成功,我們需要修改validate_password_policy為0 mysql> set global validate_password_poilcy=0; 默認的validate_password_length是8,我們可以修改為5 mysql> set global validate_password_length=5; 完成這兩步後我們可以修改root用户的密碼為admin,命令和前面一樣。 mysql> alter user user() identified by 'pass0'; root/pass0 ---------- ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 解决: 1、找到密码[root@localhost opt]# cat /var/log/mysqld.log | grep password 上面的結果太多, 可改用下面這個 grep 'temporary password' /var/log/mysqld.log 2016-07-16T05:01:47.771362Z 1 [Note] A temporary password is generated for root@localhost: q.)aq!YaH6y- 2016-07-16T05:02:32.000199Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO) 2016-07-16T05:02:39.063852Z 3 [Note] Access denied for user 'root'@'localhost' (using password: NO) 2、重新连接mysql:[root@zzs171 opt]# /usr/bin/mysql -u root -p Enter password: 输入上面找到的密码,即可进入mysql客户端连接。 3、修改mysql的root的密码: 进入mysql客户端之后,show databases; 会提醒先修改密码: mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.设置密码,以下两种方式均可: SET PASSWORD FOR 'root'@'localhost' = PASSWORD('Mysqlpassw0rd.'); alter user 'root'@'localhost' identified by 'Mysqlpassw0rd.'; OK,至此设置成功。注意mysql有密码复杂度的限制。密码太简单会提示:mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123'); ERROR 1819 (HY000): Your password does not satisfy the current policy requirements ---------- 20190109 密碼重置: systemd is now used to look after mySQL instead of mysqld_safe (which is why you get the -bash: mysqld_safe: command not found error - it’s not installed) The user table structure has changed. 1. 先關閉 3306 防火牆. ----> 參考"firewall-cmd防火牆.txt" 2. Stop mysql: systemctl stop mysqld 3. Set the mySQL environment option systemctl set-environment MYSQLD_OPTS=”–skip-grant-tables” Start mysql usig the options you just set 4. systemctl start mysqld Login as root 5. mysql -u root ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) mysql -u root mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) Update the root user password with these mysql commands mysql> UPDATE mysql.user SET authentication_string = PASSWORD(‘MyNewPassword’) -> WHERE User = ‘root’ AND Host = ‘localhost’; mysql> FLUSH PRIVILEGES; mysql> quit Stop mysql systemctl stop mysqld Unset the mySQL envitroment option so it starts normally next time systemctl unset-environment MYSQLD_OPTS Start mysql normally: systemctl start mysqld Try to login using your new password: mysql -u root -p 重新開啟 3306 防火牆. ----> 參考"firewall-cmd防火牆.txt" Reference As it says at http://dev.mysql.com/doc/refman/5.7/en/mysqld-safe.html, Note As of MySQL 5.7.6, for MySQL installation using an RPM distribution, server startup and shutdown is managed by systemd on several Linux platforms. On these platforms, mysqld_safe is no longer installed because it is unnecessary. For more information, see Section 2.5.10, “Managing MySQL Server with systemd”.Which takes you to http://dev.mysql.com/doc/refman/5.7/en/server-management-using-systemd.html where it mentions the systemctl set-environment MYSQLD_OPTS= towards the bottom of the page The password reset commands are at the bottom of http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html ---------- 以下 mysqld_safe 已經變更為以上的 systemd 忘記密碼重設 先關閉 3306 防火牆. ----> 參考"firewall-cmd防火牆.txt" 1. 停止 MySql systemctl stop mariadb systemctl stop mysql 2. 在啟動 MySQL 或 MariaDB 時,若跳過 trant table 的話,就可以在不用密碼的情況下以資料庫 root 權限存取資料庫裡面的資料,這樣我們就可以重設密碼。 不過以這種模式啟動資料庫會有風險,所以建議關閉網路功能(networking),以防止外部的使用者連入。 sudo mysqld_safe --skip-grant-tables --skip-networking & 3. 使用 root 帳號連線至 MySQL/MariaDB 資料庫: # 以 root 連線至 MySQL/MariaDB 資料庫 mysql -u root 4. -- 載入 grant tables FLUSH PRIVILEGES; 5 變更 root 密碼: -- 重設 root 密碼 ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password'; 若是 MySQL 5.7.5 或 MariaDB 10.1.20 以前的版本,則要改用舊的指令: -- 重設 root 密碼(舊版指令) SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password'); 更改完成後,按下 Ctrl + d(或執行 exit 指令)離開。 6. 停止剛剛手動啟動的 MariaDB(或 MySQL)資料庫。 # 停止 MariaDB sudo kill `cat /var/run/mariadb/mariadb.pid` # 停止 MySQL sudo kill `cat /var/run/mysqld/mysqld.pid` 7. 啟動系統的 MariaDB(或 MySQL)服務。 # 啟動 MariaDB 服務 sudo systemctl start mariadb # 啟動 MySQL 服務 sudo systemctl start mysql 8. 使用 root 與新密碼進入 MySQL/MariaDB 資料庫。 # 使用新密碼進入資料庫 mysql -u root -p 重新開啟 3306 防火牆. ----> 參考"firewall-cmd防火牆.txt"