Linux 环境下 安装 MySQL 查看临时密码出现 Access denied for user’root’@‘localhost’ (using password: NO)
一、关闭当前 MySQL 服务
systemctl stop mysqld # 停止mysql服务
二、修改 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]
skip-grant-tables # 加到此处
#
# 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
三、重启 MySQL 服务
systemctl start mysqld # 启动mysql服务
四、登录,此时不需要输入密码直接进入
mysql -uroot -p
五、修改密码
# 这里我的 mysql 版本是 5.7.25 的,修改密码时列名为 authentication_string
# 修改密码为 root
update mysql.user SET authentication_string=password("root") where user='root';
# 如果为 5.1 的版本,修改密码时列名为 password
update mysql.user SET password=password("root") where user='root';
六、关闭当前 MySQL 服务,重复第一条
systemctl stop mysqld # 停止mysql服务
七、再次修改 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
八、重启 MySQL 服务
systemctl start mysqld # 启动mysql服务
九、登录,此时输入密码 root 进入
mysql -uroot -proot