版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/weixin_42867972/article/details/82745259
mysql 密码修改
在服务器搭建中我们提到了使用 skip-grant-tables 来进行无密码登陆,这种方法在最初安装好 mysql 的时候,对 mysql 的检查,是否能够正常运行。这种方法可以运用在我们忘记密码的时候,进入数据库来修改密码。当我们对密码进行了修改,就应该把 skip-grant-tables 注释掉。
- 修改密码执行的命令
**1、**进入数据库,创建用户 root 初次修改密码(必须执行 ALTER 命令)
[root@centos-7 ~]# /usr/local/mysql57/bin/mysql -uroot
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
mysql> flush privileges; #刷新使得修改后的密码生效
mysql> exit
**2、**再一次修改密码(后续修改密码执行 update 命令)
mysql> update mysql.user set authentication_string=password("654321");
mysql> flush privileges; #刷新使得修改后的密码生效
mysql> exit
**3、**使用新密码登陆数据库
[root@centos-7 ~]# /usr/local/mysql57/bin/mysql -uroot -p654321
4、把密码写入配置文件中,去掉skip-grant-tables ,可实行免密码登陆
**注意:**此是 免密码登陆,是指已经正确设置好密码;和 skip-grant-tables 无密码登陆不一样,是基于无密码的状态
[root@centos-7 ~]# vim /etc/my.cnf
[client]
password=654321 #修改后的密码加入到 [client] 客户端中
......
[root@centos-7 ~]# /usr/local/mysql57/bin/mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
......
mysql>
[root@centos-7 rosen]# /usr/local/mysql57/support-files/mysql.server restart #重启数据库
Shutting down MySQL.... SUCCESS!
Starting MySQL.. SUCCESS!
**5、**修改主配置文件的权限,防止其他用户进行有密码或者免密码登陆数据库
[root@centos-7 ~]# chmod 600 /etc/my.cnf
当普通用户登陆的时候,不管是 有密码 还是 免密码 登陆都会进行拒绝登陆
**注意:**此用户是 linux 下的普通用户,并非数据库登陆的用户,数据库登陆的用户在创建密码的时候进行创建
bash-4.2$ /usr/local/mysql57/bin/mysql -uroot -p654321 #有密码登陆
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
bash-4.2$ /usr/local/mysql57/bin/mysql -uroot #免密码登陆
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
- mysql 配置文件
mysqld 主服务程序: 启动 | 停止 | 重启 服务
[root@centos-7 ~]# /usr/local/mysql57/support-files/mysql.server start(stop|restart)
mysqld 服务启动脚本: 进入数据库 -uroot(用户) -p654321(密码)
[root@centos-7 ~]# /usr/local/mysql57/bin/mysql -uroot -p654321
mysql 主配置文件:
[root@centos-7 ~]# vim /etc/my.cnf
[client] #客户端
port=3306 #端口3306
password=654321 #修改后的密码写到配置文件里可以免密码登陆
socket=/mysql/mysql.sock #mysql.sock 存放的位置(一般和其他mysql文件统一摆放)
[mysqld] #服务端
character-set-server=utf8
collation-server=utf8_general_ci
skip-name-resolve
#skip-grant-tables #开启后可以无密码登陆,修改密码后要关闭(通常是忘记密码的情况下)
user=mysql #用户mysql
port=3306
basedir=/usr/local/mysql57 #启动脚本、主程序等重要的文件存放所在的目录
datadir=/mysql
tmpdir=/tmp
socket=/mysql/mysql.sock
log-error=/mysql/mysqld.log #错误日志
pid-file=/mysql/mysqld.pid #运行的pid