1.主从配置概念
MySQL数据库主从同步主要是基于二进制日志机制,主服务器使用二进制日志来记录数据库的变动情况,从服务器通过读取和执行该日志文件来保持和主服务器的数据一致。主从服务器上都需要开启binlog才能进行主从。
主服务器配置(master)
主服务器my.cnf 文件修改
开启binlog日志功能,配置读写权限(read-only),并配置需要复制的数据库名(或表名)和不需要进行备份(ignore)表。
# For advice on how to change settings please see# # http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html[client]socket=/lizijie/db/mysql.sockdefault-character-set=UTF8[mysqld]# 端口号port=3306# server-id 需要唯一,与从服务器不可重复server-id=1# 默认时区default-time_zone='+8:00'# slave-skip-errors在从库配置即可slave-skip-errors=1032,1049,1053,1062,1146# binlog日志开启log-bin=binlogbinlog_format=rowbinlog_expire_logs_seconds=604800log_bin_trust_function_creators=1# 需要备份的表binlog-do-db=check# 不需要备份的表binlog-ignore-db=mysqlbinlog-ignore-db=information_schemabinlog-ignore-db=performance_schemabinlog-ignore-db=sysgtid_executed_compression_period=1000gtid_mode=onenforce_gtid_consistency=oncharacter-set-server=UTF8collation-server=utf8_unicode_ciskip-host-cacheskip-name-resolveinnodb_buffer_pool_size=8Gmax_connections=2000wait_timeout=28800lower_case_table_names=1transaction-isolation=READ-COMMITTEDslow_query_log=onlong_query_time=0.1binlog_cache_size=2Minnodb_open_files=3000max_allowed_packet=1Gopen_files_limit=65535performance_schema=OFFtmp_table_size=256Mmax_heap_table_size=256Mmax_write_lock_count=102400thread_cache_size=100sort_buffer_size=4Mkey_buffer_size=32Mread_rnd_buffer_size = 4Mread_buffer_size=4Mjoin_buffer_size=128Minnodb_flush_log_at_trx_commit=2innodb_log_file_size=1Ginnodb_buffer_pool_instances=8innodb_io_capacity=2000innodb_io_capacity_max=4000# 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# These are commonly set, remove the # and set as required.# basedir = .....datadir = lizijie/dbsocket = lizijie/db/mysql.socklog-error = lizijie/log/mysqld.logpid-file = var/run/mysqld/mysqld.pid# port = .....# server_id = .....# socket = .....# 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## Remove leading # to revert to previous value for default_authentication_plugin,# # this will increase compatibility with older clients. For background, see:# # https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugindefault-authentication-plugin=mysql_native_passwordsql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
Master配置用于复制的用户(IP为SlaveIP地址)
-- mysql5.7之前版本grant replication slave on *.* to 'backup'@'192.168.30.132'identified by '123456';
-- mysql5.7之后版本create user 'backup'@'192.168.30.130' identified with mysql_native_password by '123456';grant replication slave on *.* to 'backup'@'192.168.30.132';
重新启动MySQL
/sbin/service mysql restart;
获取Master同步状态
# 锁定数据库mysql> flush tables with read lock# 新打开一个终端,导出数据mysqldump --master-data -uroot -p test> test.sql# 查看Master状态,记录Position 和 File值 这个值需要在从服务器上进行配置show master status\G;# 解锁数据库mysql> unlock tables;
从服务器配置
从服务器my.cnf 文件修改
# For advice on how to change settings please see# # http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html[client]socket=/lizijie/db/mysql.sockdefault-character-set=UTF8[mysqld]port=3306server-id=2default-time_zone='+8:00'#slave-skip-errors在从库配置即可slave-skip-errors=1032,1049,1053,1062,1146,22log-bin=binlogbinlog_format=rowread_only=1super_read_only=1binlog-do-db=checkbinlog-ignore-db=mysqlbinlog-ignore-db=information_schemabinlog-ignore-db=performance_schemabinlog-ignore-db=sysreplicate-do-db=checkreplicate-ignore-db=mysqlreplicate-ignore-db=information_schemareplicate-ignore-db=performance_schemareplicate-ignore-db=sysskip_slave_start=1gtid_executed_compression_period=1000gtid_mode=onenforce_gtid_consistency=on#中继日志的名字。复制线程先把远程的变化拷贝到这个中继日志中再执行。relay_log=mysqld-relay-bin#意思是,中继日志执行之后,这些变化是否需要计入自己的binarylog。#当你的 B服务器需要作为另外一个服务器的主服务器的时候需要打开。#就是双主互相备 份,或者多主循环备份。如果需要,则打开。#log-slave-updates=ONcharacter-set-server=UTF8skip-host-cacheskip-name-resolveinnodb_buffer_pool_size=8Gmax_connections=2000wait_timeout=28800lower_case_table_names=1transaction-isolation=READ-COMMITTEDslow_query_log=onlong_query_time=0.1binlog_cache_size=2Minnodb_open_files=3000max_allowed_packet=1Gopen_files_limit=65535performance_schema=OFFtmp_table_size=256Mmax_heap_table_size=256Mmax_write_lock_count=102400thread_cache_size=100sort_buffer_size=4Mkey_buffer_size=32Mread_rnd_buffer_size = 4Mread_buffer_size=4Mjoin_buffer_size=128Minnodb_flush_log_at_trx_commit=2innodb_log_file_size=1Ginnodb_buffer_pool_instances=8innodb_io_capacity=2000innodb_io_capacity_max=4000# 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# These are commonly set, remove the # and set as required.# basedir = .....datadir = lizijie/dbsocket = /lizijie/db/mysql.socklog-error = /lizijie/log/mysqld.logpid-file = /var/run/mysqld/mysqld.pid# port = .....# server_id = .....# socket = .....# 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## Remove leading # to revert to previous value for default_authentication_plugin,# # this will increase compatibility with older clients. For background, see:# # https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugindefault-authentication-plugin=mysql_native_passwordsql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
/sbin/service mysql restart
mysql> create database test default charset utf8;mysql -uroot -p test< test.sql
change master to master_host='192.168.30.132',master_user='backup',master_password='egova',master_connect_retry=60, master_log_file='binlog.000010',master_log_pos=155;
重启MySQL查看状态是否已经开启

如果状态为Yes,则说明主从配置成功。数据已经开始同步。
文章转载自栗子Jie,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




