忘记MySQL密码,MySQL用户管理

本文详细介绍了在忘记MySQL密码时如何恢复,并且全面讲解了MySQL用户管理,包括登录退出、创建用户、删除用户及修改用户密码的操作步骤和方法。
摘要由CSDN通过智能技术生成

1、忘记MySQL密码-恢复密码

(1)停止服务

[root@localhost ~]# systemctl stop mysqld

(2)修改配置文件

#socket=/var/lib/mysql/mysql.sock
skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks

(3)重起服务

root@localhost ~]# systemctl start mysqld

(4)登入数据库

[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.14 Source distribution
Copyright (c) 2000, 2016, 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>

(5)切换当前数据库

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>

(6)修改密码

mysql> update user set authentication_string=password('123456') where
user='root' and Host = 'localhost';
Query OK, 0 rows affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 0 Warnings: 1
Mysql8.0以上版本
mysql> flush privileges;
mysql> alter user 'root'@'localhost'IDENTIFIED BY 'newpassword';

(7)退出数据库并停止服务

mysql> exit
Bye
[root@localhost /]# systemctl stop mysqld

(8)修改配置文件

#socket=/var/lib/mysql/mysql.sock
#skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks

(9)重起服务登入系统

[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# mysql -uroot -p'ABC123com'
mysql: [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 10
Server version: 5.7.14 Source distribution
Copyright (c) 2000, 2016, 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>

2、MySQL用户管理

2.1 登录和退出MySQL

示例:

mysql -h192.168.5.240 -P 3306 -u root -p123 mysql -e ‘select user,host from user’
 -h 指定主机名
 -P MySQL服务器端口
 -u 指定用户名
 -p 指定登录密码
此处mysql为指定登录的数据库
 -e 接SQL语句
2.2 创建用户

方法一:CREATE USER语句创建
CREATE USER user1@’localhost’ IDENTIFIED BY ‘123456’;

方法二: INSERT语句创建
INSERT INTO mysql.user(user,host, password,ssl_cipher,x509_issuer,x509_subject) VALUES(‘user2’,’localhost’,password('123456'),’’,’’,’’);
FLUSH PRIVILEGES;

方法三: GRANT语句创建
GRANT SELECT ON *.* TO user3@’localhost’ IDENTIFIED BY ‘123456’;
FLUSH PRIVILEGES;

2.3 删除用户

方法一:DROP USER语句删除
DROP USER user1@’localhost’;

方法二:DELETE语句删除
DELETE FROM mysql.user WHERE user=’user2’ AND host=’localhost’;
FLUSH PRIVILEGES;

2.4修改用户密码

root修改自己密码

方法一:
mysqladmin -uroot -p123 password 'new_password'//123为旧密码

方法二:
UPDATE mysql.user SET password=password(‘new_password’) WHERE user=’root’ AND host=’localhost’;
FLUSH PRIVILEGES;

方法三:
SET PASSWORD=password(‘new_password’);
FLUSH PRIVILEGES;

root修改其他用户密码

方法一:
SET PASSWORD FOR user3@’localhost’=password(‘new_password’);
FLUSH PRIVILEGES;

方法二:
UPDATE mysql.user SET password=password(‘new_password’) WHERE user=’user3’ AND host=’localhost’;
FLUSH PRIVILEGES;

方法三:
GRANT SELECT ON *.* TO user3@’localhost’ IDENTIFIED BY ‘localhost’;
FLUSH PRIVILEGES;

普通用户修改自己密码

方法一:
SET password=password(‘new_password’);

方法二:
mysqladmin -uzhuzhu -p123 password 'new_password' //123为旧密码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值