#服务器规划: mysql1:192.168.2.153 mysql2:192.168.2.154 mysql-vip:192.168.2.157 #主主复制:
创建复制账号: 主库:mysql> grant replication slave on . to repl@'192.168.2.%' identified by 'slave'; 备库:mysql> grant replication slave on . to repl@'192.168.2.%' identified by 'slave';
配置主库和备库:修改my.cnf
#mysql1 [mysqld] server-id = 26 port = 3306 user = mysql basedir = /mvtech/mysql datadir = /mvtech/mysql/data tmpdir = /mvtech/mysql/ socket = /mvtech/mysql/mysql.sock skip-external-locking skip-name-resolve lower_case_table_names=1 auto_increment_offset = 1 auto_increment_increment = 2 ########## binlog ########## log_bin = /mvtech/mysql/log/mysql-bin binlog_format = row binlog_cache_size = 2M expire-logs-days = 7 ########## error log ########## log_error = /mvtech/mysql/log/mysql-error.log ########## slow log ########## slow_query_log = 1 slow_query_log_file = /mvtech/mysql/log/mysql-slow.log long_query_time = 5 ########## per_thread_buffers ########## max_connections = 1024 max_connect_errors = 1000 key_buffer_size = 64M max_allowed_packet = 128M table_open_cache = 6144 table_definition_cache = 4096 sort_buffer_size = 512K read_buffer_size = 512K join_buffer_size = 512K tmp_table_size = 64M max_heap_table_size = 64M thread_cache_size = 64 thread_concurrency = 32 bulk_insert_buffer_size = 64M ########innodb######## innodb_data_file_path = ibdata1:76M;ibdata2:1G:autoextend innodb_buffer_pool_size = 30G innodb_log_file_size = 500M innodb_log_buffer_size = 64M innodb_flush_log_at_trx_commit = 2 innodb_file_per_table = 1 innodb_file_io_threads = 4 innodb_flush_method = O_DIRECT innodb_thread_concurrency = 0 innodb_additional_mem_pool_size = 16M [mysqlhotcopy] interactive-timeout [mysqld_safe] open_files_limit = 65535
mysql
#mysql2:
[mysqld]
server-id = 27
port = 3306
user = mysql
basedir = /mvtech/mysql
datadir = /mvtech/mysql/data
tmpdir = /mvtech/mysql/
socket = /mvtech/mysql/mysql.sock
skip-external-locking
skip-name-resolve
lower_case_table_names=1
auto_increment_offset = 2
auto_increment_increment = 2
########## binlog ##########
log_bin = /mvtech/mysql/log/mysql-bin
binlog_format = row
binlog_cache_size = 2M
expire-logs-days = 7
########## error log ##########
log_error = /mvtech/mysql/log/mysql-error.log
########## slow log ##########
slow_query_log = 1
slow_query_log_file = /mvtech/mysql/log/mysql-slow.log
long_query_time = 5
########## per_thread_buffers ##########
max_connections = 1024
max_connect_errors = 1000
key_buffer_size = 64M
max_allowed_packet = 128M
table_open_cache = 6144
table_definition_cache = 4096
sort_buffer_size = 512K
read_buffer_size = 512K
join_buffer_size = 512K
tmp_table_size = 64M
max_heap_table_size = 64M
thread_cache_size = 64
thread_concurrency = 32
bulk_insert_buffer_size = 64M
########innodb########
innodb_data_file_path = ibdata1:76M;ibdata2:1G:autoextend
innodb_buffer_pool_size = 30G
innodb_log_file_size = 500M
innodb_log_buffer_size = 64M
innodb_flush_log_at_trx_commit = 2
innodb_file_per_table = 1
innodb_file_io_threads = 4
innodb_flush_method = O_DIRECT
innodb_thread_concurrency = 0
innodb_additional_mem_pool_size = 16M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open_files_limit = 65535
* 复制配置
登陆mysql1:查看show binary logs;
mysql2上操作:根据查询结果修改下面语句mysql2上操作:
CHANGE MASTER TO MASTER_HOST='192.168.2.153',
MASTER_USER='repl',
MASTER_PASSWORD='slave',
MASTER_LOG_FILE='mysql-bin.000006',
MASTER_LOG_POS=0;
查看状态:show slave status\G
Slave_IO_Running: No
Slave_SQL_Running: No
启动slave
start slave
查看mysql2状态:show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
查看mysql2线程:
show processlist\G
登陆mysql2:
查看show binary logs;
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 143 |
| mysql-bin.000002 | 229383637 |
| mysql-bin.000003 | 120 |
+------------------+-----------+
3 rows in set (0.00 sec)
mysql1操作:
CHANGE MASTER TO MASTER_HOST='192.168.2.154',
MASTER_USER='repl',
MASTER_PASSWORD='slave',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=120;
启动slave
start slave
查看mysql1状态:show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
查看mysql1线程:
show processlist\G
#keepalived配置:
* mysql1:keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface em1
virtual_router_id 61
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.157
}
}
virtual_server 192.168.2.157 3306 {
delay_loop 2
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.2.153 3306 {
weight 3
notify_down /mvtech/mysql/keepalive-mysql.sh
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
* mysql2:keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface em1
virtual_router_id 61
priority 90
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.157
}
}
virtual_server 192.168.2.157 3306 {
delay_loop 2
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.2.154 3306 {
weight 3
notify_down /mvtech/mysql/keepalive-mysql.sh
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
#keepalive-mysql.sh
#!/usr/bin/env bash
service keepalived stop
服务启动
- 启动 1.启动mysql1上的mysql;登陆查看mysql-slave状态:show slave status\G;启动slave:start slave; 2.启动mysql2上的mysql;登陆查看mysql-slave状态:show slave status\G;启动slave:start slave; 3.启动mysql1上的keepalived;查看vip状态:ip addr 4.启动mysql2上的keepalived。
- 关闭 1.查看vip漂在那台机器上,例如:在mysql1上; 2.关闭mysql2的keepalived; 3.关闭mysql1的keepalived; 4.关闭mysql1上的mysql; 5.关闭mysql2上的mysql。
keepalived模拟切换
- 关闭mysql1上的mysql,vip漂至mysql2主机上;
- 启动mysql1上的mysql同时启动keepalived,vip不漂回mysql1主机上,这是由于keepalive.conf 配置文件中将两台机器都设为backup,并增加nopreempt,此模式为不抢占模式。避免高并发网站频繁切换造成数据库数据不一致问题。
#mysql主主复制测试 mysql1和mysql2上均新建数据库,检查同步现象 #取消主主复制
mysql> show slave status\G
mysql> stop slave IO_THREAD;
mysql> stop slave;
mysql> reset master;
mysql> show slave status\G
mysql> reset slave all;