驗證環境:
[root@~~/]# rpm -qa | grep mysqlmysql-5.6.28-1.el6.x86_64[root@~~/]# lsb_release -aLSB Version: :core-4.1-amd64:core-4.1-noarchDistributor ID: CentOSDescription: CentOS Linux release 7.2.1511 (Core)Release: 7.2.1511Codename: Core[root@~~/]# uname -r3.10.0-327.22.2.el7.x86_64
首先確認服務器出于安全的狀態,也就是沒有人能夠任意地連接MySQL數據庫。
因為在重新設置mysql的root密碼的期間,MySQL數據庫完全出于沒有密碼保護的狀態下,其他的用戶也可以任意地登錄和修改MySQL的信息。可以采用將MySQL對外的端口封閉,并且停止Apache以及所有的用戶進程的方法實現服務器的準安全狀態。最安全的狀態是到服務器的Console上面操作,并且拔掉網線。
修改MySQL的登錄設置:
在[mysqld]的段中加上一句:skip-grant-tables
[root@~~/]# vi /etc/my.cnf
例如:
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock skip-grant-tables
保存并且退出vi。
重新啟動mysqld
[root@~~/]# /etc/init.d/mysqld restart Stopping MySQL: [ OK ] Starting MySQL: [ OK ]
登錄并修改MySQL的root密碼
[root@~~/]# mysqlWelcome to the MySQL monitor. Commands end with ; or /g.Your MySQL connection id is 1Server version: 5.6.28-log Source distributionCopyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.mysql> use mysql;mysql> update user set password=password("test") where user='root';mysql> flush privileges;mysql> exit; Bye
將MySQL的登錄設置修改回來
將剛才在[mysqld]的段中加上的skip-grant-tables刪除,保存并且退出vi;
[root@~~/]# vi /etc/my.cnf
保存并且退出vi。
再次重新啟動mysqld
[root@~~/]# /etc/init.d/mysqld restart Stopping MySQL: [ OK ] Starting MySQL: [ OK ]
使用新的密碼登錄,正常登錄
如果外網不能訪問數據庫,可以進行如下操作:
[root@~~/]# firewall-cmd --permanent --zone=public --add-port=3306/tcp success[root@~~/]# firewall-cmd --reloadsuccess
以上所述是小編給大家介紹的阿里云Linux CentOS 7.2下自建MySQL的root密碼忘記的解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!
新聞熱點
疑難解答