MySQL初始化以及更改密码


MySQL初始化,不修改配置文件(作为随笔笔记)

执行命令:

mysql_secure_installation

 

MySQL更改密码:

使用 mysqladmin 来更改 root 用户密码

[root@localhost ~ ]# mysqladmin -uroot(username) -p123456(old_password) password test(new_password)

Warning: Using a password on the command line interface can be insecure.

会提示不安全,可以采用如下命令:

[root@localhost ~ ]# mysqladmin -uroot -p password
Enter password:
New password:
Confirm new password:

这样比较安全。

 

进入MySQL终端更改密码:

mysql -uroot -p

use mysql;

update user set password=password("new_password") where user='username' and host='localhost';

flush privileges;

password()函数用于加密,不写的话代表使用明文密码。

简单介绍两种常用方法,不再介绍过多方法。

 

忘记MySQL root 密码:

skip-grant-tables的解法

首先,关闭实例

这里,只能通过kill mysqld进程的方式。

注意:不是mysqld_safe进程,也切忌使用kill -9。

ps -ef |grep mysqld

kill mysqld_ps

mysqld_safe --defaults-file=my.cnf --skip-grant-tables  --skip-networking &

mysql -S /var/lib/mysql/mysql.sock

mysql> update mysql.user set password=password('123456') where host='localhost' and user='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1

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

注意:
这里的update语句针对的是MySQL 5.6的操作,如果是在5.7版本,修改的应该是authentication_string字段,而不是password。

 

更优雅的解法(本人测试并未成功):

创建init.sql文件,关闭mysqld

vim init.sql
alter user 'root'@'localhost' identified by '123456';

重新启动服务

mysqld_safe --defaults-file=/etc/my.cnf --init-file=/root/init.sql &

借鉴了这位大佬的方法,请查看原文,挺详细的:

https://www.cnblogs.com/ivictor/p/9243259.html

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM