You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JDBC连接远程MySQL遇故障求解决,附代码及报错信息

Troubleshooting JDBC Connection Issues with Remote MySQL

Alright, let’s figure out why your JDBC connection to that remote MySQL server is failing—there are a few obvious (and easy-to-fix) issues in your code and setup. Let’s break this down step by step.

First, Fix the JDBC URL (Biggest Culprits Here!)

Your connection URL has three critical problems that are almost certainly causing failures:

  1. Wrong Port: Port 2083 is typically used for cPanel’s web interface, not MySQL. Most remote MySQL servers use the default port 3306 unless your hosting provider explicitly told you otherwise. Double-check with them to confirm the correct port.
  2. Unencoded Space in Database Name: You’ve got inventory management with a space in the database name. JDBC URLs can’t handle raw spaces—you need to URL-encode it as inventory%20management.
  3. Missing Timezone Parameter (For MySQL 8.0+): If your remote server is running MySQL 8.0 or newer, you’ll need to add &serverTimezone=UTC (or your local timezone) to avoid timezone-related connection errors.

Here’s the corrected URL:

"jdbc:mysql://www.merinasoftbd.com:3306/merinaso_inventory%20management?useSSL=false&serverTimezone=UTC"

Next, Verify Credentials & Permissions

  • Double-check your username (merinaso_inventorymanagement) and password (inventorymanagement) for typos—passwords are case-sensitive, so even a single wrong character will block access.
  • Critical: Most hosting providers lock down MySQL to prevent remote access by default. You need to log into your host’s control panel (like cPanel) and:
    • Whitelist your public IP address in the "Remote MySQL" section.
    • Confirm that your database user has permission to connect from your IP (or % for any IP, though that’s less secure).

Check Network & Firewall Blocks

If the URL and credentials look right, the issue is likely network-related:

  • Test if you can reach the server’s MySQL port using a tool like telnet or nc:
    telnet www.merinasoftbd.com 3306
    
    If this command fails (times out or says "connection refused"), your local firewall is blocking outgoing connections, or your host’s firewall is rejecting your IP.
  • Disable your local firewall temporarily to rule it out, then adjust settings if needed.

Update Your MySQL JDBC Driver

If you’re using an older version of the MySQL Connector/J driver with a MySQL 8.0+ server, you’ll get connection errors. Make sure you’re using Connector/J 8.0 or newer, and note that the driver class has changed from com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver. You should explicitly load this driver in your code (some environments require it).

Corrected Full Code

Here’s your code with all the fixes applied:

package simple;
import java.sql.*;

public class Dbconnect {
    public static Connection Dbconnector() {
        Connection con = null;
        try {
            // Load the updated MySQL driver for 8.0+
            Class.forName("com.mysql.cj.jdbc.Driver");
            // Fixed connection URL
            String dbUrl = "jdbc:mysql://www.merinasoftbd.com:3306/merinaso_inventory%20management?useSSL=false&serverTimezone=UTC";
            String username = "merinaso_inventorymanagement";
            String password = "inventorymanagement";
            
            con = DriverManager.getConnection(dbUrl, username, password);
            System.out.println("Successfully connected");
        } catch (ClassNotFoundException e) {
            System.err.println("Error: MySQL JDBC driver not found! Make sure it's included in your project.");
            e.printStackTrace();
        } catch (SQLException e) {
            System.err.println("Connection failed—here's why:");
            e.printStackTrace();
        }
        return con;
    }
}

Common Error Breakdown

  • If you see com.mysql.cj.jdbc.exceptions.CommunicationsException: This means your code can’t reach the server—check port, firewall, or host address.
  • If you see java.sql.SQLException: Access denied for user...: Credentials are wrong, or the user doesn’t have remote access privileges.

内容的提问来源于stack exchange,提问作者md Tusher

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:19:44