Unix & Linux
centos mysql services
Updated Fri, 20 May 2022 04:05:39 GMT

setting mysql root password in CentOS 7


How can I log into the mysql 5.6 command line client and reset the root password in Centos7?

I read the following at this link, but it does not work:

1) sudo service mysqld stop
2) sudo service mysqld startsos
3) mysql -u root
4) Now you will be at mysql prompt. Here type:-
4.1) UPDATE mysql.user SET Password=PASSWORD('NewPassHere') WHERE User='root';
4.2) FLUSH PRIVILEGES;
4.3) quit;
5) sudo service mysqld restart  

Step 1) above results in:

[root@localhost ~]# sudo service mysqld stop
Redirecting to /bin/systemctl stop  mysqld.service
Failed to issue method call: Unit mysqld.service not loaded.

Step 3) above results in:

-bash: syntax error near unexpected token `('

When I change step 3 to UPDATE mysql.user SET Password='NewPassHere' WHERE User='root';, I get the following error:

bash: UPDATE: command not found...

I do seem to be able to get into mysql when i type su - to become root and then type mysql - u root at the next prompt. But then the above 5 step commands do not work, even when I remove the word sudo and/or replace the word service with systemctl. How can I get working access to the mysql 5.6 command line in CentOS 7, starting with setting the root password?




Solution

Sometimes you can clobber your configuration. As such, it's easier to start over, as if the package had never been installed. In your case, we are looking at MySQL.

  1. We use Yum to Remove MySQL, like so:
    yum remove mysql mysql-server
  2. With MySQL removed, we can safely backup the configuration:
    mv /var/lib/mysql /var/lib/mysql_old_backup
    If you'd rather remove it, issue:
    rm -vR /var/lib/mysql
  3. Now we can safely reinstall MySQL, using the default configuration that is included in the package from the Official MySQL repository (we need wget to fetch the rpm that will update your repos):
    yum install wget
  4. Now download and install the repository:
    wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm && rpm -ivh mysql-community-release-el7-5.noarch.rpm
  5. Verify the repositories are installed:
    ls -1 /etc/yum.repos.d/mysql-community*
  6. Issue the actual install command (This will replace the mysql-server in the CentOS repository with the official package from upstream MySQL):
    yum install mysql-server
  7. Use the script provided to set the root password, now that we have a fresh install again: mysql_secure_installation
    If you ever need to set the password after using the script, use:
    mysql -u root
  8. Now you can use the standard commands from systemctl, part of systemd to Start and Stop the daemon like so:
    systemctl start mysqld

References

  1. How to Remove MySQL Completely from Linux System - CentOS
  2. How to install MySQL Server 5.6 on CentOS 7 / RHEL 7