1. 程式人生 > 實用技巧 >2.3.4 mysql 使用者密碼管理

2.3.4 mysql 使用者密碼管理

使用者密碼管理

修改使用者密碼

1)修改root密碼

A. mysqladmin
mysqladmin -u root -h localhost -p password 'rootroot'

B.修改 mysql.user表
use mysql;
update mysql.user set authentication_string=PASSWORD('root') where user='root';
flush privileges;

mysql root@localhost:(none)> set password=PASSWORD('root')

D.使用 alter user
alter user 'root'@'localhost' identified by 'root'
alter user 'root'@'%' identified by 'root'

2)修改普通使用者的碼

A.修改 mysql.user 表
use mysql;
update mysql.user set authentication_string=PASSWORD('itpux') where user='itpux' and host='localhost';
flush privileges;


B.使用grant語句
grant usage on *.* to 'itpux'@'%' identified by 'root';
grant usage on *.* to 'itpux'@'localhost' identified by 'root';
flush privileges;


C.當前使用者登入(比如:itpux)
set password=PASSWORD('itpux')
D.使用 alter user(推薦)
alter user 'itpux'@'localhost' identified by 'root';
alter user 'itpux'@'%' identified by 'root';

3)密碼過期問題

mysql 5.7.11之前有一個360天密碼過期的問題,5.7.12之後又改為密碼不過期。
show variables like 'default_password_lifetime';


mysql> show variables like 'default_password_lifetime';
+---------------------------+-------+
| Variable_name             | Value |
+---------------------------+-------+
| default_password_lifetime | 0     |
+---------------------------+-------+


A.永久:如果密碼要設定過期或者不過期
my.cnf 引數
[mysqld]
default_password_lifetime=0 或 360


B.水久:alter user
alter user 'itpux'@'localhost' password expire interval 90 day;
select * from mysql.user;
alter user 'itpux'@'localhost' password expire never;
alter user 'itpux'@'localhost' password expire DEFAULT;

4)使用者鎖定與解鎖的問題

alter user 'itpux'@'localhost' account lock;
alter user 'itpux'@'localhost' account unlock;

root使用者密碼丟失的解決辦法

win

加入引數到my.ini引數檔案中,然後重啟,登入時不用密碼,進入後再改密碼,改完後,去掉引數,重啟生效
skip-grant-tables

or:

mysqld --skip-grant-tables


use mysql;
update mysql.user set authentication_string=PASSWORD('root') where user='root';
flush privileges;

linux

1. service mysql stop
2.加入忽略授權表引數到my.cnf,改引數前備份原引數
[mysqld]
skip-grant-tables

3. service mysql start

4. mysql -uroot-p
    不用密碼
    
5.改密碼
update mysql.user set authentication_string=PASSWORD('root') where user='root';
flush privileges;

6.從my.cnf裡面取消;
skip-grant-tables

7. service mysql restart
8.登入測試

常用的登入方式5種

A mysql -uroot -p
B mysqL -p
C mysqL -S /mysql/data/3306/mysql.sock -uroot -p
D mysql -h ip -u root -p
E mysql -hlocalhost -uroot -proot
F mysql -uroot -p --defaults-file=/mysql/data/3306/my.cnf

免密碼登入的方式5種

A skip-grant-tables 引數方法 B 統一方法:直接修改my.cnf
[client]
user = "root"
password = "root"
登入方法:
mysql --defaults-file=/mysql/data/3306/my.cnf
C.不同客戶端方法:
[mysql]
user = "root"
password = "root"
[mysqladmin]
user = "root"
password = "root"
D.當前環境變數
vi vim ~/.my.cnf
[client]
user = "root"
password = "root"
E.使用環境變數 MYSQL_PWD
export MYSQL_PWD=root
登入方法:
mysql -uroot
F.最安全的方法,使用 login-path:
[root@elasticsearch ~]# mysql_config_editor  set --login-path=itpuxpw --user=root --password
Enter password:
[root@elasticsearch ~]# mysql_config_editor print --all
[itpuxpw]
user = root
password = *****
[root@elasticsearch ~]# mysql --login-path=itpuxpw
清除
[root@elasticsearch ~]# mysql_config_editor remove --login-path=itpuxpw