Linux MySQL5.7.28由于时间太长没有登陆密码忘记了
通过mysql -v
命令知道我主机上的MySQL是5.7.28版本,不断尝试终于把这个密码修改了,话不多说,解决的步骤如下:
1.找到mysql5.7的配置文件my.cnf
MySQL默认安装位置为:/etc/my.cnf
,编辑my.cnf,最底新填加一行:skip-grant-tables
,其他内容注释掉
注:注释内容前,最好截个图,知道哪些是加注释的
vi /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#datadir=/var/lib/mysql #
#socket=/var/lib/mysql/mysql.sock #
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0 #
#log-error=/var/log/mysqld.log #
#pid-file=/var/run/mysqld/mysqld.pid #
skip-grant-tables
2.重启MySQL服务
service mysqld restart
3.重启完成后登陆MySQL
登录不需要验证密码,直接登入成功。然后进行修改密码
mysql -uroot
4.登陆MySQL后,修改密码
- 密码修改成 QWer@123
mysql> use mysql;
mysql> update user set authentication_string = password("QWer@123") where user="root" ;
- 刷新mysql库
mysql> flush privileges;
- 退出登陆:
mysql> quit;
5.打开mysql5.7的配置文件my.cnf,把刚增加这行:skip-grant-tables 删除掉 注释掉也恢复原样 保存并退出(:wq)
vi /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
6.重新启动mysql,即可用修改好的密码登录了
service mysqld restart