Linux下的MySQL主主复制


为什么,会有mysql的主主复制。因为在一些高可用的环境中,mysql的主从不能满足现实中的一些实际需求。比如,一些流量大的网站数据库访问有了瓶颈,需要负载均衡的时候就用两个或者多个的mysql服务器,而这些mysql服务器的数据库数据必须要保持一致,那么就会用到主主复制。
mysql主从架构中其实就一个主在工作,而从就相当于一个备份机器,从通过日志监测的方式来备份主库上的数据而保证主库的数据安全。在这种架构中如果从上的数据做了改变,主数据是不会用任何变化的。因为mysql主从架构主要是mysql从监控mysql主的日志变化来实现同步,相反的在这个架构中主并没有监控从的日志变化。所以,mysql从数据反生变化,主也就没有什么变化了。
通过上述描述,可以看到如果想实现主主复制,无非就是在mysql主从架构上让mysql主实现监测从的日志变化,从而实现两台机器相互同步。(主从的架构前面有博文  )
实验环境:两台服务器:
主机名:HA1,HA2(呵呵,这个主机名是英文缩写High availability,高可用的意思)
ip:192.168.1.231
192.168.1.232
主机系统:CentOS6.4
mysql版本5.5.22
首先,看下HA1(192.168.1.231)的mysql配置文件
vim /etc/my.cnf
# Example MySQL config file for very large systems.
#
# This is for a large system with memory of 1G-2G where the system runs mainly
# MySQL.
#
# MySQL programs look for option files in a set of
# locations which depend on the deployment platform.
# You can copy this option file to one of those
# locations. For information about these locations, see:
# http://dev.mysql.com/doc/mysql/en/option-files.html
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.
# The following options will be passed to all MySQL clients
[client]
#password      = your_password
port            = 3306
socket          = /usr/local/mysql/tmp/mysql.sock
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
port            = 3306
socket          = /usr/local/mysql/tmp/mysql.sock
skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin
log-slave-updates
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id      = 1

在这个配置文件中,需要特别注意的三处地方:
log-bin=mysql-bin:这个选项基本默认都是开着的,如果没有打开,可以手动打开。
log-slave-updates:这个选项特别的重要它是为了让slave也能充当master,同时也为了更好的服务于 m-m + s 的环境,保证slave挂在任何一台master上都会接收到另一个master的写入信息。当然不局限于这个架构,级联复制的架构同样也需要log-slave-updates的支持。
server-id = 1:这个ID为服务器ID如果配置一样会出现冲突,而不能复制
接着再看下HA2(192.168.1.232)的mysql配置文件
vim /etc/my.cnf
# Example MySQL config file for very large systems.
#
# This is for a large system with memory of 1G-2G where the system runs mainly
# MySQL.
#
# MySQL programs look for option files in a set of
# locations which depend on the deployment platform.
# You can copy this option file to one of those
# locations. For information about these locations, see:
# http://dev.mysql.com/doc/mysql/en/option-files.html
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.
# The following options will be passed to all MySQL clients
[client]
#password      = your_password
port            = 3306
socket          = /usr/local/mysql/tmp/mysql.sock
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
port            = 3306
socket          = /usr/local/mysql/tmp/mysql.sock
skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin
log-slave-updates
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id      = 10
# Replication Slave (comment out master section to use this)

在HA2的mysql配置文件中,除了server-id不一样,其他几乎一模一样。配置文件写好后,我们把两台服务器上的mysql服务器启动起来。

更多详情见请继续阅读下一页的精彩内容

推荐阅读:

生产环境MySQL主主同步主键冲突处理

MySQL + KeepAlived + LVS 单点写入主主同步高可用架构实验

MySQL 主主同步配置

CentOS 6.3下MySQL主从复制笔记

  • 1
  • 2
  • 下一页

相关内容

    暂无相关文章