開啟mysql遠端登入
1、登陸mysql資料庫
mysql -u root -p
檢視user表
mysql> use mysql;
Database changed
mysql> select host,user,password from user;
+--------------+------+-------------------------------------------+
| host | user | password |
+--------------+------+-------------------------------------------+
| localhost | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
| 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
+--------------+------+-------------------------------------------+
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 [email protected]'%' identified by "password";
Query OK, 0 rows affected (0.00 sec)mysql> select host,user,password from user;
+--------------+------+-------------------------------------------+
| host | user | password |
+--------------+------+-------------------------------------------+
| localhost | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
| 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
| % | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
+--------------+------+-------------------------------------------+
3 rows in set (0.00 sec)
這樣機器就可以以使用者名稱root密碼root遠端訪問該機器上的MySql.
3、實現遠端連線(改表法)
use mysql;
update user set host = '%' where user = 'root';
這樣在遠端就可以通過root使用者訪問Mysql.