目录
1、下载解压包
2、解压文件
2.1 压缩包解压之后如下
2.2 选择第一个文件再次解压
3、配置主从服务器的配置文件:在命令行中直接跟着命令走一遍即可调通
3.1 配置master主服务器的配置文件:创建并修改my.cnf
3.2 配置从服务器slave1的配置文件:创建并修改my.cnf
3.3 配置从服务器slave2的配置文件:创建并修改my.cnf
3.4 初始化:master和slave服务器与三个窗口截图
3.5 启动:master和slave服务器与三个窗口截图
3.6 在master主服务器上创建授权用户+查看master状态
3.7 在slave1和slave2从服务器上分别连接master主服务器
3.7.1 在slave1从服务器上连接master主服务器+查看slave1状态
3.7.2 在slave2从服务器上连接master主服务器+查看slave2状态
3.8 测试:一主两从的数据库集群
3.8.1 在master主服务器中依次执行如下命令
3.8.2 在slave1和slave2两个从服务器中依次执行如下命令
3.8.3 测试结束后关闭master节点(两个slave节点类似)
4、备注
99、参考
前言:本文是基于Mac环境实现【一主二从】的MySQL主从复制实战(本文使用Socket连接,而非TCP/IP协议连接)。想要了解主从复制原理的童鞋,可以参考:MySQL主从复制原理解析
打开 MySQL 官网地址:https://dev.mysql.com/downloads/mysql/ ,选择免安装版本。
选择压缩包 mysql-8.0.24-macos11-x86_64.tar.gz
解压,然后把解压之后的文件手动复制三份,
分别命名文件夹为:mysql-8.0.24-master、mysql-8.0.24-slave1、mysql-8.0.24-slave2。
(1)对于master主
服务器,在support-files
文件夹下面 新建一个配置文件my.cnf:
# 进入 master 目录
cd /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-master/support-files
touch my.cnf
open -e my.cnf
(2)对于master主
服务器,配置文件my.cnf的内容如下:ip=127.0.0.1,port=3306
[client]
default-character-set=outfit
#password = your_password
port = 3306
#修改socket文件的位置,默认是走的/tmp下的mysql.sock会有冲突
#这是报错 Another process with pid 77346 is using unix socket file.
#客户端也需要这个和服务端的一致
socket = /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-master/mysql.sock
[mysqld]
event_scheduler=ON
character-set-server=utf8
init_connect='SET NAMES utf8'
port = 3306
mysqlx_port = 33060
bind-address=127.0.0.1
#修改socket文件的位置,默认是走的/tmp下的mysql.sock会有冲突
socket = /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-master/mysql.sock
mysqlx_socket= /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-master/mysqlx.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
character-set-server=utf8
init_connect='SET NAMES utf8'
#修改mysql的主目录
basedir=/Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-master
#添加data文件的目录,存储各种数据和日志
datadir=/Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-master/data
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
default-character-set=utf8
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
(1)对于slave1从
服务器,在support-files
文件夹下面 新建一个配置文件my.cnf:
# 进入 slave1 目录
cd /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave1/support-files
touch my.cnf
open -e my.cnf
(2)对于slave1从
服务器,配置文件my.cnf的内容如下:ip=127.0.0.1,port=3316
[client]
default-character-set=utf8
#password = your_password
#修改端口号不要和主库一致
port = 3316
#修改socket文件的位置,默认是走的/tmp下的mysql.sock会有冲突
#这是报错 Another process with pid 77346 is using unix socket file.
#客户端也需要这个和服务端的一致
socket = /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave1/mysql.sock
[mysqld]
event_scheduler=ON
character-set-server=utf8
init_connect='SET NAMES utf8'
#修改端口号
port = 3316
mysqlx_port = 33061
bind-address=127.0.0.1
#修改的socket文件的位置,默认是走的/tmp下的mysql.sock会有冲突
socket = /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave1/mysql.sock
mysqlx_socket= /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave1/mysqlx.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
character-set-server=utf8
init_connect='SET NAMES utf8'
#修改mysql的主目录
basedir=/Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave1
#添加data文件的目录,存储各种数据和日志
datadir=/Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave1/data
log-bin=mysql-bin
binlog_format=mixed
#不要和主库一致
server-id = 2
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
default-character-set=utf8
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
(1)对于slave2从
服务器,在support-files
文件夹下面 新建一个配置文件my.cnf:
# 进入 slave2 目录
cd /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave2/support-files
touch my.cnf
open -e my.cnf
(1)对于slave2从
服务器,配置文件my.cnf的内容如下:ip=127.0.0.1,port=3326
[client]
default-character-set=utf8
#password = your_password
#修改端口号不要和主库一致
port = 3326
#修改socket文件的位置,默认是走的/tmp下的mysql.sock会有冲突
#这是报错 Another process with pid 77346 is using unix socket file.
#客户端也需要这个和服务端的一致
socket = /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave2/mysql.sock
[mysqld]
event_scheduler=ON
character-set-server=utf8
init_connect='SET NAMES utf8'
#修改端口号
port = 3326
mysqlx_port = 33062
#修改socket文件的位置,默认是走的/tmp下的mysql.sock会有冲突
socket = /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave2/mysql.sock
mysqlx_socket= /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave2/mysqlx.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
character-set-server=utf8
init_connect='SET NAMES utf8'
#修改mysql的主目录
basedir=/Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave2
#添加data文件的目录,存储各种数据和日志
datadir=/Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave2/data
log-bin=mysql-bin
binlog_format=mixed
#不要和主库一致
server-id = 3
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
default-character-set=utf8
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
使用命令行进入到对应目录,执行以下语句。
# 进入 master 目录
cd /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-master/bin
# 执行 master 初始化
./mysqld --defaults-file=/Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-master/support-files/my.cnf --initialize-insecure
# 进入 slave1 目录
cd /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave1/bin
# 执行 slave1 初始化
./mysqld --defaults-file=/Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave1/support-files/my.cnf --initialize-insecure
# 进入 slave2 目录
cd /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave2/bin
# 执行 slave2 初始化
./mysqld --defaults-file=/Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave2/support-files/my.cnf --initialize-insecure
【问题出现】每一个窗口在执行过程中,可能会出现提示 [mysqld] 不安全等各种不安全提示:
【问题解决】直接选择 Cancel
,然后,进入系统安全
里面,选择 allow anyway
,然后,再次执行上述命令,选择 open
就可以继续运行了。
使用命令行进入到对应目录,执行以下语句。
# 进入 master 目录
cd /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-master/bin
# 启动 master
./mysqld --defaults-file=/Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-master/support-files/my.cnf
# 进入 slave1 目录
cd /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave1/bin
# 启动 slave1
./mysqld --defaults-file=/Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave1/support-files/my.cnf
# 进入 slave2 目录
cd /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave2/bin
# 启动 slave2
./mysqld --defaults-file=/Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave2/support-files/my.cnf
# 进入 master 目录
cd /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-master/bin
# 命令行方式连接并进入 master 主库
bin> ./mysql -h 127.0.0.1 -u root -P 3306
# 创建用户(此处使用 123456 生成密码,需要记住密码)
# create user 'copymaster'@'%' identified by '123456';
# create user 'copymaster'@'%' IDENTIFIED BY RANDOM PASSWORD;
mysql> create user 'copymaster'@'%' identified by '123456';
# 授权远程同步
mysql> grant replication slave on *.* to 'copymaster'@'%';
# 保存刷新
mysql> flush privileges;
# 查看master主库状态:记录File和Position的值,slave中需要用到
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 869 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
# 进入 slave1 从库的状态
cd /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave1/bin
# 在从库中使用刚才主库创建的账号,连接一次,看否账号可用(使用刚才创建账号对应的密码)
bin> ./mysql -h 127.0.0.1 -u copymaster -p -P 3306
然后输入密码:123456
# 如果登录没有问题,则 quit 退出,进入 slave1 自己的数据库
bin> ./mysql -h 127.0.0.1 -u root -P 3316
# 使用命令(注意此处替换自己创建的数据库账号,以及修改对应 master_log_file 名称以及偏移位置)
# change master to master_host='127.0.0.1',master_port=3306,master_user='copymaster',master_password='VrQ>-YtFPGw&-sJ,hI2Q', master_log_file='mysql-bin.000004',master_log_pos=156;
mysql> change master to master_host='127.0.0.1',master_port=3306,master_user='copymaster',master_password='123456', master_log_file='mysql-bin.000002',master_log_pos=869;
# 启动 slave1 同步
mysql> start slave;
# 如需要关闭 slave1 同步,则使用命令:stop slave;
# 查看同步状态
mysql> show slave status\G;
# 注意观察 Slave_IO_Running 和 Slave_SQL_Running 的状态值,只有都为 Yes 的时候才表明同步 ok,同理 Slave2 也是相同的操作步骤。
# 进入 slave2 从库的状态
cd /Users/cmm/Downloads/MySQL-master-slave/mysql-8.0.24-slave2/bin
# 在从库中使用刚才主库创建的账号,连接一次,看否账号可用(使用刚才创建账号对应的密码)
bin> ./mysql -h 127.0.0.1 -u copymaster -p -P 3306
然后输入密码:123456
# 如果登录没有问题,则 quit 退出,进入 slave2 自己的数据库
bin> ./mysql -h 127.0.0.1 -u root -P 3316
# (注意:这一步在slave2中不需要执行,因为在slave1中已经执行过了!!!)使用命令(注意此处替换自己创建的数据库账号,以及修改对应 master_log_file 名称以及偏移位置)
# mysql> change master to master_host='127.0.0.1',master_port=3306,master_user='copymaster',master_password='123456', master_log_file='mysql-bin.000002',master_log_pos=869;
# 启动 slave2 同步
mysql> start slave;
# 如需要关闭 slave2 同步,则使用命令:stop slave;
# 查看同步状态
mysql> show slave status\G;
# 注意观察 Slave_IO_Running 和 Slave_SQL_Running 的状态值,只有都为 Yes 的时候才表明同步 ok。
另:在 slave1窗口 和 slave2窗口 上执行 show slave status\G; 的结果如下图所示:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: copymaster
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 869
Relay_Log_File: cmmMacPro-relay-bin.000003
Relay_Log_Pos: 324
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: 869
Relay_Log_Space: 705
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: 1
Master_UUID: e7a012fa-a3e3-11eb-a952-a12e0386965f
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: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.01 sec)
此时我们使用 Sequel Pro、DataGrip 等DBMS连接数据库,在 master 修改数据,然后在slave中查看,看数据是否正确同步。这里我是在命令行中测试的。
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 869 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database CopyMasterSlave;
Query OK, 1 row affected (0.00 sec)
mysql> use CopyMasterSlave;
Database changed
mysql>
mysql> show tables;
Empty set (0.00 sec)
mysql> create table Student(Id int(11) primary key auto_increment,Name varchar(100),Gender varchar(100),Age int(11));
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> select * from Student;
Empty set (0.01 sec)
mysql> insert into Student(Name,Gender,Age) values("zs","男",10);
Query OK, 1 row affected (0.01 sec)
mysql> select * from Student;
+----+------+--------+------+
| Id | Name | Gender | Age |
+----+------+--------+------+
| 1 | zs | 男 | 10 |
+----+------+--------+------+
1 row in set (0.00 sec)
mysql> insert into Student(Name,Gender,Age) values("lisi","男",12),("qingfeng","女",10);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from Student;
+----+----------+--------+------+
| Id | Name | Gender | Age |
+----+----------+--------+------+
| 1 | zs | 男 | 10 |
| 2 | lisi | 男 | 12 |
| 3 | qingfeng | 女 | 10 |
+----+----------+--------+------+
3 rows in set (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 2215 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: copymaster
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 869
Relay_Log_File: cmmMacPro-relay-bin.000003
Relay_Log_Pos: 324
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
......做了省略
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| CopyMasterSlave |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql> use CopyMasterSlave;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_copymasterslave |
+---------------------------+
| Student |
+---------------------------+
1 row in set (0.00 sec)
mysql> select * from Student;
+----+------+--------+------+
| Id | Name | Gender | Age |
+----+------+--------+------+
| 1 | zs | 男 | 10 |
+----+------+--------+------+
1 row in set (0.00 sec)
mysql> select * from Student;
+----+----------+--------+------+
| Id | Name | Gender | Age |
+----+----------+--------+------+
| 1 | zs | 男 | 10 |
| 2 | lisi | 男 | 12 |
| 3 | qingfeng | 女 | 10 |
+----+----------+--------+------+
3 rows in set (0.00 sec)
mysql>
(1)本文是在Mac环境下搭建一主二从的数据库集群,不管是主库还是从库,都是通过配置文件【my.cnf】使用socket方式连接的,而不是使用TCP/IP方式连接。
(2)master、slave1、slave2 中的配置文件 my.cnf,其中有很多内容都是默认的,无需修改,我们只需要关注其中很少的一部分即可,比如:端口号配置、路径配置、服务器唯一标识等。
(1)原理参考:https://blog.csdn.net/cmm0401/article/details/116029006
(2)实战参考:https://www.cnblogs.com/holddie/p/14079223.html