如何使用 Java 连接到远程 MySQL 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2318250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:03:54  来源:igfitidea点击:

How to connect to a remote MySQL database with Java?

javamysqleclipsejdbc

提问by Angeline

I am trying to create a JSF application using the Eclipse IDE. I am using a remote mySQL server as my database. How do I connect to this remote database for creating tables and accessing them?

我正在尝试使用 Eclipse IDE 创建一个 JSF 应用程序。我使用远程 mySQL 服务器作为我的数据库。如何连接到此远程数据库以创建表并访问它们?

回答by Bozho

Just supply the IP / hostname of the remote machine in your database connection string, instead of localhost. For example:

只需在数据库连接字符串中提供远程机器的 IP/主机名,而不是localhost. 例如:

jdbc:mysql://192.168.15.25:3306/yourdatabase

Make sure there is no firewall blocking the access to port 3306

确保没有防火墙阻止访问端口 3306

Also, make sure the user you are connecting with is allowed to connect from this particular hostname. For development environments it is safe to do this by 'username'@'%'. Check the user creation manualand the GRANTmanual.

此外,请确保您正在连接的用户被允许从此特定主机名进行连接。对于开发环境,通过'username'@'%'. 检查用户创建手册GRANT手册

回答by giri

You need to pass IP/hostname of the rempote machine in the connection string.

您需要在连接字符串中传递远程计算机的 IP/主机名。

import java.sql.*;
import javax.sql.*;

public class Connect
{
   public static void main (String[] args)
   {
       Connection conn = null;

       try
       {

           String url = "jdbc:mysql://localhost:3306/mydb";
           Class.forName ("com.mysql.jdbc.Driver");
           conn = DriverManager.getConnection (url,"root"," ");
           System.out.println ("Database connection established");
       }
       catch (Exception e)
       {
           e.printStackTrace();

       }
       finally
       {
           if (conn != null)
           {
               try
               {
                   conn.close ();
                   System.out.println ("Database connection terminated");
               }
               catch (Exception e) { /* ignore close errors */ }
           }
       }
   }
}

回答by tmr

Plus, you should make sure the MySQL server's config (/etc/mysql/my.cnf, /etc/default/mysql on Debian) doesn't have "skip-networking" activated and is not binded exclusively to the loopback interface (127.0.0.1) but also to the interface/IP address you want connect to.

另外,您应该确保 MySQL 服务器的配置(Debian 上的 /etc/mysql/my.cnf、/etc/default/mysql)没有激活“skip-networking”并且没有专门绑定到环回接口(127.0 .0.1) 以及您要连接的接口/IP 地址。

回答by Puran Singh

to access database from remote machine , you need to give grant all privileges to you data base.

要从远程机器访问数据库,您需要授予您数据库的所有权限。

run the following script to give permissions: GRANT ALL PRIVILEGES ON .TO user@'%' IDENTIFIED BY 'password';

运行以下脚本以授予权限: GRANT ALL PRIVILEGES ON TO user@'%' IDENTIFIED BY 'password';

回答by Buminda

in my.cnf file , please change the following

在 my.cnf 文件中,请更改以下内容

## Instead of skip-networking the default is now to listen only on ## localhost which is more compatible and is not less secure. ## bind-address = 127.0.0.1

## 现在的默认设置不是跳过网络,而是仅在 ## localhost 上侦听,这更兼容且安全性更低。## 绑定地址 = 127.0.0.1

回答by JeewanaSL

  1. Create a new user in the schema ‘mysql' (mysql.user) Run this code in your mysql work space “GRANT ALL ON .to user@'%'IDENTIFIED BY '';

  2. Open the ‘3306' port at the machine which is having the Data Base. Control Panel -> Windows Firewall -> Advance Settings -> Inbound Rules -> New Rule -> Port -> Next -> TCP & set port as 3306 -> Next -> Next -> Next -> Fill Name and Description -> Finish ->

  3. Try to check by a telnet msg on cmd including DB server's IP

  1. 在模式 'mysql' (mysql.user) 中创建一个新用户 在你的 mysql 工作空间中运行此代码 “GRANT ALL ON .to user@'%'IDENTIFIED BY '';

  2. 在拥有数据库的机器上打开“3306”端口。 Control Panel -> Windows Firewall -> Advance Settings -> Inbound Rules -> New Rule -> Port -> Next -> TCP & set port as 3306 -> Next -> Next -> Next -> Fill Name and Description -> Finish ->

  3. 尝试通过 cmd 上的 telnet msg 进行检查,包括数据库服务器的 IP

回答by vishakha sharma

Close all the connection which is open & connected to the server listen port, whatever it is from application or client side tool (navicat) or on running server (apache or weblogic). First close all connection then restart all tools MySQL,apache etc.

关闭所有打开并连接到服务器侦听端口的连接,无论它来自应用程序或客户端工具(navicat)还是运行的服务器(apache 或 weblogic)。首先关闭所有连接,然后重新启动所有工具 MySQL、apache 等。

回答by Hyman Mason

On Ubuntu, after creating localhost and '%' versions of the user, and granting appropriate access to database.tables for both, I had to comment out the 'bind-address' in /etc/mysql/mysql.conf.d/mysql.cnf and restart mysql as sudo.

在 Ubuntu 上,在创建 localhost 和 '%' 版本的用户,并为两者授予对 database.tables 的适当访问权限后,我不得不注释掉 /etc/mysql/mysql.conf.d/mysql 中的 'bind-address' .cnf 并以 sudo 身份重新启动 mysql。

bind-address = 127.0.0.1

绑定地址 = 127.0.0.1