13

I am trying to install mysql in a serving having CentOS Linux release 7.2.1511. Take a look to the process installation:

# sudo yum install mysql-server

Output:

Dependencies Resolved

===========================================================================================================================================================================================================
 Package                                                 Arch                                    Version                                         Repository                                           Size
===========================================================================================================================================================================================================
Removing:
 mysql-community-client                                  x86_64                                  5.7.10-1.el7                                    @mysql57-community                                  109 M
 mysql-community-server                                  x86_64                                  5.7.10-1.el7                                    @mysql57-community                                  652 M

Transaction Summary
===========================================================================================================================================================================================================

I ran the mysql damon:

# sudo service mysqld start

Checking the service:

# ps -ef|grep mysql
mysql     1371     1  0 22:17 ?        00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Here comes the problem driving me crazy. I want to set root password for the very first time, so I did:

# sudo mysql_secure_installation
// when password is required, I just type "enter key"

But the output: Securing the MySQL server deployment.

Enter password for user root: Error: Access denied for user 'root'@'localhost' (using password: NO)

Googling the error, in 90% of cases, the solution is to call mysqld_safe --skip-grant-tables & command:

service mysqld stop
mysqld_safe --skip-grant-tables &
mysql --user=root mysql
update user set Password=PASSWORD('new-password') where user='root';
flush privileges;
exit;

But mysqld_safe prompts an "command not found" error. I also tested with sudo mysqld --skip-grant-tables &, but it does not do anything. I will appreciate if you guide me to the right direction in order to set root password. Thank you in advance.

2

4 Answers 4

23

If you just run mysql command under root user you will be granted access without asked for password, because socket authentication enabled for root@localhost.

This guide is misleading.

The only way to set password is to switch to native authentication like:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'test';
2
  • You are the savior! Work as charm!.
    – Zugor
    Jan 8, 2020 at 17:43
  • That's not true. # sudo mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
    – User
    May 31, 2022 at 22:02
12

In my insane research for a solution I took a look to /var/log/mysqld.log and found this line:

[Note] A temporary password is generated for root@localhost: abc123

Looks like mysql 5.7+ generates a random password in the installation and prompted in that file.

0
8

Use the below steps to reset the password:

$ sudo systemctl start mysqld

Reset the MySQL server root password.

$sudo grep 'temporary password' /var/log/mysqld.log

Output something like:

10.744785Z 1 [Note] A temporary password is generated for root@localhost: o!5y,oJGALQa

Use the above password during reset mysql_secure_installation process.

$ sudo mysql_secure_installation
Securing the MySQL server deployment.

Enter password for user root: 

You have successfully reset the root password of MySQL server. Use the below command to check the MySQL server connecting or not.

$ mysql -u root -p

See my article: Install Latest MySQL 5.7 on RHEL/Centos 7

4

All of the above did not work for me and note, I spent an hour or more trying all other suggestions from MYSql website to everything on SO, I finally got it working with:

Note: while it showed Enter password for user root, I didnt have the original password so I just entered the same password to be used as the new password.

Note: there was no /var/log/mysqld.log only /var/log/mysql/error.log

  1. kill the current mysqld pid
  2. run mysqld with sudo /usr/sbin/mysqld &
  3. run /usr/bin/mysql_secure_installation

    Output from mysql_secure_installation

    root@myServer:~# /usr/bin/mysql_secure_installation

    Securing the MySQL server deployment.

    Enter password for user root:

    VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin?

    Press y|Y for Yes, any other key for No: no Using existing password for root. Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

    New password:

    Re-enter new password: By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.

    Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Success.

    Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.

    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y Success.

    By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.

    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y

    • Dropping test database... Success.

    • Removing privileges on test database... Success.

    Reloading the privilege tables will ensure that all changes made so far will take effect immediately.

    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Success.

    All done!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.