mysql 修改初始化密码_MySQL初始化以及更改密码

这篇博客介绍了如何在MySQL中进行初始化设置,以及两种安全更改root用户密码的方法,包括使用mysqladmin和直接在MySQL终端操作。同时提到了忘记密码时的解决方案,涉及修改grant tables和使用init.sql文件,但警告了在不同MySQL版本中需要注意的字段变化。
摘要由CSDN通过智能技术生成

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 &

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值