mysql主从之双主配置

时间:2021-10-26 20:00:38

mysql双主配置

mysql双主其实就是互相同步,互为主从

任意一台都能够执行插入动作

生产环境用得非常少,因为还是担心数据一致的问题

生产环境一般来说主从已经够用

172.19.132.121的配置,自增的话从1开始,每次递增2.数值是1,3,5,7……

[mysqld]
bind-address=0.0.0.0
port=
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
skip-name-resolve
slow_query_log=on
long_query_time=
slow_query_log_file=/data/mysql/mysql-slow.log
innodb-file-per-table=
innodb_flush_log_at_trx_commit =
log_warnings =
connect_timeout =
net_read_timeout =
performance_schema_max_table_instances =
server-id =
log-bin=master-bin
log-bin-index = master-bin.index
relay-log = relay-log
relay-log-index = relay-log.index
binlog_format = ROW
auto_increment_offset=
auto_increment_increment= [mysqld_safe]
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid

192.168.132.122的配置,自增的话从2开始,每次递增2,数值是2,4,6,8……

[mysqld]
bind-address=0.0.0.0
port=
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
skip-name-resolve
slow_query_log=on
long_query_time=
slow_query_log_file=/data/mysql/mysql-slow.log
innodb-file-per-table=
innodb_flush_log_at_trx_commit =
log_warnings =
connect_timeout =
net_read_timeout =
performance_schema_max_table_instances =
server-id =
log-bin=master-bin
log-bin-index = master-bin.index
relay-log = relay-log
relay-log-index = relay-log.index
binlog_format = ROW
auto_increment_offset=
auto_increment_increment= [mysqld_safe]
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid

防止索引一致,导致插入失败

重启数据库

先赋予对应的权限

192.168.132.121配置

mysql> grant replication slave on *.* to 'replication'@'192.168.132.122' identified by '1234567';

192.168.132.122配置

mysql> grant replication slave on *.* to 'replication'@'192.168.132.121' identified by '1234567';

启动互相同步

192.168.121操作

mysql>  change master to master_host='192.168.132.122',master_port=,master_user='replication',master_password='',master_log_file='master-bin.000001',master_log_pos=;
Query OK, rows affected, warnings (0.01 sec) mysql> start slave;
Query OK, rows affected (0.00 sec) mysql> show slave status\G;
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.132.122
Master_User: replication
Master_Port:
Connect_Retry:
Master_Log_File: master-bin.
Read_Master_Log_Pos:
Relay_Log_File: relay-log.
Relay_Log_Pos:
Relay_Master_Log_File: master-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: 141defd2-9dab-11e9-8fe3-000c2963fd11
Master_Info_File: /data/mysql/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
row in set (0.00 sec) ERROR:
No query specified mysql> show mater logs;
ERROR (): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mater logs' at line
mysql> show master logs;
+-------------------+-----------+
| Log_name | File_size |
+-------------------+-----------+
| master-bin. | |
+-------------------+-----------+

192.168.132.122操作

mysql>  change master to master_host='192.168.132.121',master_port=,master_user='replication',master_password='',master_log_file='master-bin.000001',master_log_pos=;
Query OK, rows affected, warnings (0.01 sec) mysql> start slave;
Query OK, rows affected (0.00 sec) mysql> show slave status\G;
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.132.121
Master_User: replication
Master_Port:
Connect_Retry:
Master_Log_File: master-bin.
Read_Master_Log_Pos:
Relay_Log_File: relay-log.
Relay_Log_Pos:
Relay_Master_Log_File: master-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: 77278e78-9da8-11e9-bc6c-000c2991dd19
Master_Info_File: /data/mysql/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
row in set (0.00 sec)

查看进程

mysql> mysql> show processlist;

验证双主状态

.121创建数据库
mysql> create database darren;
Query OK, row affected (0.00 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| darren |
| mysql |
| performance_schema |
| sys |
+--------------------+
.122查看并创建数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| darren |
| mysql |
| performance_schema |
| sys |
+--------------------+
mysql> create database darren1;
Query OK, row affected (0.00 sec)
.121查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| darren |
| darren1 |
| mysql |
| performance_schema |
| sys |
+--------------------+

其他指令比较

.121操作
mysql> use darren;
Database changed
mysql> create table test( id int primary key NOT NULL auto_increment, name varchar() );
Query OK, rows affected (0.01 sec) mysql> show master logs;
+-------------------+-----------+
| Log_name | File_size |
+-------------------+-----------+
| master-bin. | |
+-------------------+-----------+
row in set (0.00 sec)
.122插入一个数据
mysql> use darren;
Database changed
mysql> insert into test values (null, 'shijiange');
Query OK, row affected (0.00 sec)
.121查看
mysql> show binlog events in 'master-bin.000001'; #同步的数据没有记录到日志中来
+-------------------+-----+----------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+----------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
| master-bin. | | Format_desc | | | Server ver: 5.7.-log, Binlog ver: |
| master-bin. | | Previous_gtids | | | |
| master-bin. | | Anonymous_Gtid | | | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| master-bin. | | Query | | | create database darren |
| master-bin. | | Anonymous_Gtid | | | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| master-bin. | | Query | | | use `darren`; create table test( id int primary key NOT NULL auto_increment, name varchar() ) |
+-------------------+-----+----------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
mysql> select * from test;
+----+-----------+
| id | name |
+----+-----------+
| | shijiange | #是以2开始,每次加2
+----+-----------+
同时插入一条数据
mysql> insert into test values (null, UUID());
Query OK, row affected (0.01 sec) mysql> select * from test;
+----+--------------------------------------+
| id | name |
+----+--------------------------------------+
| | shijiange |
| | eb11d3b8-9e7d-11e9--000c2991dd19 | #以奇数开始,每次加2
+----+--------------------------------------+
mysql> insert into test values (null, UUID());
Query OK, row affected (0.01 sec) mysql> select * from test;
+----+--------------------------------------+
| id | name |
+----+--------------------------------------+
| | shijiange |
| | eb11d3b8-9e7d-11e9--000c2991dd19 |
| | f331ca26-9e7d-11e9--000c2991dd19 |
+----+--------------------------------------+
.122插入数据
mysql> insert into test values (null, UUID());
Query OK, row affected (0.01 sec) mysql> select * from test;
+----+--------------------------------------+
| id | name |
+----+--------------------------------------+
| | shijiange |
| | eb11d3b8-9e7d-11e9--000c2991dd19 |
| | f331ca26-9e7d-11e9--000c2991dd19 |
| | f8ca41f9-9e7d-11e9-ba37-000c2963fd11 |
+----+--------------------------------------+
rows in set (0.00 sec) mysql> insert into test values (null, UUID());
Query OK, row affected (0.00 sec) mysql> select * from test;
+----+--------------------------------------+
| id | name |
+----+--------------------------------------+
| | shijiange |
| | eb11d3b8-9e7d-11e9--000c2991dd19 |
| | f331ca26-9e7d-11e9--000c2991dd19 |
| | f8ca41f9-9e7d-11e9-ba37-000c2963fd11 |
| | 043f7a3f-9e7e-11e9-ba37-000c2963fd11 |
+----+--------------------------------------+
.121插入数据

双主配置完成