anúncios

quarta-feira, 14 de outubro de 2015

Resetando a senha de root do MySQL no Linux

O objetivo deste post, é resetar a senha do MySQL, caso tenha esquecido devido um longo período sem utilizar o MySQL.

Observação: Distro utilizada: Debian 8 (Jessie), mas pode ser utilizado em distros derivadas do Debian, como o Ubuntu/Mint e até outras distros não derivadas do Debian, adaptando apenas a inicialização do serviço do MySQL.

Segue os procedimentos:
Parar o serviço do MySQL
#/etc/init.d/mysql stop

Iniciar o serviço MySQL como segue:
#mysqld_safe --skip-grant-tables

Acessar o MySQL em outro terminal

$mysql -u root
Execute os comandos após o comando acima
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=PASSWORD("nova_senha_mysql") where User='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

Redefinindo as permissões
mysql> grant all privileges on *.* to 'root'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to 'root@localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for 'root'@'%';
+-------------------------------------------+
| Grants for root@% |
+-------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' |
+-------------------------------------------+
1 row in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>quit

Parar o serviço do MySQL
# /etc/init.d/mysql stop
[ ok ] Stopping MySQL database server: mysqld.


Iniciar o serviço do MySQL normal
# /etc/init.d/mysql start
[ ok ] Starting MySQL database server: mysqld.


Por fim, faça o teste logando com a nova senha definida para o root no MySQL.
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 49
Server version: 5.5.44-0+deb7u1 (Debian)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

Feito!

Nenhum comentário:

Postar um comentário