1.修改MySQL的登錄設置:
# vi /etc/my.cnf
在[mysqld]的段中加上一句:skip-grant-tables
例如:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables
保存並且退出vi。
2.重新啟動mysqld
停止 MySQL 數據庫
/opt/lampp/lampp stopmysql
只啟動 MySQL 數據庫
/opt/lampp/lampp startmysql
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]
3.登錄並修改MySQL的root密碼
# /usr/bin/mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3 to server version: 3.23.56
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> USE mysql ;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> UPDATE user SET Password = password ('new-password') WHERE User = 'root' ;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 0
mysql> flush privileges ;
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye
注意password里面不要有空格
4.將MySQL的登錄設置修改回來
# vi /etc/my.cnf
將剛才在[mysqld]的段中加上的skip-grant-tables刪除
保存並且退出vi。
5.重新啟動mysqld
# /etc/init.d/mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]
----------------------------------------------------------------------------------------------------------------------------
同時需要支持下mysql遠程連接,否則會出現bug: Host 'XXX' is not allowed to connect to this MySQL server 解決方案/如何開啟MySQL的遠程帳號
mysql遠程連接 Host * is not allowed to connect to this MySQL server
解決此問題有以下2個方法:
localhost改成%
進入mysql的BIN目錄
代碼如下 | 復制代碼 |
mysql -u root -p mysql>use mysql; mysql>update user set host =’%' where user =’root’; mysql>flush privileges; |
具體分析
1、在本機登入mysql后,更改“mysql”數據庫里的“user”表里的“host”項,從”localhost”改為'%'。
代碼如下 | 復制代碼 |
mysql> mysql>use mysql; mysql>select 'host' from user where user='root'; |
#查看mysql庫中的user表的host值(即可進行連接訪問的主機/IP名稱)
代碼如下 | 復制代碼 |
mysql>update user set host = '%' where user ='root'; |
#修改host值(以通配符%的內容增加主機/IP地址,當然也可以直接增加某個特定IP地址,如果執行update語句時出現ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY' 錯誤,需要select host from user where user = 'root';
查看一下host是否已經有了%這個值,如果有了直接執行下面的flush privileges;即可)
代碼如下 | 復制代碼 |
mysql>flush privileges; |
另一種方法
如果你使用的是phpmyadmin我們可以登錄phpmyadmin然后在權限管理中進入,這里會看到所有的用戶了,你找到root用戶點擊修改,然后把域修改成%號即可了(注意,你必須是root權限用戶登錄哦,否則是不可以修改的)