【伍哥原创】
生产环境下的MySQL主从复制方案原理
Mysql的 Replication 是一个异步的复制过程,从一个 Mysql instace(我们称之为 Master)复制到另一个 Mysql instance(我们称之 Slave)。在 Master 与 Slave 之间的实现整个复制过程主要由三个线程来完成,其中两个线程(Sql线程和IO线程)在 Slave 端,另外一个线程(IO线程)在 Master 端。
要实现 MySQL 的 Replication ,首先必须打开 Master 端的Binary Log(mysql-bin.xxxxxx)功能,否则无法实现。因为整个复制过程实际上就是Slave从Master端获取该日志然后再在自己身上完全 顺序的执行日志中所记录的各种操作。打开 MySQL 的 Binary Log 可以通过在启动 MySQL Server 的过程中使用 “—log-bin” 参数选项,或者在 my.cnf 配置文件中的 mysqld 参数组([mysqld]标识后的参数部分)增加 “log-bin” 参数项。
MySQL 复制的基本过程如下:
1. Slave 上面的IO线程连接上 Master,并请求从指定日志文件的指定位置(或者从最开始的日志)之后的日志内容;
2. Master 接收到来自 Slave 的 IO 线程的请求后,通过负责复制的 IO 线程根据请求信息读取指定日志指定位置之后的日志信息,返回给 Slave 端的 IO 线程。返回信息中除了日志所包含的信息之外,还包括本次返回的信息在 Master 端的 Binary Log 文件的名称以及在 Binary Log 中的位置;
3. Slave 的 IO 线程接收到信息后,将接收到的日志内容依次写入到 Slave 端的Relay Log文件(mysql-relay-bin.xxxxxx)的最末端,并将读取到的Master端的bin-log的文件名和位置记录到master- info文件中,以便在下一次读取的时候能够清楚的告诉Master需要从某个bin-log的哪个位置开始往后的日志内容
4. Slave 的 SQL 线程检测到 Relay Log 中新增加了内容后,会马上解析该 Log 文件中的内容成为在 Master 端真实执行时候的那些可执行的 Query 语句,并在自身执行这些 Query。这样,实际上就是在 Master 端和 Slave 端执行了同样的 Query,所以两端的数据是完全一样的。
具体实现:
一,在两个服务器上都安装好mysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#准备好用户
groupadd mysql
useradd
-g mysql mysql
#新建mysql数据目录
mkdir
-p
/data/mysql_data/3306
chown
-R mysql.mysql
/data/mysql_data
cd
/usr/local/src
#先下载mysql最新稳定版本,建议从官方网站下载,不要到其他网站下载
wget http:
//cdn
.mysql.com
/Downloads/MySQL-5
.5
/mysql-5
.5.25a.
tar
.gz
#mysql-5.5.25a需要通过cmake来安装
wget http:
//www
.cmake.org
/files/v2
.8
/cmake-2
.8.4.
tar
.gz
#安装依赖库
yum -y groupinstall
"Development Tools"
yum -y
install
gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel \
libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel
bzip2
bzip2
-devel ncurses \
ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl \
openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers
#安装cmake
tar
zxf cmake-2.8.4.
tar
.gz
cd
cmake-2.8.4
.
/configure
--prefix=
/usr/local/cmake
make
&&
make
install
cd
..
#安装mysql
tar
zxf mysql-5.5.25a.
tar
.gz
cd
mysql-5.5.25a
/usr/local/cmake/bin/cmake
-DCMAKE_INSTALL_PREFIX=
/usr/local/mysql
\
-DMYSQL_UNIX_ADDR=
/usr/local/mysql/mysql
.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=
/data/mysql_data/3306
\
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306
make
&&
make
install
#安装默认数据库
/usr/local/mysql/scripts/mysql_install_db
--basedir=
/usr/local/mysql
\
--datadir=
/data/mysql_data/3306
--user=mysql
#编辑 /data/mysql_data/3306/my.cnf
vim
/data/mysql_data/3306/my
.cnf
|
/data/mysql_data/3306/my.cnf 的内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
[client]
character-set-server = utf8
port = 3306
socket = /usr/local/mysql/mysql.sock
[mysqld]
character-set-server = utf8
replicate-ignore-db = mysql
replicate-ignore-db = performance_schema
replicate-ignore-db = information_schema
user = mysql
port = 3306
socket = /usr/local/mysql/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql_data/3306
log-error = /data/mysql_data/3306/mysql_error.log
pid-file = /data/mysql_data/3306/mysql.pid
open_files_limit = 10240
back_log = 600
max_connections = 5000
max_connect_errors = 6000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 300
thread_concurrency = 8
query_cache_size = 512M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 246M
max_heap_table_size = 246M
long_query_time = 3
log-slave-updates
log-bin = /data/mysql_data/3306/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 1G
relay-log-index = /data/mysql_data/3306/relaylog
relay-log-info-file = /data/mysql_data/3306/relaylog
relay-log = /data/mysql_data/3306/relaylog
expire_logs_days = 30
key_buffer_size = 256M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
interactive_timeout = 120
wait_timeout = 120
skip-name-resolve
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
server-id = 1 #主库写1,从库写2
log-bin = dou55_mall #仅主库需要
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 512M
innodb_data_file_path = ibdata1:256M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
log-slow-queries = /data/mysql_data/3306/slow.log
long_query_time = 1
[mysqldump]
quick
max_allowed_packet = 32M
|
接着设置密码
1
2
3
4
5
6
7
8
9
10
11
|
#启动mysqld
nohup
/usr/local/mysql/bin/mysqld_safe
--defaults-
file
=
/data/mysql_data/3306/my
.cnf &
#设置root密码及其他安全选项
#方式一:
/usr/local/mysql/bin/mysqladmin
-u root password
'root的密码'
#方式二:
#这里要先修正mysql_secure_installation的相对路径问题:
# 43行: for n in ./bin/mysql mysql 改为:for n in /usr/local/mysql/bin/mysql mysql
/usr/local/mysql/bin/mysql_secure_installation
|
二,主库的配置 (192.168.1.34)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
nohup
/usr/local/mysql/bin/mysqld_safe
--defaults-
file
=
/data/mysql_data/3306/my
.cnf &
# 在主库加入数据库
/usr/local/mysql/bin/mysql
-u root -p
mysql> create database dou55_mall;
mysql> grant all privileges on dou55_mall.* to
'dou55_mall'
@
'%'
identified by
'dou55_mall_password'
;
mysql> quit;
/usr/local/mysql/bin/mysql
-u root -p dou55_mall < .
/dou55_mall_20120710
.sql
# 设置从库权限和帐号
/usr/local/mysql/bin/mysql
-u root -p
mysql> grant replication slave on *.* to
'slave'
@
'192.168.1.27'
identified by
'slave_password'
;
mysql> flush privileges;
mysql> flush tables with
read
lock;
# 全局读锁定,执行了命令之后所有库所有表都被锁定只读
mysql> show master status;
# 显示主库的binlog的文件和位置记录,配置从库时使用
mysql> quit;
# 退出时隐式执行unlock tables
# 导出从库使用的sql备份,并发送给192.168.1.27
/usr/local/mysql/bin/mysqldump
--master-data -u root -p dou55_mall > .
/dou55_mall_for_slave_20120713
.sql
|
三,从库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
nohup
/usr/local/mysql/bin/mysqld_safe
--defaults-
file
=
/data/mysql_data/3306/my
.cnf &
# 在从库加入数据库
/usr/local/mysql/bin/mysql
-u root -p
mysql> create database dou55_mall;
mysql> grant all privileges on dou55_mall.* to
'dou55_mall'
@
'%'
identified by
'dou55_mall_password'
;
mysql> quit;
# 导入数据
/usr/local/mysql/bin/mysql
-u root -p dou55_mall <
/root/dou55_mall_for_slave_20120713
.sql
# 解锁,并设置master
/usr/local/mysql/bin/mysql
-u root -p
mysql> unlock tables;
mysql> slave stop;
mysql> change master to master_host=
'192.168.1.34'
, master_user=
'slave'
, master_password=
'slave_password'
, master_log_file=
'binlog.00018'
, master_log_position=10;
mysql> slave start;
mysql> show slave status\G;
#纵向排列,确认Slave_IO_Running=yes,Slave_SQL_Running=yes.
mysql> quit;
|
四,测试
测试比较简单,就是在主库修改数据,然后在从库查看对应的数据是否也被修改。最好增、删、改都确认一下。