I recently migrated one of our MySQL servers (Simply by using rsync) and afterwards when restarting the MySQL server I was faced with:
admin@db:~$ sudo /etc/init.d/mysql restart
* Stopping MySQL database server mysqld <strong>[fail]</strong>
* Starting MySQL database server mysqld [ OK ]
/usr/bin/mysqladmin: connect to server at 'localhost' failed
error: '<strong>Access denied for user 'debian-sys-maint'@'localhost'</strong> (using password: YES)'
This MySQL user is created for Ubuntu to be able to start/stop the database and to preform other maintenance operations.
The issue is that with each update to MySQL, the user’s password in the database is overwritten. Ubuntu searches the file /etc/mysql/debian.cnf in order to find this user’s password, but obviously the password was out of sync after copying the databases from the old database server.
First, check the contents of the /etc/mysql/debian.cnf file:
admin@db:~$sudo cat /etc/mysql/debian.cnf
The contents of the file should look something like the:
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
<strong>password = PASSWORD</strong>
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
user = debian-sys-maint
<strong>password = PASSWORD</strong>
socket = /var/run/mysqld/mysqld.sock
basedir = /usr
The PASSWORD is what we’re looking for.
Next, you will want to issue a command to MySQL to tell it to grant the debian-sys-maint user all necessary privileges using the new password.
Login to your MySQL server using your root account and the root password:
admin@db:~$ mysql -u root -p
Issue the GRANT command now to grant those permissions:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY 'PASSWORD';
Restart MySQL, you should no longer be getting the “access denied” error message.
admin@db:~$ sudo /etc/init.d/mysql restart
* Stopping MySQL database server mysqld [ OK ]
* Starting MySQL database server mysqld [ OK ]
* Checking for corrupt, not cleanly closed and upgrade needing tables.
You may need to kill the MySQL server processes in order to get MySQL to shut down.