Page 1 of 1

<SOLVED>Java unable to connect to mysql

Posted: 27. Jul 2014, 19:22
by ciremay
Hi,
I have problem with mysql and java. My OS is SALIX-KDE-14.0.1

It seems that mysql is already installed when I install the OS. However it is not complete. So I have created default databases as follows:

Code: Select all

su
mysql_install_db -- user=mysql
and then I set the root user and password as follows:

Code: Select all

/usr/bin/mysqladmin -u root password "my-password"
And then I am able to start and use mysql. But then it seems that mysql server is automatically start every time I start the computer. And if I try to start the server I will got this error:

Code: Select all

root[xxx]# /usr/bin/mysqld_safe &
[1] 2814
root[xxx]# 140728 00:46:58 mysqld_safe Logging to '/var/lib/mysql/darkstar.err'.
140728 00:46:58 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
140728 00:48:39 mysqld_safe mysqld from pid file /var/lib/mysql/darkstar.pid ended
and if I continue to login to mysql, I will got this error:

Code: Select all

xxx[~]$ su
Password: 
root[xxx]# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysql/mysql.sock' (2)
so I have to restart the computer in order to be able to use mysql. That's OK it's not a big deal. All I have to do is to start the computer and let mysqld to auto-started and use it. I'm able to use mysql with C++ or PHP. The problem is that I can not connect to mysql through Java (connector/J). I try netstat to check the open ports:

Code: Select all

root[xxx]# netstat -plntu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1935/cupsd          
tcp6       0      0 ::1:631                 :::*                    LISTEN      1935/cupsd          
udp        0      0 0.0.0.0:631             0.0.0.0:*                           1935/cupsd          
udp        0      0 127.0.0.1:123           0.0.0.0:*                           1920/ntpd          
.......
.......
there is no mysqld on the list. But still I can use mysql with several programming languages but not Java.
I just read the wiki and then set up the .cnf file as follows:

Code: Select all

su
cp /etc/mysql/my-medium.cnf /etc/mysql/my.cnf
but still I can not connect to mysql with Java. why? why the mysqld port is not listed in netstat? is it closed? but why I am able to connect to it with PHP and C++? what should I do?

Re: Java unable to connect to mysql

Posted: 30. Jul 2014, 17:13
by ciremay
Finally I solve it 8-)
I think the problem is the way I start mysqld was wrong.
I change the way to start mysqld as follows:

Code: Select all

su
/usr/bin/mysqld_safe --user=root &
Now I can see mysqld port (3306) in netstat output. And I am able to connect to mysql with Java.
So actually my mysqld was not auto-started. That is why Java was unable to connect to mysql.