mysql主从
1,首先保证数据库版本一致
2,修改主数据库配置
3,同步需要备份库的数据
4,修改备份配置
5,查询主库二进制日志文件
6,备份库同步日志文件
7,查看从库状态
主数据库配置
#server-id需要保证唯一性 不可与其他从服务器相同 如果为0会拒绝所有从服务器连接
34 server-id=100
35 log_bin=master-bin
36 ##binlog的索引文件 可以不配置 默认会根据上面配置增加index后缀
37 log-bin-index=master-bin.index
38 #需要同步的数据库
39 binlog-do-db=
45 #不需要同步的数据库
46 binlog-ignore-db=mysql
47 #binlog-ignore-db=test
48 binlog-ignore-db=information_schema
49 #为每个session分配的内存,在事务过程中用来存储二进制日志的缓存(可以不配置)
50 #binlog_cache_size=1M
51 # 主从复制的格式(mixed,statement,row,默认格式是statement)
52 binlog_format=mixed
53 # 二进制日志自动删除/过期的天数。默认值为0,表示不自动删除。
54 expire_logs_days=30
同步数据
主库有数据需要先同步数据到主库(注意数据要保持一致),如果新库可不用同步
备份配置
server-id=102
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/usr/mysql/mysql/data
#socket=/var/lib/mysql/mysql.sock
port = 3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
symbolic-links=0
主库二进制日志文件
show master status;
+-------------------+----------+-----------------------------------------------------------------------------------------------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+-----------------------------------------------------------------------------------------------------+--------------------------+-------------------+
| master-bin.000004 | 1471 | das| mysql,information_schema | |
+-------------------+----------+-----------------------------------------------------------------------------------------------------+--------------------------+-------------------+
备份库同步日志文件
change master to
master_host='xxx.xxx.xxx.xxx',
master_port=xxxx,
master_user='xxxx',
master_password='xxxx',
master_log_file='xxxxxx',
master_log_pos=xxxx;
查看从库状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.79.100
Master_User: db_sync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000005
Read_Master_Log_Pos: 154
Relay_Log_File: haiyang02-relay-bin.000009
Relay_Log_Pos: 321
Relay_Master_Log_File: master-bin.000005
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: 532
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: 20f46711-b984-11ec-8c70-000c297937ef
Master_Info_File: /usr/mysql/mysql/data/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:
这两个属性为Yes 证明同步完成
Slave_IO_Running: Yes
Slave_SQL_Running: Yes