以下的文章主要介紹的是實現MySQL遠程連接的實際操作流程,本文主要是由其相關的實際應用代碼來引出MySQL遠程連接的具體操作步驟,以下就是文章的主要內容描述,希望會給你帶來一些幫助在此方面。
1、進入MySQL,創建一個新用戶root,密碼為root:
格式:grant 權限 on 數據庫名.表名 to 用戶@登錄主機 identified by "用戶密碼";
- grant select,update,insert,delete on *.* to [email protected] 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、實現MySQL遠程連接(授權法)
將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、實現MySQL遠程連接改表法)
- use MySQL;
- update user set host = '%' where user = 'root';
這樣在遠端就可以通過root用戶訪問MySQL.