下载解压
先下载在centos系统下MySQL8.0的安装包https://downloads.mysql.com/archives/community/
我下载的是 mysql-8.0.40-1.el7.x86_64.rpm-bundle.tar
我这里把MySQL8安装包上传到/var/www/data/mysql8目录下。
进入目录/var/www/data/mysql8,运行以下命令解压缩:
tar -xvf mysql-8.0.40-1.el7.x86_64.rpm-bundle.tar
如下所示:
# tar -xvf mysql-8.0.40-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-8.0.40-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.40-1.el7.x86_64.rpm
mysql-community-common-8.0.40-1.el7.x86_64.rpm
mysql-community-debuginfo-8.0.40-1.el7.x86_64.rpm
mysql-community-devel-8.0.40-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.40-1.el7.x86_64.rpm
mysql-community-icu-data-files-8.0.40-1.el7.x86_64.rpm
mysql-community-libs-8.0.40-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.40-1.el7.x86_64.rpm
mysql-community-server-8.0.40-1.el7.x86_64.rpm
mysql-community-server-debug-8.0.40-1.el7.x86_64.rpm
mysql-community-test-8.0.40-1.el7.x86_64.rpm
卸载自带的mariadb
查询已经安装的mariadb
rpm -qa | grep mariadb
展示出来所有名字,有多个,则重复卸载多次,务必卸载干净。如下是卸载mariadb-libs-5.5.68-1.el7.x86_64
rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
准备安装环境
依次执行如下命令:
yum update -y
yum install -y libaio
yum install -y net-tools
yum install openssl-devel.x86_64 openssl.x86_64 -y
yum -y install autoconf
yum install perl.x86_64 perl-devel.x86_64 -y
yum install perl-JSON.noarch -y
yum install perl-Test-Simple -y
以上命令是解决安装mysql8时的依赖环境问题。(如果以上命令运行失败可能会影响接下来的安装)
安装mysql8
在/var/www/data/mysql8下依次运行以下几个命令,安装MySQL8,注意如下命令运行顺序不能颠倒,必须严格按照以下顺序运行:
rpm -ivh mysql-community-common-8.0.40-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.40-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.40-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.40-1.el7.x86_64.rpm
rpm -ivh mysql-community-icu-data-files-8.0.40-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.40-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-8.0.40-1.el7.x86_64.rpm
rpm -ivh mysql-community-embedded-compat-8.0.40-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-8.0.40-1.el7.x86_64.rpm
修改配置文件:
编辑/etc/my.cnf文件,可以为MySQL设置如下参数(按实际需求选择即可)
[mysqld]
# 设置 MySQL 服务运行的端口号
port = 3306
# 指定 MySQL 数据库的默认字符集
character-set-server=utf8mb4
# 允许外部访问
bind-address=0.0.0.0
# 设置client连接mysql时的字符集,防止乱码
init_connect='SET NAMES utf8mb4'
# 设置 MySQL 允许访问的最大连接数
max_connections = 1000
# 指定MySQL服务器存储数据文件的目录
datadir=/var/www/data/mysql8/data
# 是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names = 1
# 事务隔离级别,默认为可重复读,mysql默认可重复读级别(此级别下可能参数很多间隙锁,影响性能)
transaction_isolation = READ-COMMITTED
# TIMESTAMP如果没有显示声明NOT NULL,允许NULL值
explicit_defaults_for_timestamp = true
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
目录授权
chown -R mysql:mysql /var/www/data/mysql8/data
启动mysql
systemctl start mysqld.service
查看MySQL8的状态
systemctl status mysqld.service
设置开机自启动
systemctl enable mysqld
启动mysql后会生成一个临时密码,使用此命令可以查询到临时密码
less /var/log/mysqld.log
找到下面这行:
A temporary password is generated for root@localhost: mM4Hl,U1F:XZ
默认密码就是 mM4Hl,U1F:XZ
命令行登录mysql
mysql -u root -p
输入临时密码,后进入,执行操作时提示:
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
则执行如下:
ALTER USER 'root'@'localhost' IDENTIFIED BY '你的密码' PASSWORD EXPIRE NEVER;