一、Msql安装完成后,进行密码初始化
背景: 一些版本的mysql 在刚刚安装完成后,会有临时密码机制,临时密码存放在mysql 的日志文件中,当你完成安装后,第一时间就要更改临时密码。
1.1 数据库安装完成后,如何查找临时密码?
冒号后面的就是临时密码,存放在mysql的日志中
[root@other2 /]# grep " temporary password " /var/log/mysqld.log
2021-12-31T07:57:21.554155Z 1 [Note] A temporary password is generated for root@localhost: HcLuq)atg3yO
1.2 如何修改密码
默认密码必须符合复杂性需求即大小写字母+数字+特殊符号并且密码有效长度是8位及8位以上
例如:
mysql> alter user root@localhost identified by 'MySQLroot123#';
1.3 如果想要修改为简单密码,需要执行一下语句,然后再修改密码
密码的长度是由validate_password_length决定的,但是可以通过以下命令修改
set global validate_password_length=4;
validate_password_policy决定密码的验证策略,默认等级为MEDIUM(中等),可通过以下命令修改为LOW(低)
set global validate_password_policy=0;
修改完成后密码就可以设置的很简单,比如1234之类的。
坑1
背景:由于某些原因你对安装日志做了清理,这时临时密码也会被清理掉,即使重新安装数据库也无法找回临时密码。需要绕过密码验证机制然后再重置密码
- 在 /etc/my.cnf 找到mysql 的配置文件,在[mysqld] 下添加 skip-grant-tables, 重启数据库使其生效,意为跳过跳过授权表,即跳过密码验证直接进入数据库。
- mysql -uroot -p //此时直接回车,既可以进入数据库
- 进入mysql 库并查看是否有user表
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
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> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.00 sec)
- 修改简单密码机制,如果不使用简单密码可以不用操作这步,使用复杂密码见<1.2小节>。
密码的长度是由validate_password_length决定的,但是可以通过以下命令修改
mysql> set global validate_password_length=4;validate_password_policy决定密码的验证策略,默认等级为MEDIUM(中等),可通过以下命令修改为LOW(低)
mysql> set global validate_password_policy=0;更改本地root密码(此时没有创建其他用户,只有本地root用户)
#这里设置的123456,password()是mysql密码加密的一个函数。mysql>update user set password=password(‘1234’) where user=”root”;
#有些数据要执行下面命令修改密码
mysql> update user set authentication_string=password('coship') where user="root";#刷新密码,使更改的生效。
mysql> flush privileges; - exit //退出数据库,重新登录验证密码是否生效;生效后注释 [mysqld] 下添加的skip-grant-tables,重启数据库,至此完成所有操作。
https://www.jianshu.com/p/a34de6eaa3bf
文档更新时间: 2023-07-28 06:48 作者:月影鹏鹏