检查mysql服务是否启动
ps -ef|grep -i mysql
如果启动则停止mysql服务
service mysqld stop
修改mysql的配置文件my.cnf
my.cnf配置文件的位置,一般在/etc/my.cnf,有些版本在/etc/mysql/my.cnf
vim /etc/my.cnf # 可以用tab补全测试my.cnf是否在当前目录 vim /etc/my. + tab
# 如果vim命令不可用,使用vi命令
vi /etc/my.cnf
# 如果/etc目录下没有my.cnf文件,则
vim /etc/mysql/my.cnf
打开配置文件后按i进入编辑模式
在文件的[mysqld]标签下添加一句:skip-grant-tables,作用是登录mysql的时候跳过密码验证
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/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 the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_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
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
skip-grant-tables #新加上的
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
然后按ESC输入:wq保存退出
:wq
重启数据库服务
service mysqld start
登录MySQL
mysql -u root # 注意此处不需要加 -p
修改密码
选择mysql数据库
use mysql;
mysql8.0以下
运行修改密码语句
update user set authentication_string=password('新密码') where User='root';
注意:在mysql8.0以上下不能运行此命令,因为mysql8.0以上去除了user表中 password字段 和 password()方法
mysql8.0及以上
运行下面语句将密码置空(这一步最好置空,使用这一步设置的密码登录会报密码错误Access denied for user ‘root’@’localhost’ (using password: YES))
update user set authentication_string = '' where user = 'root';
退出
EXIT
# 或者
QUIT
修改my.cnf
vim /etc/my.cnf # 可以用tab补全测试my.cnf是否在当前目录 vim /etc/my. + tab
# 如果vim命令不可用,使用vi命令
vi /etc/my.cnf
# 如果/etc目录下没有my.cnf文件,则
vim /etc/mysql/my.cnf
打开配置文件后按 i 进入编辑模式
删掉步骤2的语句 skip-grant-tables
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/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 the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_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
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
然后按ESC输入:wq保存退出
:wq
重启服务器
service mysqld restart
登录MySQL
如图mysql -u root命令以不可使用,必须加上-p
mysql -u root -p # 提示输入密码时直接敲回车,刚刚已经将密码置空了
修改密码
ALTER USER root@localhost IDENTIFIED BY '新密码';
注意:mysql8.0后密码规则进行了更改
密码必须包含字母(大小写都行),数字,特殊字符,密码长度不能小于8