所以我在Docker容器中运行了一个Nginx,我在localhost上运行了一个mysql,我想从我的Nginx中连接到MySql.MySql在localhost上运行,而不是将端口暴露给外部世界,所以它绑定在localhost上,而不是绑定在机器的ip地址上.
有没有办法从这个docker容器中连接到这个MySql或localhost上的任何其他程序?
我意识到,由于UNIX套接字是特定于平台的,因此必须涉及一些非Java代码.具体来说,我们感兴趣的是使用JDBC连接到只启用了UNIX域套接字的MySQL实例.
它看起来不像是支持的,但是根据我的阅读,如果我们能够找到适合Java的UNIX套接字的适当实现,那么至少应该可以为基于UNIX套接字的JDBC编写SocketFactory .
有没人试过这个?有谁知道这样的实现?
我试图通过localhost从JDBC连接到MySQL .但连接失败了.在异常中,我看到JDBC正在尝试连接到127.0.0.1
String connectionString = "";
try {
loadProperties();
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connectionString = "jdbc:mysql://" + properties.getProperty("host") + "/" + properties.getProperty
("database") + "?user=" + properties.getProperty("user") + "&password=" + properties
.getProperty
("password");
connect = DriverManager
.getConnection(connectionString);
logger.debug("Connected to " + properties.getProperty("host"));
} catch (Exception e) {
logger.error("Database Connection failed with connection string - " + connectionString,e);
}
Run Code Online (Sandbox Code Playgroud)
从日志中:
Database Connection failed with connection string - jdbc:mysql://localhost/testdb?user=testuser&password=testpass
java.sql.SQLException: Access denied for user 'testuser'@'127.0.0.1' …Run Code Online (Sandbox Code Playgroud)