# mysql -u root -h 192.168.194.142 -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'192.168.194.142' (using password: YES)
【解決辦法】
1. 先用localhost登錄
# mysql -u root -p
Enter password:
2. 執行授權命令
mysql> grant all privileges on *.* to root@'%' identified by '123';
Query OK, 0 rows affected (0.07 sec)
3. 退出再試
mysql> quit
Bye
再試登錄:
# mysql -u root -h 192.168.194.142 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
成功啦!
下面詳細說說如何給用戶授權。
mysql> grant 權限1,權限2, ... 權限n on 數據庫名稱.表名稱 to 用戶名@用戶地址 identified by '連接口令';
權限1,權限2,... 權限n 代表 select、insert、update、delete、create、drop、index、alter、grant、references、reload、shutdown、process、file 等14個權限。
當權限1,權限2,... 權限n 被 all privileges 或者 all 代替時,表示賦予用戶全部權限。
當 數據庫名稱.表名稱 被 *.* 代替時,表示賦予用戶操作服務器上所有數據庫所有表的權限。
用戶地址可以是localhost,也可以是IP地址、機器名和域名。也可以用 '%' 表示從任何地址連接。
'連接口令' 不能為空,否則創建失敗。
舉幾個例子:
mysql> grant select,insert,update,delete,create,drop on vtdc.employee to joe@10.163.225.87 identified by ‘123′;
給來自10.163.225.87的用戶joe分配可對數據庫vtdc的employee表進行select,insert,update,delete,create,drop等操作的權限,並設定口令為123。
mysql> grant all privileges on vtdc.* to joe@10.163.225.87 identified by ‘123′;
給來自10.163.225.87的用戶joe分配可對數據庫vtdc所有表進行所有操作的權限,並設定口令為123。
mysql> grant all privileges on *.* to joe@10.163.225.87 identified by ‘123′;
給來自10.163.225.87的用戶joe分配可對所有數據庫的所有表進行所有操作的權限,並設定口令為123。
mysql> grant all privileges on *.* to joe@localhost identified by ‘123′;
給本機用戶joe分配可對所有數據庫的所有表進行所有操作的權限,並設定口令為123。