系统运维——mysql 企业级配置

实验环境

mysql-node2

复制代码
[root@localhost ~]# hostnamectl set-hostname mysql-node2.timinglee.org
[root@localhost ~]# exit
logout
​
Connection closed.
​
Disconnected from remote host(172.25.254.140:22) at 11:39:30.
​
Type `help' to learn how to use Xshell prompt.
[C:\~]$ ssh root@172.25.254.140
​
​
Connecting to 172.25.254.140:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
​
WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Thu Aug 22 11:37:40 2024 from 172.25.254.1
[root@mysql-node2 ~]# nmcli connection modify ens33 ipv4.addresses 172.25.254.20/24 ipv4.method manual connection.autoconnect yes
[root@mysql-node2 ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.140  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::da8c:dc95:8517:bfff  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:57:fc:c4  txqueuelen 1000  (Ethernet)
        RX packets 348  bytes 31686 (30.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 223  bytes 25211 (24.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

改IP。

复制代码
[root@mysql-node2 ~]# nmcli connection modify ens33 ipv4.addresses 172.25.254.20/24 ipv4.method manual connection.autoconnect yes

[root@mysql-node2 ~]# nmcli connection up ens33 

Socket error Event: 32 Error: 10053.
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(172.25.254.140:22) at 15:34:09.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ ssh root@172.25.254.20


Connecting to 172.25.254.20:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Thu Aug 22 11:39:54 2024 from 172.25.254.1
[root@mysql-node2 ~]# 

挂载。

复制代码
[root@mysql-node2 ~]# mount /dev/sr0 /mnt/
mount: /dev/sr0 is write-protected, mounting read-only

下载依赖。

复制代码
[root@mysql-node2 ~]# yum install cmake gcc-c++ openssl-devel ncurses-devel.x86_64 libtirpc-devel-1.3.3-8.el9_4.x86_64.rpm rpcgen.x86_64

Installed:
  cmake.x86_64 0:2.8.12.2-2.el7                gcc-c++.x86_64 0:4.8.5-44.el7       
  ncurses-devel.x86_64 0:5.9-14.20130511.el7_4 openssl-devel.x86_64 1:1.0.2k-19.el7

传包。

复制代码
[root@mysql-node1 ~]# ls
anaconda-ks.cfg                           mysql-5.7.44
libtirpc-devel-0.2.4-0.16.el7.x86_64.rpm  mysql-boost-5.7.44.tar.gz
[root@mysql-node1 ~]# yum install libtirpc-devel-0.2.4-0.16.el7.x86_64.rpm -y

安装 mysql 。

复制代码
[root@mysql-node1 ~]# ls
anaconda-ks.cfg	libtirpc-devel-0.2.4-0.16.el7.x86_64.rpm  mysql-boost-5.7.44.tar.gz
[root@mysql-node2 ~]# tar zxf mysql-boost-5.7.44.tar.gz 
[root@mysql-node2 ~]# ls
anaconda-ks.cfg  mysql-5.7.44  mysql-boost-5.7.44.tar.gz
[root@mysql-node2 ~]# cd mysql-5.7.44/
[root@mysql-node2 mysql-5.7.44]# 

检测。

复制代码
[root@mysql-node2 mysql-5.7.44]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_EXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_unicode_ci -DWITH_BOOST=/root/mysql-5.7.44/boost/boost_1_59_0/

编译。

复制代码
[root@mysql-node2 mysql-5.7.44]# make -j2
[root@mysql-node2 mysql-5.7.44]# make install
复制代码
[root@mysql-node2 mysql-5.7.44]# cd /usr/local/mysql/
[root@mysql-node2 mysql]# ls
bin   include  LICENSE  mysql-test  README-test  support-files
docs  lib      man      README      share
[root@mysql-node2 mysql]# useradd -s /sbin/nologin -M mysql
[root@mysql-node2 mysql]# mkdir /data/mysql -p
[root@mysql-node2 mysql]# chown mysql.mysql -R /data/mysql/

启动脚本。

复制代码
[root@mysql-node2 mysql]# pwd
/usr/local/mysql
[root@mysql-node2 mysql]# ls
bin   include  LICENSE  mysql-test  README-test  support-files
docs  lib      man      README      share
[root@mysql-node2 mysql]# cd support-files/
[root@mysql-node2 support-files]# ls
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@mysql-node2 support-files]# vim mysql.server
复制代码
[root@mysql-node2 support-files]# cp mysql.server /etc/init.d/mysqld

进配置文件写日志。

复制代码
[root@mysql-node2 support-files]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
复制代码
[root@mysql-node2 support-files]# ls /data/mysql/
[root@mysql-node2 support-files]# 
复制代码
[root@mysql-node2 support-files]# ll /usr/local/mysql/bin/
total 1059020
-rwxr-xr-x. 1 root root   6413584 Aug 22 12:00 innochecksum
-rwxr-xr-x. 1 root root    611920 Aug 22 11:55 lz4_decompress
-rwxr-xr-x. 1 root root   8469448 Aug 22 12:00 myisamchk
-rwxr-xr-x. 1 root root   8020712 Aug 22 12:00 myisam_ftdump
-rwxr-xr-x. 1 root root   7584528 Aug 22 12:00 myisamlog
-rwxr-xr-x. 1 root root   8147880 Aug 22 12:00 myisampack
-rwxr-xr-x. 1 root root   5537376 Aug 22 11:56 my_print_defaults
-rwxr-xr-x. 1 root root   8349832 Aug 22 12:08 mysql
-rwxr-xr-x. 1 root root   7016864 Aug 22 12:08 mysqladmin
-rwxr-xr-x. 1 root root   8955760 Aug 22 12:08 mysqlbinlog
-rwxr-xr-x. 1 root root   7307808 Aug 22 12:08 mysqlcheck
-rwxr-xr-x. 1 root root   8963568 Aug 22 12:08 mysql_client_test
-rwxr-xr-x. 1 root root 202554416 Aug 22 12:08 mysql_client_test_embedded
-rwxr-xr-x. 1 root root      5238 Aug 22 11:54 mysql_config
-rwxr-xr-x. 1 root root   5703384 Aug 22 12:08 mysql_config_editor
-rwxr-xr-x. 1 root root 247378728 Aug 22 12:08 mysqld
-rwxr-xr-x. 1 root root     27038 Aug 22 11:54 mysqld_multi
-rwxr-xr-x. 1 root root     27859 Aug 22 11:54 mysqld_safe
-rwxr-xr-x. 1 root root   7375200 Aug 22 12:08 mysqldump
-rwxr-xr-x. 1 root root      7844 Aug 22 11:54 mysqldumpslow
-rwxr-xr-x. 1 root root 201741784 Aug 22 12:08 mysql_embedded
-rwxr-xr-x. 1 root root   7010824 Aug 22 12:08 mysqlimport
-rwxr-xr-x. 1 root root   8281016 Aug 22 12:08 mysql_install_db
-rwxr-xr-x. 1 root root   5597912 Aug 22 12:08 mysql_plugin
-rwxr-xr-x. 1 root root  16822472 Aug 22 12:08 mysqlpump
-rwxr-xr-x. 1 root root   6959456 Aug 22 12:08 mysql_secure_installation
-rwxr-xr-x. 1 root root   6968472 Aug 22 12:08 mysqlshow
-rwxr-xr-x. 1 root root   7066168 Aug 22 12:08 mysqlslap
-rwxr-xr-x. 1 root root   5973416 Aug 22 11:56 mysql_ssl_rsa_setup
-rwxr-xr-x. 1 root root   7925424 Aug 22 12:08 mysqltest
-rwxr-xr-x. 1 root root 201155360 Aug 22 12:08 mysqltest_embedded
-rwxr-xr-x. 1 root root   5158200 Aug 22 11:56 mysql_tzinfo_to_sql
-rwxr-xr-x. 1 root root  10570088 Aug 22 12:08 mysql_upgrade
-rwxr-xr-x. 1 root root  22343048 Aug 22 12:08 mysqlxtest
-rwxr-xr-x. 1 root root   5679224 Aug 22 12:00 perror
-rwxr-xr-x. 1 root root   5334880 Aug 22 11:56 replace
-rwxr-xr-x. 1 root root   5537048 Aug 22 11:56 resolveip
-rwxr-xr-x. 1 root root   5610112 Aug 22 11:56 resolve_stack_dump
-rwxr-xr-x. 1 root root    160088 Aug 22 11:55 zlib_decompress

修改环境变量。

复制代码
[root@mysql-node2 support-files]# vim ~/.bash_profile 

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin

export PATH
复制代码
[root@mysql-node2 support-files]# source ~/.bash_profile

初始化。

复制代码
[root@mysql-node2 support-files]# ll /data/mysql/ -d
drwxr-xr-x. 2 mysql mysql 6 Aug 22 13:51 /data/mysql/
[root@mysql-node2 support-files]# mysqld --user mysql --initialize
2024-08-22T05:58:53.178397Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-08-22T05:58:53.339965Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-08-22T05:58:53.366416Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-08-22T05:58:53.420686Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 9c613d24-604b-11ef-b0b3-000c2957fcc4.
2024-08-22T05:58:53.421538Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-08-22T05:58:53.498179Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2024-08-22T05:58:53.498196Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2024-08-22T05:58:53.498680Z 0 [Warning] CA certificate ca.pem is self signed.
2024-08-22T05:58:53.528950Z 1 [Note] A temporary password is generated for root@localhost: zFimd/EX/9AE

初始密码为:

复制代码
zFimd/EX/9AE

如果初始化过程中出现问题,执行以下命令后重新初始化。

复制代码
[root@mysql-node2 support-files]# rm -rf /data/mysql/*

设置开机自启动。

复制代码
[root@mysql-node2 support-files]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/mysql-node2.timinglee.org.err'.
 SUCCESS! 
[root@mysql-node2 support-files]# yum install chkconfig -y

[root@mysql-node2 support-files]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
rhnsd          	0:off	1:off	2:on	3:on	4:on	5:on	6:off

[root@mysql-node2 support-files]# chkconfig mysqld on
[root@mysql-node2 support-files]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
rhnsd          	0:off	1:off	2:on	3:on	4:on	5:on	6:of

安全初始化。

复制代码
[root@mysql-node2 ~]# mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: no
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : no

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y     
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

登录数据库。

复制代码
[root@mysql-node2 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.44 Source distribution

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> quit;
Bye
[root@mysql-node2 ~]# 

停止20数据库,同步10。

复制代码
[root@mysql-node2 ~]# /etc/init.d/mysqld stop
Shutting down MySQL. SUCCESS!

mysql-node1

复制代码
[root@mysql-node10 ~]# hostnamectl set-hostname mysql-node1.timinglee.org
[root@mysql-node10 ~]# exit
logout

Connection closed.

Disconnected from remote host(172.25.254.10:22) at 15:51:17.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ ssh root@172.25.254.10


Connecting to 172.25.254.10:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Thu Aug 22 09:27:01 2024 from 172.25.254.1
[root@mysql-node1 ~]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.10  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::929:2f3f:51e2:42d2  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:37:a6:56  txqueuelen 1000  (Ethernet)
        RX packets 372  bytes 34024 (33.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 238  bytes 26261 (25.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 4  bytes 340 (340.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 340 (340.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@mysql-node1 ~]# 
复制代码
[root@mysql-node1 ~]# mount /dev/sr0 /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
[root@mysql-node1 ~]# yum install rsync.x86_64 -y

下载依赖。

复制代码
[root@mysql-node1 ~]# yum install cmake gcc-c++ openssl-devel \
>  ncurses-devel.x86_64 libtirpc-devel-1.3.3-8.el9_4.x86_64.rpm rpcgen.x86_64
复制代码
[root@mysql-node1 mysql-5.7.44]# yum install openssl-devel ncurses-devel libncurses5-dev libtirpc-devel -y
复制代码
[root@mysql-node1 ~]# ls
anaconda-ks.cfg  mysql-boost-5.7.44.tar.gz
[root@mysql-node1 ~]# tar zxf mysql-boost-5.7.44.tar.gz 
[root@mysql-node1 ~]# ls
anaconda-ks.cfg  mysql-5.7.44  mysql-boost-5.7.44.tar.gz
[root@mysql-node1 ~]# cd mysql-5.7.44/
[root@mysql-node1 mysql-5.7.44]# 
复制代码
[root@mysql-node1 mysql-5.7.44]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_EXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_unicode_ci -DWITH_BOOST=/root/mysql-5.7.44/boost/boost_1_59_0/
复制代码
[root@mysql-node1 mysql-5.7.44]# make -j2
[root@mysql-node1 mysql-5.7.44]# make install

10添加用户。

复制代码
[root@mysql-node10 ~]# useradd -s /sbin/nologin -M mysql
[root@mysql-node10 ~]# mkdir -p /data/mysql
[root@mysql-node10 ~]# chown mysql.mysql /data/mysql/

20传数据。

复制代码
[root@mysql-node2 ~]# rsync -al -r mysql root@172.25.254.10:/usr/local/
-bash: rsync: command not found
[root@mysql-node2 ~]# yum search rsync
Loaded plugins: product-id, search-disabled-repos, subscription-manager

This system is not registered with an entitlement server. You can use subscription-manager to register.

================================ N/S matched: rsync ================================
rsync.x86_64 : A program for synchronizing files over a network

  Name and summary matches only, use "search all" for everything.
[root@mysql-node2 ~]# yum install rsync.x86_64 -y

[root@mysql-node2 ~]# cd /usr/local/
[root@mysql-node2 local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  sbin  share  src
[root@mysql-node2 local]# rsync -al -r mysql root@172.25.254.10:/usr/local/
root@172.25.254.10's password:

查看10数据是否传输成功。

复制代码
[root@mysql-node1 ~]# cd /usr/local/
[root@mysql-node1 local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  sbin  share  src
[root@mysql-node1 local]# du -sh mysql/
1.9G	mysql/
[root@mysql-node1 local]# cd mysql/
[root@mysql-node1 mysql]# ls
bin   include  LICENSE  mysql-test  README-test  support-files
docs  lib      man      README      share
复制代码
[root@mysql-node1 mysql]# cp support-files/mysql.server /etc/init.d/mysqld

改配置文件。

复制代码
[root@mysql-node1 mysql]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
复制代码
[root@mysql-node1 mysql]# vim ~/.bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin

export PATH
复制代码
[root@mysql-node1 mysql]# source ~/.bash_profile

初始化。

复制代码
[root@mysql-node1 mysql]# mysqld --user mysql --initialize
2024-08-22T08:08:28.482223Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-08-22T08:08:28.683692Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-08-22T08:08:28.714939Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-08-22T08:08:28.720097Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b6d1c60f-605d-11ef-b05a-000c2937a656.
2024-08-22T08:08:28.720737Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-08-22T08:08:28.799169Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2024-08-22T08:08:28.799185Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2024-08-22T08:08:28.800015Z 0 [Warning] CA certificate ca.pem is self signed.
2024-08-22T08:08:28.870053Z 1 [Note] A temporary password is generated for root@localhost: j+2/8lKoQsR2
[root@mysql-node1 mysql]# 

启动 mysql 。

复制代码
[root@mysql-node1 mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/mysql-node1.timinglee.org.err'.
 SUCCESS! 

安全初始化。输入初始密码,设置新密码 mysql 。

复制代码
[root@mysql-node1 mysql]# mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: no
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : no

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 
[root@mysql-node1 mysql]# 

登录数据库。

复制代码
[root@mysql-node1 mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.44 Source distribution

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> quit;
Bye
[root@mysql-node1 mysql]# 

把编译好的 mysql 文件打包。

复制代码
[root@mysql-node2 local]# cd /usr/local/
[root@mysql-node2 local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  sbin  share  src
[root@mysql-node2 local]# tar zcf mysql.tar.gz mysql/

mysql-node3

改主机名。

复制代码
[root@localhost ~]# hostnamectl set-hostname mysql-node3.timinglee.org
[root@localhost ~]# exit
logout

Connection closed.

Disconnected from remote host(172.25.254.139:22) at 13:42:40.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ ssh root@172.25.254.139


Connecting to 172.25.254.139:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Sat Aug 24 13:42:05 2024 from 172.25.254.1
[root@mysql-node3 ~]# hostnamectl 
   Static hostname: mysql-node3.timinglee.org
         Icon name: computer-vm
           Chassis: vm
        Machine ID: d7e2d1b1747947c4849d402bf671b69d
           Boot ID: e2605ecd576b4781be9efb5da7c45ac9
    Virtualization: vmware
  Operating System: Red Hat Enterprise Linux Server 7.9 (Maipo)
       CPE OS Name: cpe:/o:redhat:enterprise_linux:7.9:GA:server
            Kernel: Linux 3.10.0-1160.el7.x86_64
      Architecture: x86-64

静态IP。

复制代码
[root@mysql-node3 ~]# nmcli connection modify ens33 ipv4.addresses 172.25.254.30/24 ipv4.method manual connection.autoconnect yes
[root@mysql-node3 ~]# nmcli connection up ens33

重连。

复制代码
[C:\~]$ ssh root@172.25.254.30


Connecting to 172.25.254.30:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Sat Aug 24 13:42:49 2024 from 172.25.254.1
[root@mysql-node3 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:9d:79:e3 brd ff:ff:ff:ff:ff:ff
    inet 172.25.254.30/24 brd 172.25.254.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::da8c:dc95:8517:bfff/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::bdc2:dc2a:c867:cdd3/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

下载软件。

复制代码
[root@mysql-node3 ~]# mount /dev/sr0 /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
[root@mysql-node3 ~]# yum install rsync -y

主从复制

一主一从

10主20从。

设置主机 ID 。

复制代码
[root@mysql-node1 ~]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=10

设置从机 ID 。

复制代码
[root@mysql-node2 local]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=20

重启服务。

复制代码
[root@mysql-node2 local]# /etc/init.d/mysqld restart
 ERROR! MySQL server PID file could not be found!
Starting MySQL. SUCCESS! 
复制代码
[root@mysql-node1 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!

查看 ID 是否设置成功。

复制代码
[root@mysql-node1 ~]# mysql -uroot -pmysql -e "SELECT @@server_id"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
|          10 |
+-------------+
[root@mysql-node1 ~]# 
复制代码
[root@mysql-node2 local]# mysql -uroot -pmysql -e "SELECT @@server_id"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
|          20 |
+-------------+
[root@mysql-node2 local]# 

主开启二进制日志。

复制代码
[root@mysql-node1 ~]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=10
log-bin=mysql-bin

重启服务。

复制代码
[root@mysql-node1 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!

查看生成日志目录。

复制代码
[root@mysql-node1 mysql]# ll /data/mysql/

对二进制 ID 进行查看。

复制代码
[root@mysql-node1 mysql]# mysqlbinlog mysql-bin.000001

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
mysqlbinlog: File 'mysql-bin.000001' not found (Errcode: 2 - No such file or directory)
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

主从机关闭防火墙和 selinux 。

登录数据库,建立数据同步使用的用户。

复制代码
[root@mysql-node1 mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44-log Source distribution

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> create user repl@'%' identified by 'mysql';
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to repl@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 154
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

登录从机数据库,同步日志。

复制代码
[root@mysql-node2 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.44 Source distribution

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> change master to master_host='172.25.254.10',master_user='repl',master_password='mysql',master_log_file='mysql-bin.000001',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G

从机只读,不能写入数据。在主机中写入数据测试同步性。

复制代码
mysql> create database tyw;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| tyw                |
+--------------------+
5 rows in set (0.00 sec)

从机查看数据库。

复制代码
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| tyw                |
+--------------------+
5 rows in set (0.00 sec)

主机创建表。

复制代码
mysql> use tyw
Database changed

mysql> create table tyw(
    -> username varchar(10) not null,
    -> password varchar(50) not null
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into tyw values ('test1','111');
Query OK, 1 row affected (0.00 sec)

mysql> select * from tyw;
+----------+----------+
| username | password |
+----------+----------+
| test1    | 111      |
+----------+----------+
1 row in set (0.00 sec)

mysql> 

从机查看。

复制代码
mysql> select * from tyw.tyw;
+----------+----------+
| username | password |
+----------+----------+
| test1    | 111      |
+----------+----------+
1 row in set (0.00 sec)

在从机上写入数据。

复制代码
mysql> insert into tyw.tyw values ('test2','222');
Query OK, 1 row affected (0.01 sec)

mysql> select * from tyw.tyw;
+----------+----------+
| username | password |
+----------+----------+
| test1    | 111      |
| test2    | 222      |
+----------+----------+
2 rows in set (0.00 sec)

mysql> 

主机上不能查看。

复制代码
mysql> select * from tyw.tyw;
+----------+----------+
| username | password |
+----------+----------+
| test1    | 111      |
+----------+----------+
1 row in set (0.00 sec)

mysql> 

一主两从

日志的手动拉平

10登入数据库,master 信息发生改变。

复制代码
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      803 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> select *from tyw.tyw;
+----------+----------+
| username | password |
+----------+----------+
| test1    | 111      |
+----------+----------+
1 row in set (0.01 sec)

mysql> quit;
Bye

上表的信息是在与20同步之后做的,现在要把全部信息手动拉平给30。把10现有的表数据导出来再导进新的slave,再让30去读取节点和日志。

复制代码
[root@mysql-node1 ~]# rsync -al /usr/local/mysql root@172.25.254.30:/usr/local
root@172.25.254.30's password: 
[root@mysql-node1 ~]# 

30操作。

复制代码
[root@mysql-node3 ~]# useradd -s /sbin/nologin -M mysql
[root@mysql-node3 ~]# mkdir -p /data/mysql
[root@mysql-node3 ~]# chown mysql.mysql /data/mysql/
[root@mysql-node3 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

设定环境变量,添加执行目录。

复制代码
[root@mysql-node3 ~]# vim ~/.bash_profile

:/usr/local/mysql/bin
复制代码
[root@mysql-node3 ~]# source ~/.bash_profile

在 mysql 配置文件里添加30的 server-id 。

复制代码
[root@mysql-node3 ~]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=30

初始化。

复制代码
[root@mysql-node3 ~]# mysqld --user=mysql --initialize
2024-08-24T07:33:08.031093Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-08-24T07:33:08.198958Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-08-24T07:33:08.223912Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-08-24T07:33:08.285839Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 1bc4af3b-61eb-11ef-81ed-000c299d79e3.
2024-08-24T07:33:08.286654Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-08-24T07:33:08.350035Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2024-08-24T07:33:08.350050Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2024-08-24T07:33:08.350360Z 0 [Warning] CA certificate ca.pem is self signed.
2024-08-24T07:33:08.376662Z 1 [Note] A temporary password is generated for root@localhost: _oj3eq,uYce6

查看数据存在。

复制代码
[root@mysql-node3 ~]# ll /data/mysql/
total 110660
-rw-r-----. 1 mysql mysql       56 Aug 24 15:33 auto.cnf
-rw-------. 1 mysql mysql     1680 Aug 24 15:33 ca-key.pem
-rw-r--r--. 1 mysql mysql     1112 Aug 24 15:33 ca.pem
-rw-r--r--. 1 mysql mysql     1112 Aug 24 15:33 client-cert.pem
-rw-------. 1 mysql mysql     1680 Aug 24 15:33 client-key.pem
-rw-r-----. 1 mysql mysql      436 Aug 24 15:33 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Aug 24 15:33 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Aug 24 15:33 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 Aug 24 15:33 ib_logfile1
drwxr-x---. 2 mysql mysql     4096 Aug 24 15:33 mysql
drwxr-x---. 2 mysql mysql     8192 Aug 24 15:33 performance_schema
-rw-------. 1 mysql mysql     1676 Aug 24 15:33 private_key.pem
-rw-r--r--. 1 mysql mysql      452 Aug 24 15:33 public_key.pem
-rw-r--r--. 1 mysql mysql     1112 Aug 24 15:33 server-cert.pem
-rw-------. 1 mysql mysql     1676 Aug 24 15:33 server-key.pem
drwxr-x---. 2 mysql mysql     8192 Aug 24 15:33 sys

启动数据库。

复制代码
[root@mysql-node3 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/mysql-node3.timinglee.org.err'.
 SUCCESS!

用初始密码做安全策略。

复制代码
[root@mysql-node3 ~]# mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: no
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : no

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : 

 ... skipping.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : 

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : 

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : 

 ... skipping.
All done! 
[root@mysql-node3 ~]# 

在企业中需要对主进行锁库和锁表后再进行数据拉平。

从master节点备份数据。

复制代码
[root@mysql-node1 ~]# mysqldump -uroot -p tyw > tyw.sql
Enter password: 

查看写过的 sql 语句。

复制代码
[root@mysql-node1 ~]# cat tye.sql

把 sql 语句拷贝到30主机。

复制代码
[root@mysql-node1 ~]# scp tyw.sql root@172.25.254.30:/mnt/
root@172.25.254.30's password: 
scp: /mnt//tyw.sql: Read-only file system

这里报错,推测原因是30的/mnt/ 目录空间不足,因为本机的光盘镜像默认挂载到了这个目录。

复制代码
[root@mysql-node3 ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
devtmpfs               898M     0  898M   0% /dev
tmpfs                  910M     0  910M   0% /dev/shm
tmpfs                  910M  9.6M  901M   2% /run
tmpfs                  910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/rhel-root   47G  3.4G   44G   8% /
/dev/sda1             1014M  150M  865M  15% /boot
tmpfs                  182M     0  182M   0% /run/user/0
/dev/sr0               4.3G  4.3G     0 100% /mnt

换个目录拷贝。

复制代码
[root@mysql-node1 ~]# scp tyw.sql root@172.25.254.30:/opt/
root@172.25.254.30's password: 
tyw.sql                                              100% 1905     1.5MB/s   00:00     

切进30查看是否拷贝成功。

复制代码
[root@mysql-node3 ~]# cd /opt/
[root@mysql-node3 opt]# ls
tyw.sql

30建立 tyw 这个数据库。

复制代码
[root@mysql-node3 opt]# mysql -uroot -pmysql -e "CREATE DATABASE tyw;"
mysql: [Warning] Using a password on the command line interface can be insecure.

把数据导入库。

复制代码
[root@mysql-node3 opt]# mysql -uroot -pmysql tyw < tyw.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

10查看日志。

复制代码
[root@mysql-node1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.44-log Source distribution

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> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      803 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> 

在20上确认10的创建的用户和密码是否正确。

复制代码
[root@mysql-node2 ~]# mysql -urepl -pmysql -h 172.25.254.10
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.44-log Source distribution

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> quit;
Bye
[root@mysql-node2 ~]#  

30同步日志。

复制代码
[root@mysql-node3 opt]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.44 Source distribution

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> change master to master_host='172.25.254.10',master_user='repl',master_password='mysql',master_log_file="mysql-bin.000001",master_log_pos=803;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.25.254.10
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 803
               Relay_Log_File: mysql-node3-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 803
              Relay_Log_Space: 533
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 10
                  Master_UUID: b6d1c60f-605d-11ef-b05a-000c2937a656
             Master_Info_File: /data/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

mysql> 

如果没有显示两个 yes ,则可能是主从配置信息没写对,这时候先把30

复制代码
mysql> stop slave;

再检查信息重新配置,再开启 slave 。

在10中写数据。

复制代码
mysql> INSERT INTO tyw.tyw values ('user2','123');
Query OK, 1 row affected (0.01 sec)

30查看同步。

复制代码
mysql> SELECT * FROM tyw.tyw;
+----------+----------+
| username | password |
+----------+----------+
| test1    | 111      |
| user2    | 123      |
+----------+----------+
2 rows in set (0.00 sec)

20查看是否同步。

复制代码
[root@mysql-node2 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.44 Source distribution

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> SELECT * FROM tyw.tyw;
+----------+----------+
| username | password |
+----------+----------+
| test1    | 111      |
| test2    | 222      |
| user2    | 123      |
+----------+----------+
3 rows in set (0.00 sec)

mysql> 

test2 和 222 是20自己写入的。不会同步到主机10。

主从的生产应用。

假如是购物软件的数据库,则主比较多。

如果是微博或抖音的数据库,则从比较多。

延迟回放

30关闭 sql 线程,设置延迟同步时间,再开启线程。

复制代码
mysql> STOP SLAVE SQL_THREAD;
Query OK, 0 rows affected (0.00 sec)

mysql> change master to master_delay=60;
Query OK, 0 rows affected (0.00 sec)

mysql> start slave sql_thread;
Query OK, 0 rows affected (0.00 sec)

查看是否设置成功。

复制代码
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.25.254.10
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1063
               Relay_Log_File: mysql-node3-relay-bin.000002
                Relay_Log_Pos: 580
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1063
              Relay_Log_Space: 793
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 10
                  Master_UUID: b6d1c60f-605d-11ef-b05a-000c2937a656
             Master_Info_File: /data/mysql/master.info
                    SQL_Delay: 60
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

mysql> 

主机模拟故障。

复制代码
mysql> SELECT * FROM tyw.tyw;
+----------+----------+
| username | password |
+----------+----------+
| test1    | 111      |
| user2    | 123      |
+----------+----------+
2 rows in set (0.00 sec)

mysql> delete from tyw.tyw where username='user2';
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM tyw.tyw;
+----------+----------+
| username | password |
+----------+----------+
| test1    | 111      |
+----------+----------+
1 row in set (0.00 sec)

mysql> 

20查看表。

复制代码
mysql> SELECT * FROM tyw.tyw;
+----------+----------+
| username | password |
+----------+----------+
| test1    | 111      |
| test2    | 222      |
+----------+----------+
2 rows in set (0.00 sec)

30在60秒之内还能查看 user2 的数据。

复制代码
mysql> SELECT * FROM tyw.tyw;
+----------+----------+
| username | password |
+----------+----------+
| test1    | 111      |
| user2    | 123      |
+----------+----------+
2 rows in set (0.00 sec)

mysql> quit;
Bye

在 60 秒之内退出数据库,把数据库的数据导入文件中做备份。

复制代码
[root@mysql-node3 opt]# mysql -uroot -pmysql tyw < tyw.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

再次登入30数据库,查看表的数据是否更新。

复制代码
[root@mysql-node3 opt]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.44 Source distribution

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> SELECT * FROM tyw.tyw;
+----------+----------+
| username | password |
+----------+----------+
| test1    | 111      |
+----------+----------+
1 row in set (0.00 sec)

mysql> 

慢查询日志

10查看慢查询状态。

复制代码
mysql> SHOW variables like "slow%";
+---------------------+----------------------------------+
| Variable_name       | Value                            |
+---------------------+----------------------------------+
| slow_launch_time    | 2                                |
| slow_query_log      | OFF                              |
| slow_query_log_file | /data/mysql/mysql-node1-slow.log |
+---------------------+----------------------------------+
3 rows in set (0.00 sec)

开启主慢查询。

复制代码
mysql> SET GLOBAL slow_query_log=ON;
Query OK, 0 rows affected (0.00 sec)

再次查看慢查询状态。

复制代码
mysql> SHOW variables like "slow%";
+---------------------+----------------------------------+
| Variable_name       | Value                            |
+---------------------+----------------------------------+
| slow_launch_time    | 2                                |
| slow_query_log      | ON                               |
| slow_query_log_file | /data/mysql/mysql-node1-slow.log |
+---------------------+----------------------------------+
3 rows in set (0.00 sec)

查看慢查询时间。

复制代码
mysql> SHOW VARIABLES like "long%";
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)

测试慢查询。输入命令10秒后再显示结果。

复制代码
mysql> select sleep (10);
+------------+
| sleep (10) |
+------------+
|          0 |
+------------+
1 row in set (10.01 sec)

查看慢查询日志。

复制代码
[root@mysql-node1 ~]# cat /data/mysql/mysql-node1-slow.log 
/usr/local/mysql/bin/mysqld, Version: 5.7.44-log (Source distribution). started with:
Tcp port: 3306  Unix socket: /data/mysql/mysql.sock
Time                 Id Command    Argument
# Time: 2024-08-24T17:06:31.325960Z
# User@Host: root[root] @ localhost []  Id:    14
# Query_time: 10.007288  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
SET timestamp=1724519191;
select sleep (10);

并行复制

20设置多线程回放,让主从数据同步速度尽可能放大。

20查看从机线程数量。

复制代码
mysql> show processlist;
+----+-------------+-----------+------+---------+--------+--------------------------------------------------------+------------------+
| Id | User        | Host      | db   | Command | Time   | State                                                  | Info             |
+----+-------------+-----------+------+---------+--------+--------------------------------------------------------+------------------+
|  3 | system user |           | NULL | Connect | 149408 | Waiting for master to send event                       | NULL             |
|  4 | system user |           | NULL | Connect |  34961 | Slave has read all relay log; waiting for more updates | NULL             |
|  7 | root        | localhost | NULL | Sleep   |  34954 |                                                        | NULL             |
|  8 | root        | localhost | NULL | Query   |      0 | starting                                               | show processlist |
+----+-------------+-----------+------+---------+--------+--------------------------------------------------------+------------------+
4 rows in set (0.00 sec)

mysql> quit;
Bye
[root@mysql-node2 ~]#

回放动作和接收动作记录的日志目录。这个日志在磁盘里,通读速度会非常慢。所以在设置多线程时我们可以设置把动作记录在一个表里。

复制代码
[root@mysql-node2 ~]# ls /data/mysql/
auto.cnf         ibtmp1                         mysql.sock.lock
ca-key.pem       master.info                    performance_schema
ca.pem           mysql                          private_key.pem
client-cert.pem  mysql-node2-relay-bin.000002   public_key.pem
client-key.pem   mysql-node2-relay-bin.000003   relay-log.info
ib_buffer_pool   mysql-node2-relay-bin.index    server-cert.pem
ibdata1          mysql-node2.timinglee.org.err  server-key.pem
ib_logfile0      mysql-node2.timinglee.org.pid  sys
ib_logfile1      mysql.sock                     tyw

编辑主配置文件。打开GTID。保证ID强一致性。

复制代码
[root@mysql-node2 ~]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=20
super_read_only=on
gtid_mode=ON
enforce-gtid-consistency=ON
slave-parallel-type=LOGICAL_CLOCK   
slave-parallel-workers=16   
master_info_repository=TABLE             
relay_log_info_repository=TABLE
relay_log_recovery=ON
复制代码
注解:
slave-parallel-type=LOGICAL_CLOCK		#基于组提交
slave-parallel-workers=16		#开启线程数量    
master_info_repository=TABLE		#master信息在表中记录,默认记录在/data/mysql//master.info
relay_log_info_repository=TABLE		#回放日志信息在表中记录,默认记录在/data/mysql/relay-log.info
relay_log_recovery=ON		#日志回放恢复功能开启

重启数据库。

复制代码
[root@mysql-node2 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL. SUCCESS!

进入数据库查看进程数量。可以看到20开启了16个进程来做日志回放。但最终进程数量的多少取决于CPU 硬件的优劣。

复制代码
[root@mysql-node2 ~]# mysql -uroot -pmysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.7.44 Source distribution

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> show processlist;
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| Id | User        | Host      | db   | Command | Time | State                                                  | Info             |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
|  2 | system user |           | NULL | Connect |  304 | Slave has read all relay log; waiting for more updates | NULL             |
|  3 | system user |           | NULL | Connect |  304 | Waiting for an event from Coordinator                  | NULL             |
|  4 | system user |           | NULL | Connect |  304 | Waiting for an event from Coordinator                  | NULL             |
|  5 | system user |           | NULL | Connect |  304 | Waiting for an event from Coordinator                  | NULL             |
|  6 | system user |           | NULL | Connect |  304 | Waiting for an event from Coordinator                  | NULL             |
|  7 | system user |           | NULL | Connect |  304 | Waiting for an event from Coordinator                  | NULL             |
|  8 | system user |           | NULL | Connect |  304 | Waiting for an event from Coordinator                  | NULL             |
| 10 | system user |           | NULL | Connect |  304 | Waiting for an event from Coordinator                  | NULL             |
| 11 | system user |           | NULL | Connect |  304 | Waiting for an event from Coordinator                  | NULL             |
| 12 | system user |           | NULL | Connect |  304 | Waiting for an event from Coordinator                  | NULL             |
| 13 | system user |           | NULL | Connect |  304 | Waiting for an event from Coordinator                  | NULL             |
| 14 | system user |           | NULL | Connect |  304 | Waiting for an event from Coordinator                  | NULL             |
| 15 | system user |           | NULL | Connect |  304 | Waiting for an event from Coordinator                  | NULL             |
| 16 | system user |           | NULL | Connect |  304 | Waiting for an event from Coordinator                  | NULL             |
| 17 | system user |           | NULL | Connect |  304 | Waiting for an event from Coordinator                  | NULL             |
| 18 | system user |           | NULL | Connect |  304 | Waiting for an event from Coordinator                  | NULL             |
| 19 | system user |           | NULL | Connect |  304 | Waiting for an event from Coordinator                  | NULL             |
| 20 | root        | localhost | NULL | Query   |    0 | starting                                               | show processlist |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
18 rows in set (0.00 sec)

mysql> quit
Bye
[root@mysql-node2 ~]#

半同步

gitd日志模式

在 master 端和 slave 端开启 gtid 模式。

复制代码
[root@mysql-node1 ~]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=10
slow_query_log=on
log-bin=mysql-bin
gtid_mode=ON
enforce-gtid-consistency=ON

[root@mysql-node1 ~]# /etc/init.d/mysqld restart
Shutting down MySQL............ SUCCESS! 
Starting MySQL. SUCCESS!
复制代码
[root@mysql-node3 ~]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=30
gtid_mode=ON
enforce-gtid-consistency=ON

[root@mysql-node3 ~]# /etc/init.d/mysqld restart 
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!

20之前配置过,只用重启就好了。

复制代码
[root@mysql-node2 ~]# /etc/init.d/mysqld restart 
Shutting down MySQL. SUCCESS! 
Starting MySQL. SUCCESS!

10 登录数据库查看 master 状态。

复制代码
[root@mysql-node1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.44-log Source distribution

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> SHOW MASTER STATUS\G
*************************** 1. row ***************************
             File: mysql-bin.000002
         Position: 154
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

mysql> SELECT * FROM mysql.gtid_executed;
Empty set (0.00 sec)

显示空,新开会话查看日志。看到显示 automatic 就可以不用管了。

复制代码
[root@mysql-node1 mysql]# mysqlbinlog -vv /data/mysql/mysql-bin.000002

重新设定20、30主机 slave 。

复制代码
[root@mysql-node2 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.7.44 Source distribution

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> mysql> STOP SLAVE;
Query OK, 0 rows affected (0.01 sec)

mysql> change master to
    -> master_host='172.25.254.10',
    -> master_user='repl',
    -> master_password='mysql',
    -> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> START SLAVE;
Query OK, 0 rows affected (0.02 sec)

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.25.254.10
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-node2-relay-bin.000002
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 580
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 10
                  Master_UUID: b6d1c60f-605d-11ef-b05a-000c2937a656
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

mysql> 
复制代码
[root@mysql-node3 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.44 Source distribution

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> STOP SLAVE;
Query OK, 0 rows affected (0.01 sec)

mysql> change master to
    -> master_host='172.25.254.10',
    -> master_user='repl',
    -> master_password='mysql',
    -> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.25.254.10
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-node3-relay-bin.000002
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 580
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 10
                  Master_UUID: b6d1c60f-605d-11ef-b05a-000c2937a656
             Master_Info_File: /data/mysql/master.info
                    SQL_Delay: 60
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more up
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

mysql> 

半同步模式

即使数据传输过程中出现网络波动延迟断线等问题也不会导致主从数据不一致。

编辑10配置文件。

复制代码
[root@mysql-node1 ~]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=10
slow_query_log=on
log-bin=mysql-bin
gtid_mode=ON
enforce-gtid-consistency=ON
rpl_semi_sync_master_enabled=1

[root@mysql-node1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.44-log Source distribution

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> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.00 sec)

#查看插件情况
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| tyw                |
+--------------------+
5 rows in set (0.00 sec)

mysql> SHOW TABLES FROM information_schema;
+---------------------------------------+
| Tables_in_information_schema          |
+---------------------------------------+
| CHARACTER_SETS                        |
| COLLATIONS                            |
| COLLATION_CHARACTER_SET_APPLICABILITY |
| COLUMNS                               |
| COLUMN_PRIVILEGES                     |
| ENGINES                               |
| EVENTS                                |
| FILES                                 |
| GLOBAL_STATUS                         |
| GLOBAL_VARIABLES                      |
| KEY_COLUMN_USAGE                      |
| OPTIMIZER_TRACE                       |
| PARAMETERS                            |
| PARTITIONS                            |
| PLUGINS                               |
| PROCESSLIST                           |
| PROFILING                             |
| REFERENTIAL_CONSTRAINTS               |
| ROUTINES                              |
| SCHEMATA                              |
| SCHEMA_PRIVILEGES                     |
| SESSION_STATUS                        |
| SESSION_VARIABLES                     |
| STATISTICS                            |
| TABLES                                |
| TABLESPACES                           |
| TABLE_CONSTRAINTS                     |
| TABLE_PRIVILEGES                      |
| TRIGGERS                              |
| USER_PRIVILEGES                       |
| VIEWS                                 |
| INNODB_LOCKS                          |
| INNODB_TRX                            |
| INNODB_SYS_DATAFILES                  |
| INNODB_FT_CONFIG                      |
| INNODB_SYS_VIRTUAL                    |
| INNODB_CMP                            |
| INNODB_FT_BEING_DELETED               |
| INNODB_CMP_RESET                      |
| INNODB_CMP_PER_INDEX                  |
| INNODB_CMPMEM_RESET                   |
| INNODB_FT_DELETED                     |
| INNODB_BUFFER_PAGE_LRU                |
| INNODB_LOCK_WAITS                     |
| INNODB_TEMP_TABLE_INFO                |
| INNODB_SYS_INDEXES                    |
| INNODB_SYS_TABLES                     |
| INNODB_SYS_FIELDS                     |
| INNODB_CMP_PER_INDEX_RESET            |
| INNODB_BUFFER_PAGE                    |
| INNODB_FT_DEFAULT_STOPWORD            |
| INNODB_FT_INDEX_TABLE                 |
| INNODB_FT_INDEX_CACHE                 |
| INNODB_SYS_TABLESPACES                |
| INNODB_METRICS                        |
| INNODB_SYS_FOREIGN_COLS               |
| INNODB_CMPMEM                         |
| INNODB_BUFFER_POOL_STATS              |
| INNODB_SYS_COLUMNS                    |
| INNODB_SYS_FOREIGN                    |
| INNODB_SYS_TABLESTATS                 |
+---------------------------------------+
61 rows in set (0.00 sec)

mysql> select * from information_schema.PLUGINS where PLUGIN_NAME LIKE '%semi%'\G
*************************** 1. row ***************************
           PLUGIN_NAME: rpl_semi_sync_master
        PLUGIN_VERSION: 1.0
         PLUGIN_STATUS: ACTIVE
           PLUGIN_TYPE: REPLICATION
   PLUGIN_TYPE_VERSION: 4.0
        PLUGIN_LIBRARY: semisync_master.so
PLUGIN_LIBRARY_VERSION: 1.7
         PLUGIN_AUTHOR: He Zhenxing
    PLUGIN_DESCRIPTION: Semi-synchronous replication master
        PLUGIN_LICENSE: GPL
           LOAD_OPTION: ON
1 row in set (0.00 sec)

#打开半同步功能
mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
Query OK, 0 rows affected (0.00 sec)

 #查看半同步功能状态
mysql> SHOW VARIABLES LIKE 'rpl_semi_sync%';
+-------------------------------------------+------------+
| Variable_name                             | Value      |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled              | ON         |
| rpl_semi_sync_master_timeout              | 10000      |
| rpl_semi_sync_master_trace_level          | 32         |
| rpl_semi_sync_master_wait_for_slave_count | 1          |
| rpl_semi_sync_master_wait_no_slave        | ON         |
| rpl_semi_sync_master_wait_point           | AFTER_SYNC |
+-------------------------------------------+------------+
6 rows in set (0.00 sec)


mysql> SHOW STATUS LIKE 'rpl_semi_sync%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 0     |
| Rpl_semi_sync_master_net_avg_wait_time     | 0     |
| Rpl_semi_sync_master_net_wait_time         | 0     |
| Rpl_semi_sync_master_net_waits             | 0     |
| Rpl_semi_sync_master_no_times              | 0     |
| Rpl_semi_sync_master_no_tx                 | 0     |
| Rpl_semi_sync_master_status                | ON    |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 0     |
| Rpl_semi_sync_master_tx_wait_time          | 0     |
| Rpl_semi_sync_master_tx_waits              | 0     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 0     |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)

mysql> quit;
Bye
[root@mysql-node1 ~]#

在20 slave 端设定半同步。

复制代码
[root@mysql-node2 ~]# /etc/init.d/mysqld stop
Shutting down MySQL. SUCCESS! 
[root@mysql-node2 ~]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=20
#super_read_only=on
gtid_mode=ON
enforce-gtid-consistency=ON
#rpl_semi_sync_master_enable=1

[root@mysql-node2 ~]# /etc/init.d/mysqld start 
Starting MySQL. SUCCESS! 
[root@mysql-node2 ~]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=20
#super_read_only=on
gtid_mode=ON
enforce-gtid-consistency=ON
rpl_semi_sync_master_enable=1

[root@mysql-node2 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.44 Source distribution

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> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.01 sec)

mysql> SET GLOBAL rpl_semi_sync_slave_enabled =1;
Query OK, 0 rows affected (0.00 sec)

mysql> STOP SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)

mysql> START SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE 'rpl_semi_sync%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled     | ON    |
| rpl_semi_sync_slave_trace_level | 32    |
+---------------------------------+-------+
2 rows in set (0.00 sec)

mysql> SHOW STATUS LIKE 'Rpl_semi_sync%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON    |
+----------------------------+-------+
1 row in set (0.00 sec)

mysql> 

在30 slave 端设定半同步。

复制代码
[root@mysql-node3 ~]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=30
gtid_mode=ON
enforce-gtid-consistency=ON
rpl_semi_sync_master_enable=1

[root@mysql-node3 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.44 Source distribution

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> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.01 sec)

mysql> SET GLOBAL rpl_semi_sync_slave_enabled =1;
Query OK, 0 rows affected (0.00 sec)

#重启io线程,半同步才能生效
mysql> STOP SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)

#重启io线程,半同步才能生效
mysql> START SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE 'rpl_semi_sync%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled     | ON    |
| rpl_semi_sync_slave_trace_level | 32    |
+---------------------------------+-------+
2 rows in set (0.01 sec)

mysql> SHOW STATUS LIKE 'Rpl_semi_sync%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON    |
+----------------------------+-------+
1 row in set (0.00 sec)

mysql> 
 

测试。10添加数据查看同步情况

复制代码
mysql> SELECT * FROM tyw.tyw;
+----------+----------+
| username | password |
+----------+----------+
| test1    | 111      |
+----------+----------+
1 row in set (0.02 sec)

mysql> INSERT INTO tyw.tyw values('test2','222');
Query OK, 1 row affected (0.00 sec)

mysql> SHOW STATUS LIKE 'Rpl_semi_sync%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 2     |
| Rpl_semi_sync_master_net_avg_wait_time     | 0     |
| Rpl_semi_sync_master_net_wait_time         | 0     |
| Rpl_semi_sync_master_net_waits             | 2     |
| Rpl_semi_sync_master_no_times              | 0     |
| Rpl_semi_sync_master_no_tx                 | 0     |
| Rpl_semi_sync_master_status                | ON    |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 631   |
| Rpl_semi_sync_master_tx_wait_time          | 631   |
| Rpl_semi_sync_master_tx_waits              | 1     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 1     |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)

mysql> 

20查看数据同步情况。有两个是因为之前20设置主从写的一个。

复制代码
mysql> SELECT * FROM tyw.tyw;
+----------+----------+
| username | password |
+----------+----------+
| test1    | 111      |
| test2    | 222      |
| test2    | 222      |
+----------+----------+
3 rows in set (0.00 sec)

30查看数据同步情况。

复制代码
mysql> SELECT * FROM tyw.tyw;
+----------+----------+
| username | password |
+----------+----------+
| test1    | 111      |
| test2    | 222      |
+----------+----------+
2 rows in set (0.00 sec)

模拟故障。20、30停止 IO 线程。

复制代码
mysql> STOP SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)

此时10写入数据,会很卡顿。

复制代码
mysql> INSERT INTO tyw.tyw values('test3','333');

20、30 开启 IO 线程。

复制代码
mysql> START SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)

10数据显示正常写入。

复制代码
mysql> INSERT INTO tyw.tyw values('test3','333');
Query OK, 1 row affected (10.01 sec)

此时20、30数据同步成功。

复制代码
mysql> SELECT * FROM tyw.tyw;
+----------+----------+
| username | password |
+----------+----------+
| test1    | 111      |
| test2    | 222      |
| test2    | 222      |
| test3    | 333      |
+----------+----------+
4 rows in set (0.00 sec)

组复制

三台主机全部停掉 mysql 。

复制代码
[root@mysql-node1 ~]# /etc/init.d/mysqld stop
Shutting down MySQL............ SUCCESS!

[root@mysql-node2 ~]# /etc/init.d/mysqld stop 
Shutting down MySQL.. SUCCESS!

[root@mysql-node3 ~]# /etc/init.d/mysqld stop 
Shutting down MySQL. SUCCESS!

删掉之前的实验数据,保证干净实验环境。如果在企业中要对数据做备份。

复制代码
[root@mysql-node1 ~]# rm -rf /data/mysql/*

[root@mysql-node2 ~]# rm -rf /data/mysql/*

[root@mysql-node3 ~]# rm -rf /data/mysql/*

编辑主配置文件设置组复制模式。

复制代码
[root@mysql-node1 ~]# vim /etc/my.cnf

#rpl_semi_sync_master_enabled=1
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local-address="172.25.254.10:33061"group_replication_group_seeds="172.25.254.10:33061,172.25.254.20:33061,172.25.254.30:33061"
group_replication_ip_whitelist="172.25.254.0/24,127.0.0.1/8"
group_replication_bootstrap_group=off
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
group_replication_allow_local_disjoint_gtids_join=1

进行数据初始化。

复制代码
[root@mysql-node1 ~]# mysql --user=mysql --initialize
mysql: [ERROR] unknown option '--initialize'
[root@mysql-node1 ~]# mysqld --user=mysql --initialize
2024-08-26T16:05:27.378958Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-08-26T16:05:28.254122Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-08-26T16:05:28.314260Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-08-26T16:05:28.391082Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 03200011-63c5-11ef-ac95-000c2937a656.
2024-08-26T16:05:28.391671Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-08-26T16:05:28.476112Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2024-08-26T16:05:28.476129Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2024-08-26T16:05:28.476431Z 0 [Warning] CA certificate ca.pem is self signed.
2024-08-26T16:05:28.511156Z 1 [Note] A temporary password is generated for root@localhost: (GV5>mPWI-wo
[root@mysql-node1 ~]# 

如果初始化报错要先删掉原来的错误数据再重新进行初始化。

复制代码
rm -rf /data/mysql/*

启用数据库。

复制代码
[root@mysql-node1 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/mysql-node1.timinglee.org.err'.
 SUCCESS!

用初始密码登录数据库

复制代码
[root@mysql-node1 ~]# mysql -uroot -p'(GV5>mPWI-wo'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44-log

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> 

改密码。

复制代码
mysql> alter user root@localhost identified by 'mysql';
Query OK, 0 rows affected (0.00 sec)

为了数据一致性要建立同步数据用的用户,为了安全这步操作不用记录在日志中,所以先关掉日志功能。

复制代码
mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.00 sec)

建立用户。

复制代码
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'tyw';
Query OK, 0 rows affected (0.00 sec)

为建立的用户授权。

复制代码
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)

打开日志功能。

复制代码
mysql> SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)

给主机分配组。

复制代码
mysql> CHANGE MASTER TO MASTER_USER='rpl_user',MASTER_PASSWORD='mysql' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.01 sec)

启用组复制功能。

复制代码
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
Query OK, 0 rows affected (0.00 sec)

开启组复制功能。

复制代码
mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected, 1 warning (2.04 sec)

开启组复制功能后下次进入数据库就不用再设置启用了所以用命令把它关掉。

复制代码
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
Query OK, 0 rows affected (0.00 sec)

查看组复制操作的信息记录。

复制代码
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+---------------------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST               | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+---------------------------+-------------+--------------+
| group_replication_applier | 03200011-63c5-11ef-ac95-000c2937a656 | mysql-node1.timinglee.org |        3306 | ONLINE       |
+---------------------------+--------------------------------------+---------------------------+-------------+--------------+
1 row in set (0.00 sec)

mysql> quit;
Bye
[root@mysql-node1 ~]#

3台主机做本地解析,提高数据同步速度。

复制代码
[root@mysql-node1 ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.10   mysql-node1.timinglee.org
172.25.254.20   mysql-node2.timinglee.org
172.25.254.30   mysql-node3.timinglee.org
复制代码
[root@mysql-node2 ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.10   mysql-node1.timinglee.org
172.25.254.20   mysql-node2.timinglee.org
172.25.254.30   mysql-node3.timinglee.org
复制代码
[root@mysql-node3 ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.10   mysql-node1.timinglee.org
172.25.254.20   mysql-node2.timinglee.org
172.25.254.30   mysql-node3.timinglee.org

10复制基本配置到20、30。

复制代码
[root@mysql-node1 ~]# scp /etc/my.cnf root@172.25.254.20:/etc/my.cnf
root@172.25.254.20's password: 
my.cnf                                       100%  963     1.1MB/s   00:00    
[root@mysql-node1 ~]# scp /etc/my.cnf root@172.25.254.30:/etc/my.cnf
root@172.25.254.30's password: 
my.cnf                                       100%  963     1.5MB/s   00:00 

20主机操作。

复制代码
[root@mysql-node2 ~]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=20
#slow_query_log=on
#log-bin=mysql-bin
gtid_mode=ON
enforce-gtid-consistency=ON
#rpl_semi_sync_master_enabled=1
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local-address="172.25.254.20:33061"
group_replication_group_seeds="172.25.254.10:33061,172.25.254.20:33061,172.25.254.30:33061"
group_replication_ip_whitelist="172.25.254.0/24,127.0.0.1/8"
group_replication_bootstrap_group=off
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
group_replication_allow_local_disjoint_gtids_join=1

[root@mysql-node2 ~]# mysqld --user=mysql --initialize
2024-08-26T16:40:55.786064Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-08-26T16:40:55.929122Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-08-26T16:40:55.952689Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-08-26T16:40:56.016896Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: f749ef58-63c9-11ef-8f23-000c2957fcc4.
2024-08-26T16:40:56.017573Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-08-26T16:40:56.192636Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2024-08-26T16:40:56.192653Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2024-08-26T16:40:56.192972Z 0 [Warning] CA certificate ca.pem is self signed.
2024-08-26T16:40:56.211746Z 1 [Note] A temporary password is generated for root@localhost: lGruzYtBW3&o

[root@mysql-node2 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/mysql-node2.timinglee.org.err'.
 SUCCESS! 
[root@mysql-node2 ~]# mysql -uroot -p'lGruzYtBW3&o'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44-log

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 'mysql';
Query OK, 0 rows affected (0.00 sec)

mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'tyw';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)

mysql> CHANGE MASTER TO MASTER_USER='rpl_user',MASTER_PASSWORD='mysql' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected, 1 warning (6.05 sec)

mysql>

10查看。

复制代码
[root@mysql-node1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 23
Server version: 5.7.44-log Source distribution

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> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+---------------------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST               | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+---------------------------+-------------+--------------+
| group_replication_applier | 03200011-63c5-11ef-ac95-000c2937a656 | mysql-node1.timinglee.org |        3306 | ONLINE       |
| group_replication_applier | f749ef58-63c9-11ef-8f23-000c2957fcc4 | mysql-node2.timinglee.org |        3306 | RECOVERING   |
+---------------------------+--------------------------------------+---------------------------+-------------+--------------+
2 rows in set (0.00 sec)

mysql> 

30主机操作。

复制代码
[root@mysql-node3 ~]# vim /etc/my.cnf
​
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=30
#slow_query_log=on
#log-bin=mysql-bin
gtid_mode=ON
enforce-gtid-consistency=ON
#rpl_semi_sync_master_enabled=1
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local-address="172.25.254.30:33061"
group_replication_group_seeds="172.25.254.10:33061,172.25.254.20:33061,172.25.254.30:33061"
group_replication_ip_whitelist="172.25.254.0/24,127.0.0.1/8"
group_replication_bootstrap_group=off
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
group_replication_allow_local_disjoint_gtids_join=1
​
[root@mysql-node3 ~]# ls /data/mysql/
[root@mysql-node3 ~]# mysqld --user=mysql --initialize
2024-08-26T16:55:49.327377Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-08-26T16:55:49.706384Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-08-26T16:55:49.773322Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-08-26T16:55:49.871540Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 0c115476-63cc-11ef-8ff8-000c299d79e3.
2024-08-26T16:55:49.872018Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-08-26T16:55:50.022520Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2024-08-26T16:55:50.022536Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2024-08-26T16:55:50.022967Z 0 [Warning] CA certificate ca.pem is self signed.
2024-08-26T16:55:50.059570Z 1 [Note] A temporary password is generated for root@localhost: /r&Qq3IbLT#l
[root@mysql-node3 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/mysql-node3.timinglee.org.err'.
 SUCCESS! 
[root@mysql-node3 ~]# mysql -uroot -p'/r&Qq3IbLT#l'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44-log
​
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 'mysql';
Query OK, 0 rows affected (0.00 sec)
​
mysql> 

10查看。

复制代码
相关推荐
MrJson-架构师8 分钟前
4.银河麒麟V10(ARM) 离线安装 MySQL
arm开发·mysql
运维&陈同学23 分钟前
【Beats01】企业级日志分析系统ELK之Metricbeat与Heartbeat 监控
运维·elk·elasticsearch·云原生·kibana·heartbeat·metricbeat
中草药z23 分钟前
【Spring】深入解析 Spring 原理:Bean 的多方面剖析(源码阅读)
java·数据库·spring boot·spring·bean·源码阅读
地球资源数据云25 分钟前
全国30米分辨率逐年植被覆盖度(FVC)数据集
大数据·运维·服务器·数据库·均值算法
是店小二呀33 分钟前
【Linux】Linux开发利器:make与Makefile自动化构建详解
linux·运维·自动化
Ahern_1 小时前
Oracle 普通表至分区表的分区交换
大数据·数据库·sql·oracle
baihb10241 小时前
Jenkins 构建流水线
运维·jenkins
夜半被帅醒1 小时前
MySQL 数据库优化详解【Java数据库调优】
java·数据库·mysql
BUG 4041 小时前
LINUX--shell
linux·运维·服务器
菜鸟小白:长岛icetea2 小时前
Linux零基础速成篇一(理论+实操)
linux·运维·服务器