Linux 环境下 安装 MySQL 查看临时密码出现 Access denied for user‘root‘@‘localhost‘ (using password: NO)

在Linux系统中安装MySQL时遇到访问拒绝问题。本文详细介绍了如何通过关闭MySQL服务、修改my.cnf文件、跳过权限表、重置root密码、恢复配置并重启服务来解决'Access denied for user 'root'@'localhost' (using password: NO)'的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

最后大功告成!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梅兮昂

你的支持是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值