1、進入mysql,建立一個新使用者root,密碼為root:
格式:grant 許可權 on 資料庫名.表名 to 使用者@登入主機 identified by "使用者密碼";
grant select,update,insert,delete on *.* to root@192.168.1.12 identified by "root";
原先資料表結構
mysql> use mysql;
Database changed
mysql> select host,user,password from user;
+-----------+------+-------------------------------------------+
| host | user | password |
+-----------+------+-------------------------------------------+
| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-----------+------+-------------------------------------------+
執行上述語句後結果
mysql> use mysql;
Database changed
mysql> select host,user,password from user;
+--------------+------+-------------------------------------------+
| host | user | password |
+--------------+------+-------------------------------------------+
| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| 192.168.1.12 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+--------------+------+-------------------------------------------+
2 rows in set (0.00 sec)
可以看到在user表中已有剛才建立的root使用者。host欄位表示登入的主機,其值可以用IP,也可用主機名稱,
(1)有時想用本地IP登入,那麼可以將以上的Host值改為自己的Ip即可。
2、實現遠端連線(授權法)
將host欄位的值改為%就表示在任何用戶端機器上能以root使用者登入到mysql伺服器,建議在開發時設為%。
update user set host = ’%’ where user = ’root’;
將許可權改為ALL PRIVILEGES
mysql> use mysql;
Database changed
mysql> grant all privileges on *.* to root@'%' identified by "root";
Query OK, 0 rows affected (0.00 sec)
mysql> select host,user,password from user;
+--------------+------+-------------------------------------------+
| host | user | password |
+--------------+------+-------------------------------------------+
| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| 192.168.1.12 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| % | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+--------------+------+-------------------------------------------+
3 rows in set (0.00 sec)
這樣機器就可以以使用者名稱root密碼root遠端存取該機器上的MySql.
3、實現遠端連線(改表法)
use mysql;
update user set host = '%' where user = 'root';
這樣在遠端就可以通過root使用者訪問Mysql.