Cant access mysql root on new Centos server

Posted March 31, 2017 49.6k views

I just created a new Centos VPS and then followed the LAMP setup tutorial.

I now want to install Wordpress via SSH (I dont want to do the one click installation process)and I need to login to Mysql to create a new db but when I run this:

mysql -u root -p

and then hit enter to enter a blank password I get this:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

so then I try it again but I enter my root password and it returns this:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

also tried running this:

service mysqld start

which returns

Failed to start mysqld.service: Unit not found.

what do I need to do here?

edit: this is with MariaDB btw

and some more info:

sudo systemctl status mariadb.service

says its enabled and running:

 Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2017-03-29 13:52:43 +07; 2 days ago

and then

sudo rpm -q mariadb-server

shows this:


and this:

yum list installed | grep mariadb

returns this:

mariadb-server.x86_64 1:5.5.52-1.el7 @base

and finally when I try running this:


I get this

touch: cannot touch ‘.my.cnf.23570’: Permission denied
touch: cannot touch ‘.mysql.23570’: Permission denied
chmod: cannot access ‘.my.cnf.23570’: Permission denied
chmod: cannot access ‘.mysql.23570’: Permission denied


In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
/usr/bin/mysql_secure_installation: line 232: .my.cnf.23570: Permission denied
/usr/bin/mysql_secure_installation: line 233: .my.cnf.23570: Permission denied
/usr/bin/mysql_secure_installation: line 234: .my.cnf.23570: Permission denied
/usr/bin/mysql_secure_installation: line 236: .my.cnf.23570: Permission denied
/usr/bin/mysql_secure_installation: line 205: .mysql.23570: Permission denied
/usr/bin/mysql_secure_installation: line 207: .mysql.23570: Permission denied

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
5 answers

Just removed mariadb and reinstalled it and did I think I did something wrong at the setup phase after the mysqlsecureinstallation part, this time I set a pw for root and I can now login.

Run sudo /usr/bin/mysql_secure_installation since there’s a permission problem and I’m fairly sure you need sudo to change the installation files.

A temporary, random root password is generated and stored in the error log, which is /var/log/mysqld.log for an installation by the MySQL Yum repository. You can use this command to see the password:

shell > sudo grep ‘temporary password’ /var/log/mysqld.log


whenever you install the mysql or mariadb the default password is blank, so try running below if you are able to login in mariadb console.

sudo mysql -u root

You can change the password of maridadb using below command.

mysqladmin -u root password [newpassword]

Hope it will help you.

  • It didnt help Im afraid I get

    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)


    mysqladmin: connect to server at 'localhost' failed
    error: 'Access denied for user 'root'@'localhost' (using password: NO)'

I’ve got the same issue: right after the 'yum install mariadb-server' the script mysql_secure_installation won’t accept blank root password, saying:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

I’ve found that it’s because directory /var/lib/mysql is not empty and there were wrong privilegies on the /var/lib/mysql

mariadb honestly tells about it in journalctl -xe:

-- Unit mariadb.service has begun starting up.
Nov 06 15:14:15 openproject.exprem.dom mariadb-prepare-db-dir[15228]: Database MariaDB is not initialized, but the directory /var/lib/mysql is not empty, so initializat
Nov 06 15:14:15 openproject.exprem.dom mariadb-prepare-db-dir[15228]: Make sure the /var/lib/mysql is empty before running mariadb-prepare-db-dir.
Nov 06 15:14:15 openproject.exprem.dom systemd[1]: mariadb.service: control process exited, code=exited status=1
Nov 06 15:14:15 openproject.exprem.dom systemd[1]: Failed to start MariaDB database server.

the solution is:

# chown -R mysql:mysql /usr/local/var/mysql
# rm -rf /var/lib/mysql/*
# systemctl start mariadb.service

and after that mysql_secure_installation script ran correctly