更多文章,欢迎关注作者公众号,欢迎一起交流。
MySQL 8.2.0创新版本已于2023-10-17发布,MySQL Router 8.2 支持数据库的读/写分离,这里将在InnoDB Cluster集群中演示数如何进行读写分离,本篇内容包括:MySQL Server数据库安装、MySQL Shell安装、MySQL Router安装、InnoDB Cluster安装与读写分离演示,若您只关注读写分离的演示,可直接跳至最后章节。
1 数据库环境
数据库环境为:
环境详情 |
节点1 |
节点2 |
节点3 |
Server IP |
192.168.56.101 |
192.168.56.102 |
192.168.56.103 |
Hostname |
clusternode01 |
clusternode02 |
clusternode03 |
Server ID |
56101 |
56102 |
56103 |
DB Version |
MySQL 8.2 |
MySQL 8.2 |
MySQL 8.2 |
OS |
Redhat 8.7 |
Redhat 8.7 |
Redhat 8.7 |
2 数据库安装
分别在clusternode01、clusternode02、clusternode03节点安装MySQL数据库软件并初始化数据库,这里以在节点clusternode01为例进行创建,具体过程为:
1)关闭防火墙
关闭防火墙,并修改/etc/selinux/config文件的SELINUX值为disabled:
[root@clusternode01 ~]# systemctl status firewalld
[root@clusternode01 ~]# systemctl stop firewalld
[root@clusternode01 ~]# systemctl disable firewalld
2)安装数据库软件所需的软件包
安装libaio库,该libaio库是数据目录初始化和后续服务器启动所需:
[root@clusternode01 ~]# yum install libaio
对于Red at 8 / Oracle Linux 8 ,默认没有安装/lib64/libtinfo.so.5文件,该文件是MySQL客户端bin/mysql所需:
[root@clusternode01 ~]# yum install ncurses-compat-libs
3)创建用户和组
创建数据库所需的用户和组:
[root@clusternode01 ~]# groupadd mysql
[root@clusternode01 ~]# useradd -r -g mysql -s /bin/false mysql
4)规整化数据目录
为便于数据库管理,规整化目录用于存放对应的数据文件:
[root@clusternode01 ~]# cat dir.sh
mkdir -p /data/mysqldata/mydata
mkdir -p /data/mysqldata/innodb_ts
mkdir -p /data/mysqldata/log
mkdir -p /data/mysqldata/binlog
mkdir -p /data/mysqldata/relaylog
mkdir -p /data/mysqldata/innodb_log
mkdir -p /data/mysqldata/innodb_undo
mkdir -p /data/mysqldata/redolog_arch
mkdir -p /data/mysqldata/tmpdir
mkdir -p /data/mysqldata/mysecfiles
mkdir -p /data/mysqldata/sock
chown -R mysql:mysql /data/
chmod -R 750 /data
[root@clusternode01 ~]# source dir.sh
5)上传安装包
使用上传工具,将MySQL安装包上传至指定目录下,这里上传到/root目录:
[root@clusternode01 ~]# ll p35*
-rw-r--r--. 1 root root 556929507 Nov 3 22:11 p35939952_100_Linux-x86-64.zip
-rw-r--r--. 1 root root 19652639 Nov 3 22:11 p35940310_100_Linux-x86-64.zip
-rw-r--r--. 1 root root 102405437 Nov 3 22:11 p35942582_100_Linux-x86-64.zip
安装包说明,公众号回复【MySQL 8.2安装包】即可获取:
35940310 MySQL Router 8.2.0 TAR for Generic Linux (glibc2.28) x86 (64bit) (Patchset)
35942582 MySQL Shell 8.2.0 TAR for Generic Linux (glibc2.28) x86 (64bit) (Patchset)
35939952 MySQL Commercial Server 8.2.0 TAR for Generic Linux (glibc2.28) x86 (64bit) (Patchset)
6)解压缩MySQL安装包
[root@clusternode01 ~]# unzip -q p35939952_100_Linux-x86-64.zip
[root@clusternode01 ~]# cd /usr/local/
[root@clusternode01 local]# tar xvf /root/mysql-commercial-8.2.0-linux-glibc2.28-x86_64.tar.xz
7)创建软链接
[root@clusternode01 local]# ln -s mysql-commercial-8.2.0-linux-glibc2.28-x86_64 mysql
8)创建配置文件
创建配置文件/etc/my.cnf,用于设置数据库相关的参数以及数据目录位置,因配置较多,这里省略,公众号回复【 my.cnf】获取完整配置文件,三个节点的server_id不一样。
9)初始化数据库
初始化数据库后会在错误日志中生成root@localhost初始化密码,需修改密码后方可使用数据库:
[root@clusternode01 local]# cd mysql
[root@clusternode01 mysql]# ./bin/mysqld --initialize --user=mysql
[root@clusternode01 ~]# more /data/mysqldata/log/clusternode01.err |grep "A temporary password"
2023-11-04T22:08:40.283252+08:00 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Fwu;-a:kM5bh
10)拷贝mysql.server至/etc/init.d目录,可使用service命令进行启停数据库
[root@clusternode01 mysql]# cp support-files/mysql.server /etc/init.d/mysql.server
11)启动数据库
[root@clusternode01 mysql]# service mysql.server start
Starting MySQL..... SUCCESS!
[root@clusternode01 mysql]# service mysql.server status
SUCCESS! MySQL running (4136)
12)登录数据库并修改用户root@localhost密码,创建root账户
[root@clusternode01 mysql]# ./bin/mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.2.0-commercial
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 "alen#2023";
Query OK, 0 rows affected (0.02 sec)
mysql> create user root identified by "alen#2023";
Query OK, 0 rows affected (0.04 sec)
mysql> grant all on . to root with grant option;
Query OK, 0 rows affected (0.03 sec)
13)配置环境变量,并使其生效
[root@clusternode01 ~]# echo export "PATH=$PATH:/usr/local/mysql/bin" >> /root/.bash_profile
[root@clusternode01 ~]# . .bash_profile
3 InnoDB Cluster集群安装与配置
clusternode01、clusternode02、clusternode03节点的数据库安装并初始化完成后,接下来将配置InnoDB Cluster集群,InnoDB Cluster集群包括MySQL Server、MySQL Shell