centos 安裝mysql8,配置root遠程登陸, mysql忘記密碼

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 <<EOF
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
在這裏插入圖片描述

#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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章