centos 安装mysql8,配置root远程登陆, mysql忘记密码

文章目录

  • 1, yum安装mysql8
    • 忘记root密码
  • 2,配置root远程登录

1, yum安装mysql8

配置yum源: https://dev.mysql.com/doc/refman/8.0/en/linux-installation-yum-repo.html
重设root密码: https://dev.mysql.com/doc/refman/8.0/en/resetting-permissions.html

cat > /etc/yum.repos.d/mysql8.repo <<EOF
#MySQL 8.0
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/\$basearch/
enabled=1
gpgcheck=0
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
EOF

cat /etc/yum.repos.d/mysql8.repo
sudo yum -y install mysql-community-server
systemctl start  mysqld

#1,获取随机生成的root密码
#[root@c7 rpms]# cat /var/log/mysqld.log |grep "password"
#2020-01-10T03:20:26.007171Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: dhwx+G;po5wk
tmpass=$(cat b.txt |grep password  |awk -F 'root@localhost: ' '{print $2}')

#登录Mysql 
mysql -uroot -p$tmpass

忘记root密码

# 方法1:(先关闭mysql服务)
[root@c7 mysh]# sed -i '/mysql/s@/sbin/nologin@/bin/bash@' /etc/passwd
[root@c7 mysh]# su mysql -c "mysqld --skip-grant-tables --skip-networking=on &"
[root@c7 mysh]# ps -ef|grep mysqld
mysql    20738     1  2 10:23 ?        00:00:03 mysqld --skip-grant-tables --skip-networking=on
root     21131 16134  0 10:26 pts/0    00:00:00 grep --color=auto mysqld
[root@c7 mysh]# ss -nltp |grep 3306
[root@c7 mysh]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.18 MySQL Community Server - GPL
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 


##方法2:(先关闭mysql服务)
[root@c7 mysh]# sed -i '/mysql/s@/sbin/nologin@/bin/bash@' /etc/passwd
[root@c7 mysh]# cat >/tmp/mysqlini.txt <
ALTER USER 'root'@'localhost' IDENTIFIED BY 'XYZxyz123!';
EOF
[root@c7 mysh]# su mysql -c "mysqld --init-file=/tmp/mysqlini.txt & "
#再次重新设置root密码
[root@c7 mysh]# echo "alter user user() identified by '"'ABCxyz%123!'"';" |mysql -uroot -pXYZxyz123!
#杀死这个mysqld进程,正常启动mysqld
[root@c7 mysh]# pkill mysqld
[root@c7 mysh]# service mysqld start

2,配置root远程登录

mysql8.0.x默认的密码加密方式:https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
centos 安装mysql8,配置root远程登陆, mysql忘记密码_第1张图片

#1,允许root远程登陆: 8.0.13版本 Default Value (>= 8.0.4)0,
# 使用caching_sha2_password验证方式,很多客户端不支持,
# 需要改回默认的登录认证方式: mysql_native_password
mysql>  use mysql; 
mysql>  update user set host='%' where user='root';
mysql>  alter user 'root'@'%' identified with mysql_native_password BY 'ABCxyz%123!'; 

#2,刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

你可能感兴趣的:(centos 安装mysql8,配置root远程登陆, mysql忘记密码)