为什么我有,com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败

时间:2017-02-20 13:12:50

标签: mysql scala jdbc mariadb

我正在尝试使用SCALA连接到数据库服务器

scala> val connection = DriverManager.getConnection(
     | "jdbc:mysql://127.0.0.1:3306/test",
     | "milenko",
     | "nilton")

我得到了这个

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.
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
  at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038)
  at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338)
  at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2237)
  at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2270)
  at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2069)
  at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
  at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
  at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
  at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
  at java.sql.DriverManager.getConnection(DriverManager.java:664)
  at java.sql.DriverManager.getConnection(DriverManager.java:247)
  ... 49 elided
Caused by: java.net.ConnectException: Connection refused (Connection refused)

我只掌握SQL的基本知识,我试图从scala教程

运行这个例子
scala> val connection = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/test",
"root", // username when connecting
"" // password
)
java.sql.Connection = com.mysql.jdbc.JDBC4Connection@12e78a69

我试过这个,在端口上找到监听器

 netstat -tln

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:31416           0.0.0.0:*               LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN  

根据这个,我已经改变了我的连接,但仍然得到了相同。 我搜索数据库

milenko@milenko-desktop:~$ netstat -an | grep -i mysql
unix  2      [ ACC ]     STREAM     LISTENING     404688   /var/run/mysqld/mysqld.sock
milenko@milenko-desktop:~$ sudo su
[sudo] password for milenko: 
root@milenko-desktop:/home/milenko# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.0.29-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

如果我尝试更改协议

root@milenko-desktop:/home/milenko# mysql --protocol=TCP/IP
Unknown option to protocol: TCP/IP
Alternatives are: 'TCP','SOCKET','PIPE','MEMORY'
root@milenko-desktop:/home/milenko# mysql --protocol=TCP
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (111 "Connection refused")

我应该如何指定我的根,问题在哪里?

0 个答案:

没有答案