mysql主从同步

1 mysql主从

1.1 主节点

1.1.1 配置

[client]
port = 3306
socket = /data/mysql_3306/tmp/mysql.sock
#default-character-set = utf8
default-character-set = utf8mb4

[mysqld]
port            = 3306
socket          = /data/mysql_3306/tmp/mysql.sock
#log            = /data/mysql_3306/logs/mysql.log

slow_query_log = 1
slow_query_log_file             = /data/mysql_3306/logs/sql_query_slow.log


basedir=/data/mysql_3306
datadir=/data/mysqldata_3306 
skip-external-locking
key_buffer_size = 256M
table_open_cache = 4096 
sort_buffer_size = 2M
read_buffer_size = 1M
read_rnd_buffer_size = 64M
net_buffer_length = 2K
thread_stack = 256K
query_cache_type=2
query_cache_size = 1024M
query_cache_limit=2M
lower_case_table_names=1
#character_set_server =  utf8
log_timestamps = SYSTEM
log_bin_trust_function_creators=1  
############################################
character-set-client-handshake = FALSE 
character-set-server = utf8mb4 
collation-server = utf8mb4_bin
init_connect='SET NAMES utf8mb4'
############################################
max_prepared_stmt_count=100000
max_connections=10000
bulk_insert_buffer_size = 128M
binlog_cache_size = 32M
tmp_table_size = 256M
max_heap_table_size =256M
thread_cache_size = 32
# 每个服务器必须有一个唯一的ID
server-id = 1
# 开启二进制日志,指定日志文件名前缀
log-bin=mysql-bin
# 设置binlog格式为行级别
binlog_format = ROW
binlog_rows_query_log_events = on
binlog_row_image = full
innodb_support_xa=1
sync_binlog=100
slow_query_log
long_query_time = 1
#binlog-ignore-db = cattle
default-storage-engine=InnoDB
innodb_buffer_pool_size = 24096M 
innodb_log_file_size = 1024M
innodb_log_buffer_size = 4M
innodb_flush_log_at_trx_commit = 2
innodb_lock_wait_timeout = 50
innodb_buffer_pool_instances=23
skip-name-resolve
skip-host-cache
max_allowed_packet = 134217728
innodb_log_files_in_group = 3
innodb_thread_concurrency=16
expire_logs_days = 30  
innodb_file_per_table=1   
wait_timeout=7200
join_buffer_size=4M
######### 
innodb_print_all_deadlocks = 1
# by 5.7 slave
#slave_parallel_type=LOGICAL_CLOCK
#slave_parallel_workers=16 
#master_info_repository=TABLE
#relay_log_info_repository=TABLE
#relay_log_recovery=ON
#slave_preserve_commit_order=1
#sql_mode=ONLY_FULL_GROUP_BY,
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ALLOW_INVALID_DATES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
######################################################

[mysqldump]
quick
max_allowed_packet = 2048M

[mysql]
#######################################
default-character-set = utf8mb4
#######################################
no-auto-rehash

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout



1.1.2 设置开机启动

cat > /usr/lib/systemd/system/mysql_3306.service <

因为我之前一台机器上可以装多个mysql,所以指定了不同端口 

systemctl start firewalld
systemctl enable firewalld
systemctl status firewalld
# 开放端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=3308/tcp --permanent
# 生效
firewall-cmd --reload
firewall-cmd --list-ports

useradd mysql
chown -R mysql:mysql mysqldata_3306
chown -R mysql:mysql mysqldata_3308

cd /data/mysql_3306
ln -s /etc/my_3306.cnf my.cnf
mkdir logs
mkdir tmp
mkdir pids
chown -R mysql:mysql tmp
chown -R mysql:mysql pids
chown -R mysql:mysql logs

systemctl daemon-reload
systemctl enable mysql_3306
systemctl start mysql_3306

1.1.3 设置复制用户 

GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;

1.1.4 查看master状态

执行show master status;

1.2 从节点

1.2.1 配置

[client]
port = 3306
socket = /data/mysql_3306/tmp/mysql.sock
#default-character-set = utf8
default-character-set = utf8mb4

[mysqld]
port            = 3306
socket          = /data/mysql_3306/tmp/mysql.sock
#log            = /data/mysql_3306/logs/mysql.log

slow_query_log = 1
slow_query_log_file             = /data/mysql_3306/logs/sql_query_slow.log

basedir=/data/mysql_3306
datadir=/data/mysqldata_3306 
skip-external-locking
key_buffer_size = 256M
table_open_cache = 4096 
sort_buffer_size = 2M
read_buffer_size = 1M
read_rnd_buffer_size = 64M
net_buffer_length = 2K
thread_stack = 256K
query_cache_type=2
query_cache_size = 1024M
query_cache_limit=2M
lower_case_table_names=1
#character_set_server =  utf8
log_timestamps = SYSTEM
log_bin_trust_function_creators=1  
############################################
character-set-client-handshake = FALSE 
character-set-server = utf8mb4 
collation-server = utf8mb4_bin
init_connect='SET NAMES utf8mb4'
############################################
max_prepared_stmt_count=100000
max_connections=10000
bulk_insert_buffer_size = 128M
binlog_cache_size = 32M
tmp_table_size = 256M
max_heap_table_size =256M
thread_cache_size = 32
#  每个服务器必须有一个唯一的ID
server-id = 2
# 开启二进制日志,指定日志文件名前缀
log-bin=mysql-bin
# 设置relay log的名字
relay-log=mysql-relay
# 设置binlog格式为行级别
binlog_format = ROW
binlog_rows_query_log_events = on
binlog_row_image = full
innodb_support_xa=1
sync_binlog=100
slow_query_log
long_query_time = 1
#binlog-ignore-db = cattle
default-storage-engine=InnoDB
innodb_buffer_pool_size = 24096M 
innodb_log_file_size = 1024M
innodb_log_buffer_size = 4M
innodb_flush_log_at_trx_commit = 2
innodb_lock_wait_timeout = 50
innodb_buffer_pool_instances=23
skip-name-resolve
skip-host-cache
max_allowed_packet = 134217728
innodb_log_files_in_group = 3
innodb_thread_concurrency=16
expire_logs_days = 30  
innodb_file_per_table=1   
wait_timeout=7200
join_buffer_size=4M
######### 
innodb_print_all_deadlocks = 1
# by 5.7 slave
slave_parallel_type=LOGICAL_CLOCK
slave_parallel_workers=16 
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON
slave_preserve_commit_order=1
log_slave_updates=1
sql_mode=ONLY_FULL_GROUP_BY
#sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ALLOW_INVALID_DATES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
######################################################

[mysqldump]
quick
max_allowed_packet = 2048M

[mysql]
#######################################
default-character-set = utf8mb4
#######################################
no-auto-rehash

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout



1.2.2 启动从库

在从库的mysql中执行这些命令。

CHANGE MASTER TO
MASTER_HOST='10.101.13.55',
MASTER_USER='replication',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000103',  # 根据SHOW MASTER STATUS的结果填写
MASTER_LOG_POS=78177991; 

#重启从库
start slave;

show slave status;

如果是mysql是复制过来的,从库启动可能会提示下面的错误

Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

 修改auto.cnf中的uuid。然后重启从库即可。

# 产生新的uuid
new_uuid=$(uuidgen)
echo $new_uuid

# 在/data/mysqldata_3308/auto.cnf中配置
[mysqld]
server_uuid=de59364b-4212-4735-b160-2385e0aa73c5

# 重启从库节点

经过验证在主库中创建库,或者添加数据,数据自动从主库会同步到从库。

你可能感兴趣的:(岁月云——运维,mysql,主从)