以下的文章主要介紹的是實現MySQL遠程連接的實際操作流程,本文主要是由其相關的實際應用代碼來引出MySQL遠程連接的具體操作步驟,以下就是文章的主要內容描述,希望會給你帶來一些幫助在此方面。 1、進入MySQL,創建一個新用戶root,密碼為root: 格式:grant 權限 on 數據庫名.表名 to 用戶@登錄主機 identifIEd by "用戶密碼"; 1. grant select,update,insert,delete on *.* to
[email protected] identifIEd by "root"; 原先數據表結構 1. MySQL> use MySQL; 2. Database changed 3. MySQL> select host,user,password from user; 4. +-----------+------+-------------------------------------------+ 5. | host | user | password | 6. +-----------+------+-------------------------------------------+ 7. | localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | 8. +-----------+------+-------------------------------------------+ 執行上述語句後結果 1. MySQL> use MySQL; 2. Database changed 3. MySQL> select host,user,password from user; 4. +--------------+------+-------------------------------------------+ 5. | host | user | passWord | 6. +--------------+------+-------------------------------------------+ 7. | localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | 8. | 192.168.1.12 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | 9. +--------------+------+-------------------------------------------+ 10. 2 rows in set (0.00 sec) 可以看到在user表中已有剛才創建的root用戶。host字段表示登錄的主機,其值可以用IP,也可用主機名, (1)有時想用本地IP登錄,那麼可以將以上的Host值改為自己的Ip即可。 2、實現MySQL遠程連接(授權法) 將host字段的值改為%就表示在任何客戶端機器上能以root用戶登錄到MySQL服務器,建議在開發時設為%。 1. update user set host = ’%’ where user = ’root’; 將權限改為ALL PRIVILEGES 1. MySQL> use MySQL; 2. Database changed 3. MySQL> grant all privileges on *.* to
root@'%' identifIEd by "root"; 4. Query OK, 0 rows affected (0.00 sec) 5. MySQL> select host,user,password from user; 6. +--------------+------+-------------------------------------------+ 7. | host | user | passWord | 8. +--------------+------+-------------------------------------------+ 9. | localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | 10. | 192.168.1.12 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | 11. | % | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | 12. +--------------+------+-------------------------------------------+ 3 rows in set (0.00 sec) 這樣機器就可以以用戶名root密碼root遠程訪問該機器上的MySQL. 3、實現MySQL遠程連接(改表法) 1. use MySQL; 2. update user set host = '%' where user = 'root'; 這樣在遠端就可以通過root用戶訪問MySQL.