我有一台Linux机器(在10.0.0.10
上),我使用它部署了我的Play应用程序:
activator dist
我有一台运行MySQL的Windows机器(在10.0.0.51
上)。
该数据库设置了3个用户帐户root@localhost
、db_user@localhost
、db_user@%
,所有用户都拥有所有权限(仅用于测试)。
我可以使用mysql
shell从Linux机器访问数据库:
[neil@localhost ~]$ mysql -u db_user -p -h 10.0.0.51
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.12 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test_db |
| mysql |
| performance_schema |
| sakila |
| test |
| world |
+--------------------+
8 rows in set (0.04 sec)
mysql>
其中test_db
是我在Windows计算机上设置的测试数据库。
然而,当我尝试运行Play应用程序时,我会得到以下信息:
[neil@localhost ~]$ ~/TEST_APP-1.0-SNAPSHOT/bin/test_app
Play server process ID is 5908
[error] c.j.b.h.AbstractConnectionHook - Failed to obtain initial
connection Sleeping for 0ms and trying again. Attempts left: 0.
Exception: java.net.ConnectException: Connection
refused.Message:Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago.
The driver has not received any packets from the server.
Oops, cannot start the server.
Configuration error: Configuration error[Cannot connect to database
[db_user]]
at play.api.Configuration$.play$api$Configuration$$configError
(Configuration.scala:94)
...
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago.
The driver has not received any packets from the server.
...
这是我的应用程序中的数据库配置。conf:
db.db_user.driver=com.mysql.jdbc.Driver
db.db_user.url="jdbc:mysql://10.0.0.51:3306/test_db?allowMultiQueries=true"
db.db_user.user=db_user
db.db_user.pass="password"
db.db_user.partitionCount=3
db.db_user.maxConnectionsPerPartition=20
db.db_user.minConnectionsPerPartition=5
db.db_user.acquireIncrement=5
值得注意的是,当我在Windows上以开发模式运行应用程序时,此配置有效。但我不知道Linux盒子上出了什么问题,我已经没有什么东西可以尝试了。(注意,我在关闭所有防火墙的情况下尝试了这个操作,但遇到了同样的问题)。
更新
在我使用的播放应用程序中:
public static final String DB_USER = "db_user";
...
JdbcTemplate jt = new JdbcTemplate(DB.getDataSource(DB_USER));
所以我不应该在conf中使用db.default.etc...
?这不正确吗?
更新2
我不知道我是怎么错过的,但在堆栈跟踪中还有其他东西表明了问题(堆栈跟踪很长,所以我不想列出整件事,也许我应该列出):
Caused by: java.net.ConnectException: Connection refused
所以。。。服务器在端口3306上侦听,在运行netstat -an
的windows服务器上生成:
TCP 0.0.0.0:3306 0.0.0.0:0 LISTENING
TCP [::]:3306 [::]:0 LISTENING
防火墙已关闭(我知道…只是为了测试)。
MySQL正在接受来自CLI的连接。
不过我刚刚有了脑电波。。。。application.conf是在windows上生成的。。。我想知道我是否需要在配置文件上运行dos2unix
,这就是它找不到"db_user"
的原因。
更新3
编号:(
您需要在conf中使用db.default
。来自JavaDatabase
我认为指定密码的正确密钥不是pass
,而是password
:
db.db_user.password="password"
你也可以尝试使用db.default.*
和DB.getDataSource()
,但我想这不会有太大区别。。。