少年乘勇气,百战过乌孙。这篇文章主要讲述MySQL集群和ansible的简单介绍相关的知识,希望能为你提供帮助。
1、主从复制及主主复制的实现
1.1、mysql5.7主从复制的实现
文章图片
1.1.1、实验环境:
node | Master | Slave |
---|---|---|
System | Ubuntu18.04 | Ubuntu18.04 |
MySQL | 5.7 | 5.7 |
IP | 10.0.0.100 | 0.0.0.102 |
root@ubuntu:~# hostnamectl set-hostname master-mysql
root@master-mysql:~# apt -y install mysql-server
root@master-mysql:~# vim /etc/mysql/mysql.conf.d/mysqld.cnf
#在mysqld里面加上下面两行,定义主节点ID和开启二进制
[mysqld]
server-id= 100
log_bin= /var/log/mysql/mysql-bin.log
root@master-mysql:~# systemctl restart mysql
root@master-mysql:~# mysql -uroot -p
mysql>
show master logs;
+------------------+-----------+
| Log_name| File_size |
+------------------+-----------+
| mysql-bin.000001 |177 |
| mysql-bin.000002 |177 |
| mysql-bin.000003 |154 |
+------------------+-----------+
3 rows in set (0.00 sec)#创建复制用户并授权
mysql>
grant replication slave on *.* to repluser@10.0.0.% identified by test123;
#查看并验证账号信息
mysql>
SELECT DISTINCT CONCAT(User: ,user,@,host,;
) AS userinfo FROM mysql.user;
+---------------------------------------+
| userinfo|
+---------------------------------------+
| User: test@%;
|
| User: repluser@10.0.0.%;
|
| User: debian-sys-maint@localhost;
|
| User: mysql.session@localhost;
|
| User: mysql.sys@localhost;
|
| User: root@localhost;
|
+---------------------------------------+
6 rows in set (0.00 sec)
#查看现有的数据库信息
mysql>
show databases;
+--------------------+
| Database|
+--------------------+
| information_schema |
| mysql|
| performance_schema |
| sys|
+--------------------+
4 rows in set (0.00 sec)
1.1.3、从节点数据库的配置
[root@ubuntu ~]#hostnamectl set-hostname slave-mysql
[root@slave-mysql ~]#apt -y install mysql-server
[root@slave-mysql ~]#vim /etc/mysql/mysql.conf.d/mysqld.cnf
#在mysqld里面加上下面一行,定义主节点ID
server-id= 102
[root@slave-mysql ~]#systemctl restart mysql
[root@slave-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.37-0ubuntu0.18.04.1-log (Ubuntu)Copyright (c) 2000, 2022, 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>
help change master to#查看一下主从复制的格式的帮助
Name: CHANGE MASTER TO
Description:
Syntax:
CHANGE MASTER TO option [, option] ... [ channel_option ]option:
MASTER_BIND = interface_name
| MASTER_HOST = host_name
| MASTER_USER = user_name
| MASTER_PASSWORD = password
| MASTER_PORT = port_num
| MASTER_CONNECT_RETRY = interval
| MASTER_RETRY_COUNT = count
| MASTER_DELAY = interval
| MASTER_HEARTBEAT_PERIOD = interval
| MASTER_LOG_FILE = source_log_name
| MASTER_LOG_POS = source_log_pos
| MASTER_AUTO_POSITION = 0|1
| RELAY_LOG_FILE = relay_log_name
| RELAY_LOG_POS = relay_log_pos
| MASTER_SSL = 0|1
| MASTER_SSL_CA = ca_file_name
| MASTER_SSL_CAPATH = ca_directory_name
| MASTER_SSL_CERT = cert_file_name
| MASTER_SSL_CRL = crl_file_name
| MASTER_SSL_CRLPATH = crl_directory_name
| MASTER_SSL_KEY = key_file_name
| MASTER_SSL_CIPHER = cipher_list
| MASTER_SSL_VERIFY_SERVER_CERT = 0|1
| MASTER_TLS_VERSION = protocol_list
| IGNORE_SERVER_IDS = (server_id_list)channel_option:
FOR CHANNEL channelserver_id_list:
[server_id [, server_id] ... ]CHANGE MASTER TO changes the parameters that the replica uses for
connecting to the replication source server, for reading the sources
binary log, and reading the replicas relay log. It also updates the
contents of the replication metadata repositories (see
https://dev.mysql.com/doc/refman/5.7/en/replica-logs.html). CHANGE
MASTER TO requires the SUPER privilege.Prior to MySQL 5.7.4, the replication threads must be stopped, using
STOP SLAVE if necessary, before issuing this statement. In MySQL 5.7.4
and later, you can issue CHANGE MASTER TO statements on a running
replica without doing this, depending on the states of the replication
SQL thread and replication I/O thread. The rules governing such use are
provided later in this section.When using a multithreaded replica (in other words
slave_parallel_workers is greater than 0), stopping the replica can
cause "gaps" in the sequence of transactions that have been executed
from the relay log, regardless of whether the replica was stopped
intentionally or otherwise. When such gaps exist, issuing CHANGE MASTER
TO fails. The solution in this situation is to issue START SLAVE UNTIL
SQL_AFTER_MTS_GAPS which ensures that the gaps are closed.The optional FOR CHANNEL channel clause enables you to name which
replication channel the statement applies to. Providing a FOR CHANNEL
channel clause applies the CHANGE MASTER TO statement to a specific
replication channel, and is used to add a new channel or modify an
existing channel. For example, to add a new channel called channel2:CHANGE MASTER TO MASTER_HOST=host1, MASTER_PORT=3002 FOR CHANNEL channel2If no clause is named and no extra channels exist, the statement
applies to the default channel.When using multiple replication channels, if a CHANGE MASTER TO
statement does not name a channel using a FOR CHANNEL channel clause,
an error occurs. See
https://dev.mysql.com/doc/refman/5.7/en/replication-channels.html for
more information.Options not specified retain their value, except as indicated in the
following discussion. Thus, in most cases, there is no need to specify
options that do not change. For example, if the password to connect to
your replication source server has changed, issue this statement to
tell the replica about the new password:CHANGE MASTER TO MASTER_PASSWORD=new3cret;
MASTER_HOST, MASTER_USER, MASTER_PASSWORD, and MASTER_PORT provide
information to the replica about how to connect to its replication
source server:o MASTER_HOST and MASTER_PORT are the host name (or IP address) of the
master host and its TCP/IP port.*Note*:Replication cannot use Unix socket files. You must be able to connect
to the replication source server using TCP/IP.If you specify the MASTER_HOST or MASTER_PORT option, the replica
assumes that the source is different from before (even if the option
value is the same as its current value.) In this case, the old values
for the sources binary log file name and position are considered no
longer applicable, so if you do not specify MASTER_LOG_FILE and
MASTER_LOG_POS in the statement, MASTER_LOG_FILE= and
MASTER_LOG_POS=4 are silently appended to it.Setting MASTER_HOST= (that is, setting its value explicitly to an
empty string) is not the same as not setting MASTER_HOST at all.
Beginning with MySQL 5.5, trying to set MASTER_HOST to an empty
string fails with an error. Previously, setting MASTER_HOST to an
empty string caused START SLAVE subsequently to fail. (Bug #28796)Values used for MASTER_HOST and other CHANGE MASTER TO options are
checked for linefeed (\\n or 0x0A) characters;
the presence of such
characters in these values causes the statement to fail with
ER_MASTER_INFO
(https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference
.html#error_er_master_info). (Bug #11758581, Bug #50801)o MASTER_USER and MASTER_PASSWORD are the user name and password of the
account to use for connecting to the source. If you specify
MASTER_PASSWORD, MASTER_USER is also required. The password used for
a replication user account in a CHANGE MASTER TO statement is limited
to 32 characters in length;
prior to MySQL 5.7.5, if the password was
longer, the statement succeeded, but any excess characters were
silently truncated. In MySQL 5.7.5 and later, trying to use a
password of more than 32 characters causes CHANGE MASTER TO to fail.
(Bug #11752299, Bug #43439)It is possible to set an empty user name by specifying
MASTER_USER=, but the replication channel cannot be started with an
empty user name. Only set an empty MASTER_USER user name if you need
to clear previously used credentials from the replicas repositories
for security purposes, and do not attempt to use the channel
afterwards.The text of a running CHANGE MASTER TO statement, including values
for MASTER_USER and MASTER_PASSWORD, can be seen in the output of a
concurrent SHOW PROCESSLIST statement. (The complete text of a START
SLAVE statement is also visible to SHOW PROCESSLIST.)The MASTER_SSL_xxx options and the MASTER_TLS_VERSION option specify
how the replica uses encryption and ciphers to secure the replication
connection. These options can be changed even on replicas that are
compiled without SSL support. They are saved to the source metadata
repository, but are ignored if the replica does not have SSL support
enabled. The MASTER_SSL_xxx and MASTER_TLS_VERSION options perform the
same functions as the --ssl-xxx and --tls-version client options
described in
https://dev.mysql.com/doc/refman/5.7/en/connection-options.html#encrypt
ed-connection-options. The correspondence between the two sets of
options, and the use of the MASTER_SSL_xxx and MASTER_TLS_VERSION
options to set up a secure connection, is explained in
https://dev.mysql.com/doc/refman/5.7/en/replication-solutions-encrypted
-connections.html.As of MySQL 5.7.3, setting MASTER_SSL=1 means that the replicas
connection to the source must use SSL, or the connection attempt fails.
Before 5.7.3, the use of an SSL connection by the replica was not
enforced with MASTER_SSL=1. This is analogous to the client-side
meaning of the --ssl command-line option;
see
https://dev.mysql.com/doc/refman/5.7/en/connection-options.html#encrypt
ed-connection-options.The MASTER_HEARTBEAT_PERIOD, MASTER_CONNECT_RETRY, and
MASTER_RETRY_COUNT options control how the replica recognizes that the
connection to the source has been lost and makes attempts to reconnect.o The slave_net_timeout system variable specifies the number of seconds
that the replica waits for either more data or a heartbeat signal
from the source, before the replica considers the connection broken,
aborts the read, and tries to reconnect. The default value is 60
seconds (one minute). Prior to MySQL 5.7.7, the default was 3600
seconds (one hour).o The heartbeat interval, which stops the connection timeout occurring
in the absence of data if the connection is still good, is controlled
by the MASTER_HEARTBEAT_PERIOD option. A heartbeat signal is sent to
the replica after that number of seconds, and the waiting period is
reset whenever the sources binary log is updated with an event.
Heartbeats are therefore sent by the source only if there are no
unsent events in the binary log file for a period longer than this.
The heartbeat interval interval is a decimal value having the range 0
to 4294967 seconds and a resolution in milliseconds;
the smallest
nonzero value is 0.001. Setting interval to 0 disables heartbeats
altogether. The heartbeat interval defaults to half the value of the
slave_net_timeout system variable. It is recorded in the source
metadata repository and shown in the
replication_connection_configuration Performance Schema table.
Issuing RESET SLAVE resets the heartbeat interval to the default
value.o Prior to MySQL 5.7.4, not including MASTER_HEARTBEAT_PERIOD caused
CHANGE MASTER TO to reset the heartbeat interval to the default (half
the value of the slave_net_timeout system variable), and
Slave_received_heartbeats to 0. The heartbeat interval is now not
reset except by RESET SLAVE. (Bug #18185490)o Note that a change to the value or default setting of
slave_net_timeout does not automatically change the heartbeat
interval, whether that has been set explicitly or is using a
previously calculated default. A warning is issued if you set
@@GLOBAL.slave_net_timeout to a value less than that of the current
heartbeat interval. If slave_net_timeout is changed, you must also
issue CHANGE MASTER TO to adjust the heartbeat interval to an
appropriate value so that the heartbeat signal occurs before the
connection timeout. If you do not do this, the heartbeat signal has
no effect, and if no data is received from the source, the replica
can make repeated reconnection attempts, creating zombie dump
threads.o If the replica does need to reconnect, the first retry occurs
immediately after the timeout. MASTER_CONNECT_RETRY specifies the
interval between reconnection attempts, and MASTER_RETRY_COUNT limits
the number of reconnection attempts. If both the default settings are
used, the replica waits 60 seconds between reconnection attempts
(MASTER_CONNECT_RETRY=60), and keeps attempting to reconnect at this
rate for 60 days (MASTER_RETRY_COUNT=86400). A setting of 0 for
MASTER_RETRY_COUNT means that there is no limit on the number of
reconnection attempts, so the replica keeps trying to reconnect
indefinitely. These values are recorded in the source metadata
repository and shown in the replication_connection_configuration
Performance Schema table. MASTER_RETRY_COUNT supersedes the
--master-retry-count server startup option.MASTER_DELAY specifies how many seconds behind the source the replica
must lag. An event received from the source is not executed until at
least interval seconds later than its execution on the source. The
default is 0. An error occurs if interval is not a nonnegative integer
in the range from 0 to 231?1. For more information, see
https://dev.mysql.com/doc/refman/5.7/en/replication-delayed.html.From MySQL 5.7, a CHANGE MASTER TO statement employing the MASTER_DELAY
option can be executed on a running replica when the replication SQL
thread is stopped.MASTER_BIND is for use on replicas having multiple network interfaces,
and determines which of the replicas network interfaces is chosen for
connecting to the source.The address configured with this option, if any, can be seen in the
Master_Bind column of the output from SHOW SLAVE STATUS. If you are
using a table for the source metadata repository (server started with
master_info_repository=TABLE), the value can also be seen as the
Master_bind column of the mysql.slave_master_info table.The ability to bind a replica to a specific network interface is also
supported by NDB Cluster.MASTER_LOG_FILE and MASTER_LOG_POS are the coordinates at which the
replication I/O thread should begin reading from the source the next
time the thread starts. RELAY_LOG_FILE and RELAY_LOG_POS are the
coordinates at which the replication SQL thread should begin reading
from the relay log the next time the thread starts. If you specify any
of these options, you cannot specify MASTER_AUTO_POSITION = 1
(described later in this section). If neither of MASTER_LOG_FILE or
MASTER_LOG_POS is specified, the replica uses the last coordinates of
the replication SQL thread before CHANGE MASTER TO was issued. This
ensures that there is no discontinuity in replication, even if the
replication SQL thread was late compared to the replication I/O thread,
when you merely want to change, say, the password to use.From MySQL 5.7, a CHANGE MASTER TO statement employing RELAY_LOG_FILE,
RELAY_LOG_POS, or both options can be executed on a running replica
when the replication SQL thread is stopped. Prior to MySQL 5.7.4,
CHANGE MASTER TO deletes all relay log files and starts a new one,
unless you specify RELAY_LOG_FILE or RELAY_LOG_POS. In that case, relay
log files are kept;
the relay_log_purge global variable is set silently
to 0. In MySQL 5.7.4 and later, relay logs are preserved if at least
one of the replication SQL thread and the replication I/O thread is
running. If both threads are stopped, all relay log files are deleted
unless at least one of RELAY_LOG_FILE or RELAY_LOG_POS is specified.
For the Group Replication applier channel (group_replication_applier),
which only has an SQL thread and no I/O thread, this is the case if the
SQL thread is stopped, but with that channel you cannot use the
RELAY_LOG_FILE and RELAY_LOG_POS options.RELAY_LOG_FILE can use either an absolute or relative path, and uses
the same base name as MASTER_LOG_FILE. (Bug #12190)When MASTER_AUTO_POSITION = 1 is used with CHANGE MASTER TO, the
replica attempts to connect to the source using the auto-positioning
feature of GTID-based replication, rather than a binary log file based
position. From MySQL 5.7, this option can be employed by CHANGE MASTER
TO only if both the replication SQL thread and the replication I/O
thread are stopped. Both the replica and the source must have GTIDs
enabled (GTID_MODE=ON, ON_PERMISSIVE, or OFF_PERMISSIVE on the replica,
and GTID_MODE=ON on the source). MASTER_LOG_FILE, MASTER_LOG_POS,
RELAY_LOG_FILE, and RELAY_LOG_POS cannot be specified together with
MASTER_AUTO_POSITION = 1. If multi-source replication is enabled on the
replica, you need to set the MASTER_AUTO_POSITION = 1 option for each
applicable replication channel.With MASTER_AUTO_POSITION = 1 set, in the initial connection handshake,
the replica sends a GTID set containing the transactions that it has
already received, committed, or both. The source responds by sending
all transactions recorded in its binary log whose GTID is not included
in the GTID set sent by the replica. This exchange ensures that the
source only sends the transactions with a GTID that the replica has not
already recorded or committed. If the replica receives transactions
from more than one source, as in the case of a diamond topology, the
auto-skip function ensures that the transactions are not applied twice.
For details of how the GTID set sent by the replica is computed, see
https://dev.mysql.com/doc/refman/5.7/en/replication-gtids-auto-position
ing.html.If any of the transactions that should be sent by the source have been
purged from the sources binary log, or added to the set of GTIDs in
the gtid_purged system variable by another method, the source sends the
error ER_MASTER_HAS_PURGED_REQUIRED_GTIDS
(https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.h
tml#error_er_master_has_purged_required_gtids) to the replica, and
replication does not start. Also, if during the exchange of
transactions it is found that the replica has recorded or committed
transactions with the sources UUID in the GTID, but the source itself
has not committed them, the source sends the error
ER_SLAVE_HAS_MORE_GTIDS_THAN_MASTER
(https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.h
tml#error_er_slave_has_more_gtids_than_master) to the replica and
replication does not start. For information on how to handle these
situations, see
https://dev.mysql.com/doc/refman/5.7/en/replication-gtids-auto-position
ing.html.IGNORE_SERVER_IDS takes a comma-separated list of 0 or more server IDs.
Events originating from the corresponding servers are ignored, with the
exception of log rotation and deletion events, which are still recorded
in the relay log.In circular replication, the originating server normally acts as the
terminator of its own events, so that they are not applied more than
once. Thus, this option is useful in circular replication when one of
the servers in the circle is removed. Suppose that you have a circular
replication setup with 4 servers, having server IDs 1, 2, 3, and 4, and
server 3 fails. When bridging the gap by starting replication from
server 2 to server 4, you can include IGNORE_SERVER_IDS = (3) in the
CHANGE MASTER TO statement that you issue on server 4 to tell it to use
server 2 as its source instead of server 3. Doing so causes it to
ignore and not to propagate any statements that originated with the
server that is no longer in use.If a CHANGE MASTER TO statement is issued without any IGNORE_SERVER_IDS
option, any existing list is preserved. To clear the list of ignored
servers, it is necessary to use the option with an empty list:CHANGE MASTER TO IGNORE_SERVER_IDS = ();
Prior to MySQL 5.7.5, RESET SLAVE ALL has no effect on the server ID
list. In MySQL 5.7.5 and later, RESET SLAVE ALL clears
IGNORE_SERVER_IDS. (Bug #18816897)If IGNORE_SERVER_IDS contains the servers own ID and the server was
started with the --replicate-same-server-id option enabled, an error
results.The source metadata repository and the output of SHOW SLAVE STATUS
provide the list of servers that are currently ignored. For more
information, see
https://dev.mysql.com/doc/refman/5.7/en/replica-logs-status.html, and
[HELP SHOW SLAVE STATUS].Invoking CHANGE MASTER TO causes the previous values for MASTER_HOST,
MASTER_PORT, MASTER_LOG_FILE, and MASTER_LOG_POS to be written to the
error log, along with other information about the replicas state prior
to execution.CHANGE MASTER TO causes an implicit commit of an ongoing transaction.
See https://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html.In MySQL 5.7.4 and later, the strict requirement to execute STOP SLAVE
prior to issuing any CHANGE MASTER TO statement (and START SLAVE
afterward) is removed. Instead of depending on whether the replica is
stopped, the behavior of CHANGE MASTER TO depends (in MySQL 5.7.4 and
later) on the states of the replication SQL thread and the replication
I/O thread;
which of these threads is stopped or running now determines
the options that can or cannot be used with a CHANGE MASTER TO
statement at a given point in time. The rules for making this
determination are listed here:o If the SQL thread is stopped, you can execute CHANGE MASTER TO using
any combination that is otherwise allowed of RELAY_LOG_FILE,
RELAY_LOG_POS, and MASTER_DELAY options, even if the replication I/O
thread is running. No other options may be used with this statement
when the I/O thread is running.o If the I/O thread is stopped, you can execute CHANGE MASTER TO using
any of the options for this statement (in any allowed combination)
except RELAY_LOG_FILE, RELAY_LOG_POS, MASTER_DELAY, or
MASTER_AUTO_POSITION = 1 even when the SQL thread is running.o Both the SQL thread and the I/O thread must be stopped before issuing
a CHANGE MASTER TO statement that employs MASTER_AUTO_POSITION = 1.You can check the current state of the replication SQL thread and the
replication I/O thread using SHOW SLAVE STATUS. Note that the Group
Replication applier channel (group_replication_applier) has no I/O
thread, only an SQL thread.For more information, see
https://dev.mysql.com/doc/refman/5.7/en/replication-solutions-switch.ht
ml.If you are using statement-based replication and temporary tables, it
is possible for a CHANGE MASTER TO statement following a STOP SLAVE
statement to leave behind temporary tables on the replica. From MySQL
5.7, a warning (ER_WARN_OPEN_TEMP_TABLES_MUST_BE_ZERO
(https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.h
tml#error_er_warn_open_temp_tables_must_be_zero)) is issued whenever
this occurs. You can avoid this in such cases by making sure that the
value of the Slave_open_temp_tables system status variable is equal to
0 prior to executing such a CHANGE MASTER TO statement.CHANGE MASTER TO is useful for setting up a replica when you have the
snapshot of the replication source server and have recorded the
sources binary log coordinates corresponding to the time of the
snapshot. After loading the snapshot into the replica to synchronize it
with the source, you can run CHANGE MASTER TO
MASTER_LOG_FILE=log_name, MASTER_LOG_POS=log_pos on the replica to
specify the coordinates at which the replica should begin reading the
sources binary log.The following example changes the replication source server the replica
uses and establishes the sources binary log coordinates from which the
replica begins reading. This is used when you want to set up the
replica to replicate the source:CHANGE MASTER TO
MASTER_HOST=source2.example.com,
MASTER_USER=replication,
MASTER_PASSWORD=password,
MASTER_PORT=3306,
MASTER_LOG_FILE=source2-bin.001,
MASTER_LOG_POS=4,
MASTER_CONNECT_RETRY=10;
The next example shows an operation that is less frequently employed.
It is used when the replica has relay log files that you want it to
execute again for some reason. To do this, the source need not be
reachable. You need only use CHANGE MASTER TO and start the SQL thread
(START SLAVE SQL_THREAD):CHANGE MASTER TO
RELAY_LOG_FILE=replica-relay-bin.006,
RELAY_LOG_POS=4025;
URL: https://dev.mysql.com/doc/refman/5.7/en/change-master-to.htmlmysql>
CHANGE MASTER TO MASTER_HOST=10.0.0.100,
->
MASTER_USER=repluser,MASTER_PASSWORD=test123,MASTER_PORT=3306,
->
MASTER_LOG_FILE=mysql-bin.000003,MASTER_LOG_POS=154;
mysql>
start slave;
Query OK, 0 rows affected (0.00 sec)
1.1.4、验证数据库的主从复制
#在从数据库查看主从复制的状态
mysql>
show slave status\\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.100
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 448
Relay_Log_File: slave-mysql-relay-bin.000002
Relay_Log_Pos: 614
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes#这行和下面一行都是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: 448
Relay_Log_Space: 827
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: 100
Master_UUID: 5dbf2da8-9d23-11ec-90d9-000c295e72f9
Master_Info_File: /var/lib/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)#在主数据库内创建新数据库testdb,在从数据库验证正确复制
mysql>
create database testdb;
Query OK, 1 row affected (0.00 sec)mysql>
show databases;
+--------------------+
| Database|
+--------------------+
| information_schema |
| mysql|
| performance_schema |
| sys|
| testdb|
+--------------------+
5 rows in set (0.01 sec)#在从数据库中查看是否复制成功
mysql>
show databases;
+--------------------+
| Database|
+--------------------+
| information_schema |
| mysql|
| performance_schema |
| sys|
| testdb|
+--------------------+
5 rows in set (0.00 sec)#从上述查看结果MySQL5.7主从复制的配置已经成功了
1.2、MySQL5.7主主复制的实现【MySQL集群和ansible的简单介绍】
文章图片
1.2.1、实验环境:
node | Master-1 | Master-2 |
---|---|---|
System | Ubuntu18.04 | Ubuntu18.04 |
MySQL | 5.7 | 5.7 |
IP | 10.0.0.100 | 0.0.0.102 |
#首先安装数据库服务
root@ubuntu:~# hostnamectl set-hostname master1-mysql
root@master1-mysql:~# apt -y install mysql-server
root@master1-mysql:~# vim /etc/mysql/mysql.conf.d/mysqld.cnf
#在mysqld里面加上下面四行,定义节点ID、开启二进制、开始点和增长幅度
[mysqld]
server-id= 100
log_bin= /var/log/mysql/mysql-bin.log
auto_increment_offset=1
auto_increment_increment=2
#编辑完配置文件后,重启一下服务
root@master1-mysql:~# systemctl restart mysql
# 查看二进制日志信息并配置数据库复制账号
root@master1-mysql:~# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.Commands end with ;
or \\g.
Your MySQL connection id is 3
Server version: 5.7.37-0ubuntu0.18.04.1-log (Ubuntu)Copyright (c) 2000, 2022, 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 logs;
+------------------+-----------+
| Log_name| File_size |
+------------------+-----------+
| mysql-bin.000001 |177 |
| mysql-bin.000002 |154 |
+------------------+-----------+
2 rows in set (0.00 sec)mysql>
grant replication slave on *.* to repluser@10.0.0.% identified by test123;
Query OK, 0 rows affected, 1 warning (0.00 sec)
1.2.3、配置数据库主节点2
root@ubuntu:~# hostnamectl set-hostname master2-mysql
root@master2-mysql:~# apt -y install mysql-server
root@master2-mysql:~# vim /etc/mysql/mysql.conf.d/mysqld.cnf
#在mysqld里面加上下面四行,定义节点ID、开启二进制、开始点和增长幅度
[mysqld]
server-id= 102
log_bin= /var/log/mysql/mysql-bin.log
auto_increment_offset=2
auto_increment_increment=2
#编辑完配置文件后,重启一下服务
root@master2-mysql:~# systemctl restart mysql
#配置数据库复制
[root@master2-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.37-0ubuntu0.18.04.1-log (Ubuntu)Copyright (c) 2000, 2022, 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=10.0.0.100,
->
MASTER_USER=repluser,
->
MASTER_PASSWORD=test123,
->
MASTER_PORT=3306,
->
MASTER_LOG_FILE=mysql-bin.000002,
->
MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)mysql>
start slave;
Query OK, 0 rows affected (0.00 sec)#查看并记录二进制日志信息,将在回到数据库主节点1再次配置需要用到 mysql-bin.000002和154这两个值
mysql>
show master logs;
+------------------+-----------+
| Log_name| File_size |
+------------------+-----------+
| mysql-bin.000001 |177 |
| mysql-bin.000002 |154 |
+------------------+-----------+
2 rows in set (0.00 sec)#查看master1到master2的复制信息
mysql>
show slave status\\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.100
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 453
Relay_Log_File: master2-mysql-relay-bin.000002
Relay_Log_Pos: 619
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: 453
Relay_Log_Space: 834
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: 100
Master_UUID: 5dbf2da8-9d23-11ec-90d9-000c295e72f9
Master_Info_File: /var/lib/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)
1.2.4、再次配置数据库主节点1
mysql>
CHANGE MASTER TO
->
MASTER_HOST=10.0.0.102,
->
MASTER_USER=repluser,
->
MASTER_PASSWORD=test123,
->
MASTER_PORT=3306,
->
MASTER_LOG_FILE=mysql-bin.000002,
->
MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.03 sec)mysql>
start slave;
Query OK, 0 rows affected (0.00 sec)#查看master2到master1的复制信息
mysql>
show slave status\\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.102
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: master1-mysql-relay-bin.000002
Relay_Log_Pos: 320
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: 535
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: 102
Master_UUID: 06f9023c-a13c-11ec-b168-000c29e8994d
Master_Info_File: /var/lib/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)#到此MySQL5.7的主主复制基本上就配置完成了
1.2.5、验证MySQL5.7的主主复制
#登入master1节点(10.0.0.100)
root@master1-mysql:~# 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.37-0ubuntu0.18.04.1-log (Ubuntu)Copyright (c) 2000, 2022, 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.#在master1节点上创建一个db1数据库
mysql>
create database db1;
Query OK, 1 row affected (0.01 sec)#在master1节点上的db1数据库中创建一个表
mysql>
use db1;
Database changed
mysql>
create table t1(id int auto_increment primary key,name char(10));
Query OK, 0 rows affected (0.01 sec)#在刚刚创建的表中添加一行数据
mysql>
insert t1 (name) values(user1);
Query OK, 1 row affected (0.01 sec)#在切换到master2节点上,也在db1中t1表中添加数据
[root@master2-mysql ~]#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.37-0ubuntu0.18.04.1-log (Ubuntu)Copyright (c) 2000, 2022, 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>
use db1
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql>
insert t1 (name) values(user2);
Query OK, 1 row affected (0.00 sec)# 在通过xshell的SSH终端软件,同时向数据库master1和数据库master2,同时发送了下面命令
mysql>
insert t1 (name) values(userX);
Query OK, 1 row affected (0.002 sec)# 通过上面的操作,可以查看到如下的t1表内的键值信息,可以看到无论分别向数据库master1或数据库master2,还是同时向数据库master1和2发送向表内插入键值的命令,均会被执行,可以看到有两个ID的都为userX的键值对。
mysql>
select * from t1;
+----+-------+
| id | name|
+----+-------+
|1 | user1 |
|2 | user2 |
|3 | userX |
|4 | userX |
+----+-------+
4 rows in set (0.001 sec)#在master1上在创建一个数据库db2
mysql>
create database db2;
Query OK, 1 row affected (0.01 sec)#在master1上在查看以下数据库
mysql>
show databases;
+--------------------+
| Database|
+--------------------+
| information_schema |
| db1|
| db2|
| mysql|
| performance_schema |
| sys|
+--------------------+
6 rows in set (0.00 sec)#登入master2节点(10.0.0.102) 查看到的表和数据库信息
mysql>
select * from t1;
+----+-------+
| id | name|
+----+-------+
|1 | user1 |
|2 | user2 |
|3 | userX |
|4 | userX |
+----+-------+
4 rows in set (0.001 sec)
#在master1上创建了了数据库db2,在master2上在创建会保错,说创建的数据库一存在。
注意:两个节点同时创建数据库,发生复制冲突
mysql>
create database db2;
ERROR 1007 (HY000): Cant create database db2;
database exists
mysql>
show databases;
+--------------------+
| Database|
+--------------------+
| information_schema |
| db1|
| db2|
| mysql|
| performance_schema |
| sys|
+--------------------+
6 rows in set (0.00 sec)#到此MySQL5.7的主主复制的配置和验证基本上完成了。
2、xtrabackup实现全量+增量+binlog恢复库 3、MyCAT实现MySQL读写分离 4、ansible常用模块介绍
推荐阅读
- php连接redis,报错ERROR:同步数据异常
- 武警部队智能枪支弹药管理系统-公安智能枪弹管理系统软件
- 浅析ZABBIX和WGCLOUD各自的特点
- 军事训练信息化管理系统-训练成绩管理系统软件
- LAMP架构#yyds干货盘点#
- SpringSecurity整合SSO单点登录
- 深入分析Linux内核源码oss.org.cn/kernel-book/
- Linux 下QT安装教程
- Linux 输入子系统原理理解(原创)