1. 程式人生 > >運維常用 MySQL 命令

運維常用 MySQL 命令

目錄

設定使用者密碼

方法一

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('NewPassword');

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');
Query OK, 0 rows affected (0.00 sec)

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

mysql> \q
Bye
[
[email protected]
~]# mysql -uroot -p123456 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.6.39 MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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>

方法二

UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';

mysql> UPDATE mysql.user SET Password = PASSWORD('234567') WHERE user = 'root';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

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

mysql> \q
Bye
# 這裡使用新密碼登陸
[
[email protected]
~]# mysql -uroot -p234567 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 16 Server version: 5.6.39 MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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>

丟失root密碼找回

兩種方式:

  1. 在啟動的時候增加 --skip-grant-tables 引數啟動
  2. my.cnf配置檔案中的mysql模組中新增下面引數 skip-grant-tables = 1

上面兩種方式效果相同

使用後,啟動mysql,就可以使直接使用mysql 命令登陸,無需密碼,進入後直接修改root密碼即可,最後去掉引數或者配置檔案中的引數重啟資料庫即可;

新增使用者