注意:mysql默認的root帳戶只能在本地鏈接mysql
一、進入mysql,建立一個新用戶root,密碼爲root:sql
格式: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 |
+-----------+------+-------------------------------------------+ide
執行上述語句後結果spa
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)orm
能夠看到在user表中已有剛纔建立的root用戶。host字段表示登陸的主機,其值能夠用IP,也可用主機名,開發
(1)有時想用本地IP登陸,那麼能夠將以上的Host值改成本身的Ip便可。get
二、實現遠程鏈接(受權法)it
將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.
三、實現遠程鏈接(改表法)
use mysql;
update user set host = '%' where user = 'root';
這樣在遠端就能夠經過root用戶訪問Mysql.