一、連接遠程數據庫:
1、顯示密碼
如:MySQL 連接遠程數據庫(192.168.5.116),端口“3306”,用戶名爲“root”,密碼“123456”
C:\>mysql -h 192.168.5.116 -P 3306 -u root -p123456
2、隱藏密碼
如:MySQL 連接本地數據庫,用戶名爲“root”,
C:\>mysql -h localhost -u root -p
Enter password:
Enter password:
二、配置mysql允許遠程鏈接
默認情況下,mysql帳號不允許從遠程登陸,只能在localhost登錄。本文提供了二種方法設置mysql可以通過遠程主機進行連接。
一、改表法
在localhost登入mysql後,更改 "mysql" 數據庫裏的 "user" 表裏的 "host" 項,將"localhost"改稱"%"
例如:
#mysql -u root -p
Enter password:
……
mysql>
mysql>update user set host = '%' where user = 'root';
mysql>select host, user from user;
二、授權法
例如: 你想myuser使用mypassword(密碼)從任何主機連接到mysql服務器的話。
mysql>GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%'IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
如果你想允許用戶myuser從ip爲192.168.1.6的主機連接到mysql服務器,並使用mypassword作爲密碼
mysql>GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3'IDENTIFIED BY
'mypassword' WITH GRANT OPTION;
mysql>FLUSH PRIVILEGES
使修改生效,就可以了
常見問題:
1、在採用法二授權法之後,無法在本地登錄mysql(如:#mysql -u root -p -h 192.168.5.116
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'loadb116' (using password: YES)
上例中loadb116是主機名.
解決方法:
1、這時可以使用:mysql -u root -p 登錄,進入到mysql後。
mysql> grant all privileges on *.* to 'root'@'loadb116'
identified by '123456' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
2、在本地使用ip地址登錄
# mysql -u root -p -h 192.168.5.116
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 60
Server version: 5.1.45 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
1、在採用法二授權法之後,無法在本地登錄mysql(如:#mysql -u root -p -h 192.168.5.116
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'loadb116' (using password: YES)
上例中loadb116是主機名.
解決方法:
1、這時可以使用:mysql -u root -p 登錄,進入到mysql後。
mysql> grant all privileges on *.* to 'root'@'loadb116'
identified by '123456' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
2、在本地使用ip地址登錄
# mysql -u root -p -h 192.168.5.116
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 60
Server version: 5.1.45 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>