Skip to main content

MySQL 8

本文内容

  • 通过 binary 方式部署 MySQL8
  • 为 MySQL8 配置 Service 并设置开机启动

背景描述

  • 服务器访问不到内网,软件只能离线部署
  • 服务器的 OS 版本为 CentOS 7.9

安装包准备

点击进入 MySQL 安装包官方下载地址,选择我们服务器对应的安装包。在这里我选择的是

  • Product Version: 8.0.33
  • Operating System: Red Hat Enterprise Linux / Oracle Linux
  • OS Version: Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit)

版本选择后可以看到最下方有 Compress TAR Archive, 点击右边的 Download 按钮下载。
如果你的OS版本和笔者一样,也直接点击 这里 下载。
下载完毕后,我们得到安装包 mysql-8.0.33-el7-x86_64.tar.gz

MySQL8安装

上传 MySQL8 安装包到服务器,解压并将解压后的目录移动到 /usr/local/mysql

$ tar zxf mysql-8.0.33-el7-x86_64.tar.gz 
$ sudo mv mysql-8.0.33-el7-x86_64 /usr/local/mysql

权限配置 (我打算将/data/mysql作为MySQL8的数据目录,所以在这里一并创建)

$ sudo groupadd mysql
$ sudo useradd -r -g mysql mysql
$ sudo chown -R mysql:mysql /usr/local/mysql/
$ sudo chmod -R 755 /usr/local/mysql/
$ sudo mkdir -p /data/mysql
$ sudo chown -R mysql:mysql /data/mysql/

编辑 MySQL 8 配置文件 /etc/my.cnf

$ sudo vi /etc/my.cnf
[mysqld]
log-error=/usr/local/mysql/mysqld.log
pid-file=/usr/local/mysql/mysql.pid
datadir=/data/mysql
socket=/usr/local/mysql/mysql.sock
lower_case_table_names=1

[mysqld_safe]
log-error=/usr/local/mysql/mysqld.log
pid-file=/usr/local/mysql/mysql.pid
datadir=/data/mysql
socket=/usr/local/mysql/mysql.sock

[mysql]
socket=/usr/local/mysql/mysql.sock

[mysqldump]
socket=/usr/local/mysql/mysql.sock

这里需要注意一点,MySQL8 只有在初始化的时候,才能修改 lower_case_table_names这个配置,所以大家一开始就需要根据实际场景,来配置 lower_case_table_names,避免后面出现不兼容的情况。lower_case_table_names 默认为0.

初始化 MySQL8

$ cd /usr/local/mysql/bin/
$ sudo ./mysqld --initialize --user=mysql --lower-case-table-names=1 --datadir=/data/mysql --basedir=/usr/local/mysql/
$ sudo grep -i password /usr/local/mysql/mysqld.log

最后一步执行完,可得到 MySQL8 的root临时密码,需要记录下来。比如这里我输出的是
A temporary password is generated for root@localhost: XXXXXXX

到这里 MySQL8 已经安装好了,接下来我们为 MySQL8 配置服务

配置 MySQL8 Service

$ sudo ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
$ sudo ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
$ sudo ln -s /usr/local/mysql/mysql.sock /var/mysql.sock
$ sudo vi /usr/lib/systemd/system/mysql.service
[Unit]
Description=MySQL Server
After=network.target
After=syslog.target
[Service]
User=mysql
Group=mysql
Type=forking
PermissionsStartOnly=true
ExecStart=/etc/init.d/mysql start
ExecStop=/etc/init.d/mysql stop
ExecReload=/etc/init.d/mysql restart
LimitNOFILE = 5000
LimitNPROC = 5000
[Install]
WantedBy=multi-user.target

$ sudo systemctl daemon-reload
$ sudo systemctl stop mysql
$ sudo systemctl restart mysql
$ sudo chkconfig mysql on

到这里 MySQL 8的服务已经正常配置且启动了,接下来我们用上面获取到的临时密码登录进去,并修改密码

$ mysql -uroot -p 
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.33

Copyright (c) 2000, 2023, 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> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.02 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye

自此 MySQL8 已经成功部署。