天天看點

mysql遠端連接配接設定

遠端連接配接mysql資料庫: 

連接配接上以後,通過這台跳轉伺服器遠端連接配接mysql庫: 

用法:mysql -h ip/主機名  -u 使用者名  -p 密碼 

[root@ay131227102745952439z /etc]#mysql -h rdsa3qfmmqriq3y.mysql.rds.aliyuncs.com  -u weblog -p 

enter password: 

welcome to the mariadb monitor.  commands end with ; or \g. 

your mysql connection id is 197104 

server version: 5.5.18.1-alibaba-rds-201406-log source distribution 

copyright (c) 2000, 2014, oracle, skysql ab and others. 

type 'help;' or '\h' for help. type '\c' to clear the current input statement. 

如何設定允許别的機器遠端通路我的mysql資料庫: 

第一:更改 “mysql” 資料庫裡的 “user” 表裡的 “host” 項,從”localhost”改稱'%'。 

或者新加條記錄,“host” 項為要通路的ip位址,并授權。重新開機mysql服務。 

第二:在系統防火牆添加例外端口:3306,并允許例外。 

錯誤提示: 

error 1130: host '192.168.1.3' is not allowed to connect to this mysql server 

的解決方法: 

1。改表法。可能是你的帳号不允許從遠端登陸,隻能在localhost。這個時候隻要在localhost的那台電腦,登入mysql後, 

更改 "mysql" 資料庫裡的 "user" 表裡的 "host" 項,從"localhost"改稱"%" 

1.mysql -u root -pvmware 

mysql>use mysql; 

mysql>update user set host = '%' where user = 'root'; 

mysql>select host, user from user;   

mysql> use mysql 

reading table information for completion of table and column names 

you can turn off this feature to get a quicker startup with -a 

database changed 

mysql> select user,host from user; 

+------------------+-----------+ 

| user             | host      | 

| git              | %         | 

| root             | %         | 

| sonar            | %         | 

| root             | 127.0.0.1 | 

| debian-sys-maint | localhost | 

5 rows in set (0.00 sec) 

============================================================================== 

實際操作: 

mysql> select host,user from user; 

+-----------------------+------+ 

| host                  | user | 

| %                     | tina | 

| 127.0.0.1             | root | 

| ::1                   | root | 

| localhost             | root | 

| localhost.localdomain | root | 

mysql> update user set host='%' where user='root'; 

error 1062 (23000): duplicate entry '%-root' for key 'primary' 

=========================================================================== 

總結: 

mysql -u root -p 

mysql>select 'host' from user where user='root'; 

mysql>update user set host = '%' where user ='root'; 

mysql>flush privileges; 

mysql>select 'host'   from user where user='root'; 

第一句是以權限使用者root登入 

第二句:選擇mysql庫 

第三句:檢視mysql庫中的user表的host值(即可進行連接配接通路的主機/ip名稱) 

第四句:修改host值(以通配符%的内容增加主機/ip位址),當然也可以直接增加ip位址 

第五句:重新整理mysql的系統權限相關表 

第六句:再重新檢視user表時,有修改。。 

重起mysql服務即可完成。 

????????????????????????????????????????????????????????????????

mysql預設root使用者隻能本地通路,不能遠端連接配接管理mysql資料庫,linux如何開啟mysql遠端連接配接?設定步驟如下: 

1、grant指令建立遠端連接配接mysql授權使用者itlogger 

mysql>grant all privileges on *.* to itlogger@localhost identified by ‘www.itlogger.com’ with grant option; 

mysql>grant all privileges on *.* to itlogger@”%” identified by ‘www.itlogger.com’ with grant option; 

第一句增加itlogger使用者授權通過本地機(localhost)通路,密碼“www.itlogger.com”。第二句則是授與itlogger使用者從任何其它主機發起的通路(通配符%)。 

2、設定防火牆允許3306端口 

vi /etc/sysconfig/iptables 

添加-a rh-firewall-1-input -m state --state new -m tcp -p tcp --dport 3306 -j accept 

(注意添加在-a rh-firewall-1-input -j reject –reject-with icmp-host-prohibited之前,否則可能導緻規則不生效) 

重新開機防火牆service iptables restart 

3、附:mysql無法遠端連接配接的常見問題 

1)檢視mysql的端口是否正确,通過netstat -ntlp檢視端口占用情況,一般情況下端口是3306。 

2)報錯:error 2003 (hy000): can’t connect to mysql server on ’192.168.51.112′ (111) 

檢視/etc/my.cnf中,skip-networking 是否已被注掉,需要注掉。 

3)報錯:error 2003 (hy000): can’t connect to mysql server on ’192.168.51.112′ (113) 

檢視是否iptables沒有允許mysql連接配接,通過:service iptables stop臨時關閉測試是否可以正常遠端通路,如果可以,按上面方面設定iptable允許 

3306端口 

4)遠端通路mysql速度很慢的解決方法 

修改/etc/my.cnf或my.ini 

[mysqld]下添加 

skip-name-resolve 

skip-grant-tables 

********************************************************************************************************************** 

mysql資料庫遠端通路設定方法 

1、修改localhost 

更改 "mysql" 資料庫裡的 "user" 表裡的 "host" 項,從"localhost"改成"%" 

mysql>select host, user from user; 

2、指定授權 

使用myuser/mypassword從任何主機連接配接到mysql伺服器: 

grant all privileges on *.* to 'myuser'@'%' identified by 'mypassword' with grant option; 

使用myuser/mypassword從ip為192.168.225.166的主機連接配接到mysql伺服器: 

grant all privileges on *.* to 'myuser'@'192.168.225.166' identified by 'mypassword' with grant option; 

3、泛授權 

mysql -h localhost -u root 

mysql>grant all privileges on *.* to 'root'@'%' with grant option; //賦予任何主機上以root身份通路資料的權限 

解決mysql無法遠端連接配接的問題 

1、mysql的端口是否正确 

通過netstat -ntlp檢視端口占用情況,一般情況下端口是3306。在用工具連接配接mysql是要用到端口。例如my admin\my query browser\mysql front等。 

2、檢查使用者權限是否正确 

mysql庫的user表裡有兩條記錄:host分别為localhost和%(為了安全,%可以換成你需要外部連接配接的ip)。 

3、檢視/etc/mysql/my.cnf中,skip-networking 是否已被注掉 

需要注掉 

報錯:error 2003 (hy000): can't connect to mysql server on '192.168.51.112' (111) 

sudo gedit /etc/mysql/my.cnf 

#skip-external-locking 

#bind-address        = 127.0.0.1 

4、檢視iptables是否停掉,沒關的情況下,無法連接配接 

通過:service iptables stop臨時關閉。 

報錯:error 2003 (hy000): can't connect to mysql server on '192.168.51.112' (113) 

設定好以後需要重新開機:sudo service mysql restart 

===== 

清理自定義鍊:iptables -f 

開放3306端口:iptables -a input -p tcp -m tcp --dport 3306  -j accept 

重新開機防火牆:service iptables restart 

儲存服務:service iptables save 

預設的鍊隻有input output prerouting postrounting forword 

redhat把input和forward鍊引到一起統一處理 

添加後的效果: 

[root@localhost etc]# iptables -l -n 

chain input (policy accept) 

target     prot opt source               destination         

accept     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:3306 

這個是redhat的自帶鍊: 

chain rh-firewall-1-input