Linux下mysql安装成功之后临时密码登陆失败解决办法

问题描述

//需要用到的命令
Shift+:w            		//保存
Shift+:q           	 		//退出
Shift+:wq           	 	//保存编辑并退出
Shift+:q!            		//强制退出不保存
mysql -u*** -p******  		//登录mysql(*代表用户名和密码)
`grep "temporary password" /var/log/mysqld.log

mysql安装成功之后使用上面的命令查看临时密码之后登录mysql失败

解决办法

  1. 停止mysql服务
service mysqld stop
  1. 编辑my.cnf配置文件
vi /etc/my.cnf
  1. 添加配置项skip-grant-tables
# 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
skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
  1. 重启mysql服务
systemctl start mysqld
  1. 登录mysql,不需要输入密码,提示输入密码直接回车
mysql -uroot -p
  1. 登录成功之后更改密码’引号里面的就是需要更改的密码这里以123456为例’
ALTER USER root@localhost IDENTIFIED BY 123456;
  1. 更改之后输入q退出mysql
  2. 记得去my.cnf删除skip-grant-tables配置项不然之后登录还是不需要密码的
# 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
经验分享 程序员 微信小程序 职场和发展