简体   繁体   English

连接到MySQL数据库远程

[英]Connect to MySQL Database Remote

I have a MySQL database on another computer and I want to be able to connect from other computers (even computers on different networks). 我在另一台计算机上有一个MySQL数据库,并且希望能够从其他计算机(甚至是位于不同网络上的计算机)进行连接。 I did the following 我做了以下

MySQL 的MySQL

CREATE USER 'username_here'@'%' IDENTIFIED BY 'password_here';

then 然后

GRANT ALL PRIVILEGES ON *.* TO 'username_here'@'%';
FLUSH PRIVILEGES;

I also commented out bind-address in the my.ini file (I have windows) 我也注释了my.ini文件中的bind-address (我有Windows)

Then in the Java application (that utilizes the MySQL database) I did 然后,在Java应用程序(利用MySQL数据库)中,我做了

jdbc:mysql://public_ip:3306/database_name

I used my public ip address in the jdbc url. 我在jdbc网址中使用了我的公共IP地址。

Finally, I opened port 3306 in firewall. 最后,我在防火墙中打开了端口3306。 But I am not able to connect to the database remotely. 但是我无法远程连接到数据库。 What other things do I need to do to connect? 我还需要做什么其他事情才能连接?

First of all: Do not public post your real ip address and port and also say that you opened the firewall. 首先:不要公开发布您的真实IP地址和端口,也不要说您打开了防火墙。 Perhaps hackers now find it too easy to hack, but you can not rely on that ;-). 也许现在黑客发现黑客很容易,但是您不能依靠它;-)。

To your problem: Perhaps you configured your mysql to listen at localhost 3306 and not to your public ip address. 对您的问题:也许您将mysql配置为侦听localhost 3306而不是您的公共IP地址。 Please enter netstat -ltn in your shell. 请在您的shell中输入netstat -ltn If there is a line 如果有一行

tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN

and none with the public host address then you only have access from inside the network. 并且没有公共主机地址,那么您只能从网络内部进行访问。 Then, you have to configure your host adress in your mysql ini file. 然后,您必须在mysql ini文件中配置主机地址。 (just replace 127.0.0.1 or localhost by your ip address. (只需将127.0.0.1或localhost替换为您的IP地址即可。

FYI : It is a very bad idea to have a publicly open port to your database server, specially MySQL default( 3306 ) port. 仅供参考:对于您的数据库服务器,有一个公开开放的端口,特别是MySQL default( 3306 )端口是一个非常糟糕的主意。 You are just inviting people to try to break into your database. 您只是在邀请人们尝试闯入您的数据库。

To access MySQL remotely, follow below steps : 要远程访问MySQL ,请执行以下步骤:

1) Comment out following lines in my.cnf file. 1)注释my.cnf文件中的以下几行。

#bind-address           = 127.0.0.1
#skip-networking

2) Restart mysql server. 2)重新启动mysql服务器。

net stop MySQL   
net start MySQL

If you got The service name is invalid then Open task manager => Click Service button => search MySql service => Restart 如果得到The service name is invalid则打开任务管理器=>单击服务按钮=>搜索MySql服务=>重新启动

Remotely connect with mysql : 与mysql远程连接:

mysql -h HOST -u USERNAME -pPASSWORD

You can also check the open port with nmap command : 您还可以使用nmap命令检查打开的端口:

nmap -PN Ip_address

Note : Mask your ip address in your question. 注意:在您的问题中屏蔽您的IP地址。

MySQL Java Connectivity : MySQL Java连接性:

Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
Connection connection = DriverManager.getConnection("jdbc:mysql://publicIpAddress/dbName?",
                        sqluser,password);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM