MySQL 8.0 InnoDB Cluster集群部署步骤

三台主机

节点A:192.168.23.6

节点B:192.168.23.7

节点C:192.168.23.8

?

1.配置主机ssh互信、hosts

节点A执行:

# ssh-keygen

# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.23.6

# scp -r /root/.ssh root@192.168.23.7:/root/

# scp -r /root/.ssh root@192.168.23.8:/root/

cat >>/etc/hosts <<EOF

192.168.23.6 db1

192.168.23.7 db2

192.168.23.8 db3

EOF

# for ((i=7;i<=8;i++));do scp /etc/hosts root@192.168.23.$i:/etc/;done

2.部署MySQL

# export https_proxy=ffa.dockermix.icu:30510;wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.25-linux-glibc2.12-x86_64.tar

# tar xf mysql-8.0.25-linux-glibc2.12-x86_64.tar

# tar Jxf mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz -C /opt/

# mv /opt/mysql-8.0.25-linux-glibc2.12-x86_64/ /opt/mysql

# for ((i=6;i<=8;i++));do ssh 192.168.23.$i "useradd -s /sbin/nologin -M mysql;mkdir -p /database/mysql;chown -R mysql:mysql /database/mysql";done

# for ((i=7;i<=8;i++));do scp -r /opt/mysql root@192.168.23.$i:/opt/;done

# for ((i=6;i<=8;i++));do ssh root@192.168.23.$i "/opt/mysql/bin/mysqld --initialize-insecure --basedir=/opt/mysql --datadir=/database/mysql --user=mysql";done

# for ((i=7;i<=8;i++));do ssh root@192.168.23.$i "cat /opt/mysql/support-files/mysql.server |sed ‘/^basedir=/s/\(.*\)/\1\/opt\/mysql/;/^datadir=/s/\(.*\)/\1\/database\/mysql/‘ >/etc/init.d/mysql;chmod 755 /etc/init.d/mysql;ln -svf /usr/lib64/libtinfo.so.6 /usr/lib64/libtinfo.so.5";done

3.配置my.cnf

节点A:

my.cnf配置:

[mysql]

default-character-set = utf8mb4

[mysqlshow]

default-character-set = utf8mb4

[mysqlimport]

default-character-set = utf8mb4

[mysqlcheck]

default-character-set = utf8mb4

[mysql_upgrade]

default-character-set = utf8mb4

[mysqltest]

default-character-set = utf8mb4

[mysqld]

auto_increment_increment = 1

auto_increment_offset = 1

connect-timeout = 60

enforce-gtid-consistency

gtid-mode = on

innodb-buffer-pool-size = 24M

innodb-log-file-size = 5M

log-bin = server-binary-log

log-bin-trust-function-creators = 1

loose-group_replication_enforce_update_everywhere_checks = FALSE

loose-group_replication_group_name = ‘85de4f4d-b40b-495f-bb85-6d5d6b4bc3e0‘

loose-group_replication_group_seeds = ‘192.168.23.6:33061,192.168.23.7:33061,192.168.23.8:33061‘

loose-group_replication_local_address = ‘192.168.23.6:33061‘

loose-group_replication_recovery_get_public_key = TRUE

loose-group_replication_single_primary_mode = TRUE

loose-group_replication_start_on_boot = OFF

loose-innodb_read_io_threads = 1

loose-innodb_write_io_threads = 1

master-retry-count = 10

plugin_load_add = ‘group_replication.so‘ #(相当于mysql> INSTALL PLUGIN group_replication SONAME ‘group_replication.so‘;)

relay-log = server-relay-log

report-host = 192.168.23.6

report-user = root

server-id = 6

skip-slave-start

slave-load-tmpdir = /tmp

节点B和节点C把橙色内容中IP地址和id替换掉。

4.启动mysql服务

节点A、B、C都执行:

# service mysql start

5.修改root密码,设置连接账户repl和icadmin账户

节点A、B、C都执行:

# /opt/mysql/bin/mysql -uroot -e "SET SQL_LOG_BIN=0;SET global super_read_only=OFF; SET global read_only=OFF; alter user ‘root‘@‘localhost‘ password expire never; set password for ‘root‘@‘localhost‘=‘123456‘;create user if not exists repl@‘%‘ identified by ‘123456‘;grant all on *.* to repl@‘%‘ with grant option;create user if not exists icadmin@‘%‘ identified by ‘123456‘;grant all on *.* to icadmin@‘%‘ with grant option;flush privileges; set global super_read_only=1;set global read_only=1;SET SQL_LOG_BIN=1;"

6.启动组复制

