Cambiar la clave de root en un servidor MySQL o MariaDB

Cambiar la clave de root en un servidor MySQL o MariaDBIntroducción

En muchas ocasiones es necesario cambiar la contraseña root de la base de datos Mysql o MariaDB, eso es porque se le olvido, puede ser que en su primera instalación inicial no tenía conciencia de guardar la contraseña. No importa la razón, te enseñare los pasos para solucionar este asunto.

Desarrollo

Primero que nada, asegúrese que el servicio mariadb esté ejecutándose:
Entonces ejecutamos
# systemctl is-active mariadb
Si el resultado es active entonces debe ejecutar el siguiente comando
systemctl start mariadb
Si el resultado fue failed olvide el paso anterior.
systemctl is-active mariadb
[root@s198-12-148-97 /]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
Paso posterior se debe ejecutar el siguiente comando:
USE mysql;
UPDATE user SET password=PASSWORD('MiClaveAqui') WHERE User='root' AND Host = 'localhost';
El próximo paso consistirá en salir de la sesión actual al escribir


exit
Puede comprobar con la nueva contraseña.

en CENTOS 7 ESTA CONFIGURACIÓN SERIA LA MAS ADECUADA:

To begin, stop the database service and check the service status, we should see the environment variable we set previously:
------------- SystemD ------------- 
# systemctl stop mariadb

------------- SysVinit -------------
# /etc/init.d/mysqld stop
Next, start the service with --skip-grant-tables:
------------- SystemD ------------- 
# systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
# systemctl start mariadb
# systemctl status mariadb

------------- SysVinit -------------
# mysqld_safe --skip-grant-tables &
Start MySQL/MariaDB with Skip Tables
Start MySQL/MariaDB with Skip Tables
This will allow you to connect to the database server as root without a password (you may need to switch to a different terminal to do so):
# mysql -u root
From then on, follow the steps outlined below.
MariaDB [(none)]> USE mysql;
MariaDB [(none)]> UPDATE user SET password=PASSWORD('YourNewPasswordHere') WHERE User='root' AND Host = 'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;
Finally, stop the service, unset the environment variable and start the service once again:
------------- SystemD ------------- 
# systemctl stop mariadb
# systemctl unset-environment MYSQLD_OPTS
# systemctl start mariadb

------------- SysVinit -------------
# /etc/init.d/mysql stop
# /etc/init.d/mysql start 

This will cause the previous changes to take effect, allowing you to connect to the database server using the new password.Resumen

Para realizar estos pasos es necesario contar con el usuario root del sistema Linux. En este caso se hizo la prueba con centos7.


Comentarios

Entradas populares