创建目录
[root@node1 ~]# mkdir /usr/local/mysql [root@node1 ~]# cd /usr/local/mysql/
下载安装包
到MySQL官网查看需要下载的版本,并获取到下载地址
https://downloads.mysql.com/archives/community/
下载
[root@node1 mysql]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.24-1.el7.x86_64.rpm-bundle.tar
查看下载文件
[root@node1 mysql]# ls mysql-8.0.24-1.el7.x86_64.rpm-bundle.tar
解压安装包
[root@node1 mysql]# tar -xvf mysql-8.0.24-1.el7.x86_64.rpm-bundle.tar
查看解压后的文件
[root@node1 mysql]# ll total 1550012 -rw-r--r-- 1 root root 793600000 Mar 28 11:08 mysql-8.0.24-1.el7.x86_64.rpm-bundle.tar -rw-r--r-- 1 7155 31415 47664272 Mar 23 2021 mysql-community-client-8.0.24-1.el7.x86_64.rpm -rw-r--r-- 1 7155 31415 193620 Mar 23 2021 mysql-community-client-plugins-8.0.24-1.el7.x86_64.rpm -rw-r--r-- 1 7155 31415 628908 Mar 23 2021 mysql-community-common-8.0.24-1.el7.x86_64.rpm -rw-r--r-- 1 7155 31415 6783892 Mar 23 2021 mysql-community-devel-8.0.24-1.el7.x86_64.rpm -rw-r--r-- 1 7155 31415 23587688 Mar 23 2021 mysql-community-embedded-compat-8.0.24-1.el7.x86_64.rpm -rw-r--r-- 1 7155 31415 4226224 Mar 23 2021 mysql-community-libs-8.0.24-1.el7.x86_64.rpm -rw-r--r-- 1 7155 31415 1262864 Mar 23 2021 mysql-community-libs-compat-8.0.24-1.el7.x86_64.rpm -rw-r--r-- 1 7155 31415 448452756 Mar 23 2021 mysql-community-server-8.0.24-1.el7.x86_64.rpm -rw-r--r-- 1 7155 31415 260789456 Mar 23 2021 mysql-community-test-8.0.24-1.el7.x86_64.rpm
解压后的文件不属于root,修改所属者和所属组为root
[root@node1 mysql]# chown root:root /usr/local/mysql -R
再次查看解压文件,文件的所属者和所属组都变为了root
[root@node1 mysql]# ll total 1550012 -rw-r--r-- 1 root root 793600000 Mar 28 11:08 mysql-8.0.24-1.el7.x86_64.rpm-bundle.tar -rw-r--r-- 1 root root 47664272 Mar 23 2021 mysql-community-client-8.0.24-1.el7.x86_64.rpm -rw-r--r-- 1 root root 193620 Mar 23 2021 mysql-community-client-plugins-8.0.24-1.el7.x86_64.rpm -rw-r--r-- 1 root root 628908 Mar 23 2021 mysql-community-common-8.0.24-1.el7.x86_64.rpm -rw-r--r-- 1 root root 6783892 Mar 23 2021 mysql-community-devel-8.0.24-1.el7.x86_64.rpm -rw-r--r-- 1 root root 23587688 Mar 23 2021 mysql-community-embedded-compat-8.0.24-1.el7.x86_64.rpm -rw-r--r-- 1 root root 4226224 Mar 23 2021 mysql-community-libs-8.0.24-1.el7.x86_64.rpm -rw-r--r-- 1 root root 1262864 Mar 23 2021 mysql-community-libs-compat-8.0.24-1.el7.x86_64.rpm -rw-r--r-- 1 root root 448452756 Mar 23 2021 mysql-community-server-8.0.24-1.el7.x86_64.rpm -rw-r--r-- 1 root root 260789456 Mar 23 2021 mysql-community-test-8.0.24-1.el7.x86_64.rpm
卸载MySQL及安装必要的工具
卸载MySQL依赖(这一步不可少)
[root@node1 mysql]# yum remove mysql-libs
由于阿里云服务器安装的是Linux最小系统版,没有如下工具,所以需要安装。
[root@node1 mysql]# yum install libaio [root@node1 mysql]# yum -y install autoconf
卸载MySQL
[root@node1 mysql]# systemctl stop mysql mysqld 2>/dev/null [root@node1 mysql]# rpm -qa | grep -i 'mysql\|mariadb' | xargs -n1 rpm -e --nodeps 2>/dev/null [root@node1 mysql]# rm -rf /var/lib/mysql /var/log/mysqld.log /usr/lib64/mysql /etc/my.cnf /usr/my.cnf
安装MySQL
[root@node1 mysql]# yum install -y *.rpm >/dev/null 2>&1
启动MySQL
[root@node1 mysql]# systemctl start mysqld
更改密码级别(可选)
[root@node1 mysql]# sed -i '/\[mysqld\]/avalidate_password.length=4\nvalidate_password.policy=0' /etc/my.cnf
配置MySQL
[root@node1 mysql]# tpass=$(cat /var/log/mysqld.log | grep "temporary password" | awk '{print $NF}') [root@node1 mysql]# cat << EOF | mysql -uroot -p"${tpass}" --connect-expired-password >/dev/null 2>&1 set password='000000'; update mysql.user set host='%' where user='root'; alter user 'root'@'%' identified with mysql_native_password by '000000'; flush privileges; EOF
注意:000000
是密码,建议修改为更加安全的密码。
登录MySQL命令行
[root@node1 mysql]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.0.24 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. 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>
远程连接MySQL
登录云控制台,在安全组放开3306端口
注意:0.0.0.0/0
代表所有ip均能访问,不安全。可以只授权给特定ip,例如自己当前电脑的公网ip地址,一般情况下,当前电脑的公网ip会变化,变化后就需要重新查询当前电脑的公网ip,再重新设置授权对象。
使用Navicat远程连接阿里云的MySQL
看到连接成功
完成!enjoy it!