节点A、B、C都执行:

#/opt/mysql/bin/mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_USER=‘repl‘, MASTER_PASSWORD=‘123456‘ FOR CHANNEL ‘group_replication_recovery‘;"

节点A执行:

# /opt/mysql/bin/mysql -uroot -e "set global group_replication_bootstrap_group=on;start group_replication user=‘repl‘,password=‘123456‘;set global group_replication_bootstrap_group=off"

节点B和C执行:

# /opt/mysql/bin/mysql -uroot -e "start group_replication user=‘repl‘,password=‘123456‘"

7.查看组复制成员

在任一节点上执行:

# /opt/mysql/bin/mysql -uroot -e "select * from performance_schema.replication_group_members\G"

?

8.安装mysql-shell

节点A执行:

# tar zxf mysql-shell-8.0.25-linux-glibc2.12-x86-64bit.tar.gz -C /opt/

# mv /opt/mysql-shell-8.0.25-linux-glibc2.12-x86-64bit/ /opt/mysqlshell

# for ((i=7;i<=8;i++));do scp -r /opt/mysqlshell root@192.168.23.$i:/opt;done

在三个节点都执行:

# echo "export PATH=\$PATH:/opt/mysqlshell/bin" >>/etc/profile.d/mysqlshell.sh

# source /etc/profile.d/mysqlshell.sh

9.创建集群

节点A执行:

# mysqlsh -uicadmin -p123456 -h localhost

?

MySQL? localhost:33060+ ssl? JS > var cluster = dba.createCluster(‘testCluster’);

?

MySQL? localhost:33060+ ssl? JS > dba.getCluster(‘testCluster’).status();

?

?

10.安装MySQL Router并启动

中间件服务器要与应用服务器合设或者独立设置

在节点A上:

# tar Jxf mysql-router-8.0.25-linux-glibc2.12-x86_64.tar.xz -C /opt

# mv /opt/mysql-router-8.0.25-linux-glibc2.12-x86_64/ /opt/mysqlrouter

# echo "export PATH=\$PATH:/opt/mysqlrouter/bin" >>/etc/profile.d/mysqlrouter.sh

# source /etc/profile.d/mysqlrouter.sh

# mkdir /database/mysqlrouter

# chown -R mysql:mysql /database/mysqlrouter

# mysqlrouter --bootstrap icadmin:123456@192.168.23.6 --directory /database/mysqlrouter --conf-use-sockets --user=mysql --name=mysql_router_13306 --conf-bind-address=192.168.23.6 --account-host="192.168.23.%" --force

# /database/mysqlrouter/start.sh

测试6446写接口:

# mysql -uicadmin -p123456 -h 192.168.23.6 -P 6446

mysql> select @@hostname as hostname, @@port as port;

+--------------+------+

| hostname???? | port |

+--------------+------+

| ccab8856acfd | 3306 |

+--------------+------+

//ccab8856acfd是192.168.23.6的hostname

1 row in set (0.00 sec)

mysql> create database db1;

mysql> use db1;

Database changed

mysql> create table table1 (id int,column1 varchar(20));

Query OK, 0 rows affected (0.04 sec)

测试6447读接口:

# mysql -uicadmin -p123456 -h 192.168.23.6 -P 6447

mysql> select @@hostname as hostname, @@port as port;

+--------------+------+

| hostname???? | port |

+--------------+------+

| 0984f87b6579 | 3306 |

+--------------+------+

1 row in set (0.00 sec)

//0984f87b6579是192.168.23.8的hostname

mysql> select @@hostname as hostname, @@port as port;

+--------------+------+

| hostname???? | port |

+--------------+------+

| b41f91fece42 | 3306 |

+--------------+------+

1 row in set (0.00 sec)

//b41f91fece42是192.168.23.7的hostname

mysql> desc db1.table1;

+---------+-------------+------+-----+---------+-------+

| Field?? | Type??????? | Null | Key | Default | Extra |

+---------+-------------+------+-----+---------+-------+

| id????? | int???????? | YES? |???? | NULL??? |?????? |

| column1 | varchar(20) | YES? |???? | NULL??? |?????? |

+---------+-------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

小结:

InnoDB Cluster使用组复制,因此您的服务器实例必须满足相同的要求。AdminAPI提供了 dba.checkInstanceConfiguration()验证实例是否满足组复制要求的dba.configureInstance()方法,以及 配置实例以满足要求的方法:

?

?

?

?

?

?

?

?

?

?

?

?

?

?

上一篇:10条SQL优化技巧


下一篇:Mysql优化(1) 存储引擎、数据类型、字符集