大网数据广告活动
服务器运维问答

Connecting to a remote mysql database using its private IP Address

· 52CE 我爱测

My database server has only a private IP address. I am able to ssh into the database server from my application server. I have created an user with the app server's IP address. Can anyone tell me how to access mysql databases fro…

问题原文

原帖链接 · TrickyExplorer · CC BY-SA 3.0

My database server has only a private IP address. I am able to ssh into the database server from my application server. I have created an user with the app server's IP address.

Can anyone tell me how to access mysql databases from the app server using the db server's private IP address on ubuntu 16.04?

已采纳解答

原帖链接 · Tom · CC BY-SA 3.0

It's likely that the mysql server is bound to the loopback address, either 127.0.0.1 or the ipv6 equivalent, and so you would have to open that up to the LAN network. You can check that with the following command on the mysql server;

# netstat -lntp | grep 3306
tcp        0      0 127.0.0.1:3306     0.0.0.0:*    LISTEN   20080/mysqld

That option for binding to an ip is configured in my.cnf, or where ever the mysql config file is on your system, and can be changed to bind to all ips;

[mysqld]
bind-address                   = 127.0.0.1

change to

[mysqld]
bind-address                   = 0.0.0.0

and restart the mysql server. Obviously it's important to ensure permissions have been set correctly on the mysql server.

Where the config file lives depends on which version of mysql you are using, I see it at; /etc/mysql/mysql.conf.d/mysqld.cnf on Ubuntu 16.04.3 running mysql-server-core-5.7 but it's at /etc/my.cnf on another CentOS box... so you might have to be more specific about that environment you are in...

You can test whether it's working by connecting to the private ip of the db server from the app server with the mysql-client

# echo "show databases;"  | mysql -uroot -psomepass -h localhost

Database
information_schema
wp_is_annoying_db
mysql
performance_schema
sys

if that's not working you should start with the basics

ping the db server private ip address;

$ ping 192.168.x.x
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.051 ms

use telnet or netcat to test the port

$ telnet localhost 3306
Trying 127.0.0.1...
Connected to localhost.

or use netcat, if telnet is not installed;

nc -v localhost 3306
Connection to localhost 3306 port [tcp/mysql] succeeded!

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-02-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。