由於以前一直在服務器上直接操做mysql,因此沒有遇到過什麼問題,今天開發忽然要測試個功能,須要鏈接數據庫,發現一直鏈接不上!mysql
由於時間過得比較久mysql的root密碼已經忘記linux
1.linux下更改root密碼sql
vim /etc/my.cnf數據庫
在[mysqld]的段中加上一句:skip-grant-tables vim
skip-grant-tables服務器
wq保存ide
/etc/init.d/mysqld reatart #重啓mysql服務測試
mysql #登陸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 ; #選擇數據庫
Database changed
mysql> UPDATE user SET Password = password ( '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> exit spa
Bye #退出數據庫rest
2.以前只對locatehost/127.0.0.1受權,因此要賦予任何主機的訪問權限
mysql -uroot -ppassword #以root用戶登陸mysql
mysql>use databasename #選擇所要賦予權限的數據庫
mysql>GRANT ALL PRIVILEGES ON shandong.* TO 'demo'@'%'WITH GRANT OPTION #賦予任何主機的訪問權限
mysql> flush privileges ;
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye #退出數據庫
3.將MySQL的登陸設置修改回來 # vi /etc/my.cnf 將剛纔在[mysqld]的段中加上的skip-grant-tables刪除 從新啓動mysqld # /etc/init.d/mysqld restart Stopping MySQL: [ OK ] Starting MySQL: [ OK ]