MySQL 8安装
一、安装包下载
-
下载地址:
最新版本下载地址:MySQL :: Download MySQL Community Server
历史版本下载地址:MySQL :: Download MySQL Community Server (Archived Versions) -
版本选择:
- 新的应用安装,应尽量选择最新的GA版本进行安装,以便能够使用新版本的新特性;老旧应用升级安装,应在测试环境充分测试,并使用最近生产环境数据进行充分验证。
Ubuntu
系统选择ubuntu linux
;CentOS
选择Red Hat Enterprise Linux / Oracle Linux
;编译安装选择Source Code
-
版本选择:
- 新的应用安装,应尽量选择最新的GA版本进行安装,以便能够使用新版本的新特性;老旧应用升级安装,应在测试环境充分测试,并使用最近生产环境数据进行充分验证。
Ubuntu
系统选择ubuntu linux
;CentOS
选择Red Hat Enterprise Linux / Oracle Linux
;编译安装选择Source Code
二、多种方式安装
RPM包安装
- 安装
mysql-community-common-8.0.28-1.el7.x86_64.rpm
,rpm -ivh mysql-community-common-8.0.28-1.el7.x86_64.rpm
- 安装
mysql-community-client-plugins-8.0.28-1.el7.x86_64.rpm
,rpm -ivh mysql-community-client-plugins-8.0.28-1.el7.x86_64.rpm
- 安装
mysql-community-libs-8.0.28-1.el7.x86_64.rpm
,rpm -ivh mysql-community-libs-8.0.28-1.el7.x86_64.rpm
- 安装
mysql-community-client-8.0.28-1.el7.x86_64.rpm
,rpm -ivh mysql-community-client-8.0.28-1.el7.x86_64.rpm
- 安装
mysql-community-icu-data-files-8.0.28-1.el7.x86_64.rpm
,mysql-community-icu-data-files-8.0.28-1.el7.x86_64.rpm
- 安装
mysql-community-server-8.0.28-1.el7.x86_64.rpm
,rpm -ivh mysql-community-server-8.0.28-1.el7.x86_64.rpm
- 一键安装
sudo yum install mysql-community-{server,client,common,libs,icu}-*
二进制文件安装
- 移除
mariadb
,rpm -e mariadb-libs-5.5.68-1.el7.x86_64
或yum remove mariadb-libs-5.5.68-1.el7.x86_64
- 解压
tar.gz
,tar -zxvf mysql-8.0.28-el7-x86_64.tar.gz
- 重命名文件
mv mysql-8.0.28-el7-x86_64 mysql-8.0.28
- 添加
mysql
用户和组groupadd mysql && useradd -M -g mysql mysql
- 更改mysql解压后目录所属用户
chown -R mysql:mysql mysql-8.0.28
- 初始化mysql,
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql-8.0.28/ --datadir= /var/lib/mysql/
- 设置全局变量,
ln -s /usr/local/mysql-8.0.28/bin/mysql /usr/local/bin/
, 或export PATH=$PATH:/usr/local/mysql-8.0.28/bin
vim /etc/profile
添加export PATH=$PATH:/opt/software/mysql-8.0.28/bin
- 设置为
service
服务,cp /usr/local/mysql-8.0.28/support-file/mysql.server /etc/init.d/mysqld
,修改basedir
和datadir
- 设置开机自启,
chkconfig mysqld on
。取消自启,chkconfig mysqld off
Deb包安装
-
下载
mysql-server_5.7.37-1ubuntu18.04_amd64.deb-bundle.tar
,并解压 -
安装依赖
apt install libaio1 libmecab2
-
按顺序安装
deb
包dpkg -i libmysqlclient20_5.7.37-1ubuntu18.04_amd64.deb dpkg -i libmysqlclient-dev_5.7.37-1ubuntu18.04_amd64.deb dpkg -i libmysqld-dev_5.7.37-1ubuntu18.04_amd64.deb dpkg -i mysql-common_5.7.37-1ubuntu18.04_amd64.deb dpkg -i mysql-community-source_5.7.37-1ubuntu18.04_amd64.deb dpkg -i mysql-community-client_5.7.37-1ubuntu18.04_amd64.deb dpkg -i mysql-client_5.7.37-1ubuntu18.04_amd64.deb
YUM源安装
-
执行
sudo yum repolist enabled| grep mysql
查看本地yum源配置,如当前启用的是MySQL5.7
,禁用即可# 查看当前YUM源,可以看到当前有5.7和8.0两个版本 sudo yum repolist enabled | grep mysql # mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 540 # mysql80-community/x86_64 MySQL 8.0 Community Server enabled: 303 # 禁用5.7YUM源,修改/etc/yum.repos.d/mysql-community.repo,将5.7版本的源enabled值改为0 [mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ # enabled=1 enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
-
如果没有
8.0
版本的源,在/etc/yum.repos.d/mysql-community.repo
添加如下配置即可:[mysql80-community] name=MySQL 8.0 Community Server baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
-
执行
sudo yum install mysql-community-server
,安装MySQL。其中mysql-community-client mysql-community-common mysql-community-libs
会作为依赖一起安装。
三、验证安装
- 查看
mysql
版本,mysql --verison
四、启动MySQL服务
- 不同的安装方式,有不同的启动流程:
rpm
、yum
、Deb
安装,执行sudo service mysqld start
- 二进制文件安装:
- 先进行初始化
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql-8.0.28/ --datadir=/var/lib/mysql/
- 添加
service
服务脚本,cp /usr/local/mysql-8.0.28/support-file/mysql.server /etc/init.d/mysqld
,修改basedir
和datadir
- 启动
mysqld
,sudo service mysqld start
- 先进行初始化
- 查看服务状态,
sudo service mysqld status
- 停止服务,
sudo service mysqld stop
- 重启服务,
sudo service mysqld restart
- 设置开机启动
sudo systemctl enable mysqld
,查看开机启动状态sudo systemctl list-unit-files | grep mysqld
,禁止开机启动sudo systemctl disable mysqld
- 通过
yum
、rpm
安装的service
服务脚本默认目录为/usr/lib/systemd/system/mysqld.service
五、验证服务
-
初始化
root
密码是随机生成的,可在$log-error
(默认值为:/var/log/mysqld.log
)中,使用sudo cat /var/log/mysqld.log | grep temporary
获得 -
初始化的
root
密码是临时的,需要登陆命令行,进行重置,才能进行后续操作。重置root
密码的SQL
:-- 生产切记使用较复杂的密码 ALTER USER 'root'@'localhost' IDENTIFIED BY 'Pa4word!';
-
验证服务状态:
- 使用
mysql
命令行连接,mysql -u root -p
- 随意执行一条查询
SQL
,或者执行mysqladmin -u root -p version
- 使用
六、异常处理
-
出现
mariadb-libs is obsoleted by mysql-community-libs-8.0.28-1.el7.x86_64
,需要移除mariadb
执行yum remove mysql-libs
-
Yum安装出现
Error: Package: mysql-community-client-plugins-8.0.29-1.el6.x86_64 (mysql80-community) Requires: libsasl2.so.2()(64bit)
或Error: Package: mysql-community-client-plugins-8.0.29-1.el6.x86_64 (mysql80-community) Requires: libudev.so.0()(64bit)
,检查/etc/yum.repos.d/mysql-community.repo
,官方给的是el6
版本的源。如果使用的是
Centos7
,将baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/6/$basearch/
改为baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
-
出现
GPG key retrieval failed: [Errno 14] curl#37 - "Couldn't open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022"
,有两种解决办法:- 更改
/etc/yum.repos.d/mysql-community.repo
,将gpgcheck
值改为0
- 将
gpgkey
存储在/etc/yum.repos.d/mysql-community.repo
,指定的目录。如果本地不存在执行sudo wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022 https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
进行下载
- 更改
-
出现
GPG key retrieval failed: [Errno 14] curl#37 - "Couldn't open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql"
,有两种解决办法:- 更改
/etc/yum.repos.d/mysql-community.repo
,将gpgcheck
值改为0
- 将
gpgkey
存储在/etc/yum.repos.d/mysql-community.repo
,指定的目录。如果本地不存在执行sudo wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql https://repo.mysql.com/RPM-GPG-KEY-mysql
进行下载
- 更改
-
出现
Can't create test file /data/mysql/mysqld_tmp_file_case_insensitive_test.lower-test
,需要关闭系统SELinux -
远程无法连接,执行以下操作:
-- 创建连接用户 create user 'root'@'%' identified by '123456'; -- IP规则可以使用`%`、`127.0.0.%`、`127.0.%.%`等 -- 授权用户访问权限 grant all privileges on *.* to 'root'@'%' with grant option; -- 刷新系统权限 flush privileges;
-
/etc/init.d/mysqld: line 239: my_print_defaults: command not found
,编辑support-files/mysql.server
,将basedir
和datadir
补充完整即可。 -
You must reset your password using ALTER USER statement before executing this statement
初次需要修改密码ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';