mysql創建新用戶并設權限命令
當使用MySQL的時候,發現使用這樣的命令mysql -h 127.0.0.1 -u mysql -p 不能訪問數據庫
root#mysql -h?127.0.0.1 -u?mysql -p
Enter password:******
ERROR 1045: Access denied for user: 'mysql@127.0.01' (Using password: YES)
原因:
在127.0.0.1上的用戶mysql沒有連接localhost上MySQL的權限,可以通過如下的方式確認:
root#mysql -h localhost-u?mysql -p?
Enter password:????******
Welcome to the MySQL monitor.? Commands end with ; or \g.
Your MySQL connection id is 4 to server version: 4.0.20a-debug
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use mysql;???(此DB存放MySQL的各種配置信息)
Database changed
mysql> select host,user from user;?(查看用戶的權限情況)
+-------------+-------+
| host?????? ???? | user? ? |
+-------------+-------+
|?localhost??????|?????????? |
|?localhost??????| root?? |
| localhost??????|??? ?????? |
| localhost????? |?mysql?|
+-------------+-------+
6 rows in set (0.02 sec)
由此可以看出,只能以localhost的主機方式訪問。
解決方法:
mysql> Grant all privileges on *.* to 'root'@'%' identified by ******* with grant option;
Query OK, 0 rows affected (0.02 sec)?(%表示是所有的外部機器,如果指定某一臺機,就將%改為相應的機器名)
mysql> Grant all privileges on *.* to 'mysql'@'%' identified by ******* with grant option;
Query OK, 0 rows affected (0.02 sec)?(%表示是所有的外部機器,如果指定某一臺機,就將%改為相應的機器名)
mysql> flush privileges;??(運行為句才生效,或者重啟MySQL)
Query OK, 0 rows affected (0.03 sec)
mysql> select host,user from user;?(再次查看用戶的權限情況)
+-------------+-------+
| host?????? ???? | user? ? |
+-------------+-------+
|?%???????????????? |?mysql?|
|?%???????????????? |?root???|
|?localhost??????|?????????? |
|?localhost??????| root?? |
| localhost??????|??? ?????? |
| localhost????? |?mysql?|
+-------------+-------+
mysql>exit
現在再試試:
root#mysql -h?mysql -u root -p
Enter password:******
Welcome to the MySQL monitor.? Commands end with ; or \g.
Your MySQL connection id is 9 to server version: 4.0.20a-debug
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
就成功連接上了。
注意:
以上的設置不緊是對本機的用戶使用權限的更改,在所有外部的機器上都可以使用指定的用戶登陸連接。當使用mysql-front在windows下管理數據庫時,可能由于版本的問題有些程序不支持使用密碼,出現以下提示:
1251 - Client does not support authentication protocol requested by server; consider upgrading MySQL client
可以使用mymanager來管理mysql。