Sep 29, 2008

reset mysql root password

Recently, one person left me in his system to solve one of his problem associated with webservice.
He left me and went somewhere and asked me to solve the problem before I left.

During this i requited the mysql root password, but he want there. So i googled to reset the mysql password. And here is what i did to reset the password.

$ sudo /etc/init.d/mysql stop

$ sudo mysqld_safe --skip-grant-tables --user=root

$ sudo mysql -u root


Run this query in mysql prompt:
mysql> UPDATE mysql.user SET Password=PASSWORD('newpassword') WHERE User='root';
mysql> FLUSH PRIVILEGES;


Finally kill this mysql process and restart it safely:

$ sudo /etc/init.d/mysql start


ref: http://www.linuxweblog.com/blogs/sandip/20060330/reset-mysql-root-password

Sep 28, 2008

solve "com.mysql.jdbc.CommunicationsException"

This is the error that I got when trying to connect to mysql (of lampp package, yes in linux) using Hibernate in java.

WARN 2008-09-28 13:55:17,116 org.hibernate.cfg.SettingsFactory -s Could not obtain connection metadata
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.ConnectException
MESSAGE: Connection refused

STACKTRACE:

java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.(Socket.java:366)
at java.net.Socket.(Socket.java:209)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:271)


Basically, the problem here is because of jdbc connector, which was unable to connect to the database. There are two ways of communication with mysql in linux.
  1. Don't listen on a TCP/IP port at all, if all processes that need to connect to mysqld run on the same host. There interaction with mysqld are made via Unix sockets or named pipes which is .sock file.

  2. B. Simply listen on a TCP/IP port. This allows remote communication to the database too.


Lampp initial configuration sets the first method of communication. And the sock file is created in < lampp root dir >/var/mysql/mysql.sock . But by default the jdbc search it in "/var/run/mysql/mysql.sock" (somewhere like this). Due to this problem, jdbc wont be able to connect to the database.

There are many ways to solve this:
The simple one is to switch to second type of communication method.
find the following line in <lampp root dir >/etc/my.cnf
skip-networking

and comment it.
# skip-networking


Hope this helps you out.