数据库主从配置

主数据库配置示例

vim /etc/my.cnf:

# For advice on how to change settings please see

# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]

#

# 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

#

# 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

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

server-id=1        #数据库唯一ID,主从的标识号绝对不能重复。

log-bin=mysql-bin    #开启bin-log,并指定文件目录和文件名前缀

binlog-do-db=master_db#需要同步的数据库。如果是多个同步库,就以此格式另写几行即可。如果不指明对某个具体库同步,就去掉此行,表示同步所有库(除了ignore忽略的库)。

binlog-ignore-db=mysql  #不同步mysql系统数据库。如果是多个不同步库,就以此格式另写几行;也可以在一行,中间逗号隔开。

sync_binlog = 1      #确保binlog日志写入后与硬盘同步

binlog_checksum = none  #跳过现有的采用checksum的事件,mysql5.6.5以后的版本中binlog_checksum=crc32,而低版本都是binlog_checksum=none

binlog_format = mixed  #bin-log日志文件格式,设置为MIXED可以防止主键重复。

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

# Recommended in standard MySQL setup

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid


从数据库配置示例

vim /etc/my.cnf:

# For advice on how to change settings please see

# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]

#

# 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

#

# 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

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

server-id=2  #设置从服务器id,必须于主服务器不同

log-bin=mysql-bin  #启动MySQ二进制日志系统

#如果需要同步的数据库名相同

#replicate-do-db=zxkang_sync  #需要同步的数据库名。如果不指明同步哪些库,就去掉这行,表示所有库的同步(除了ignore忽略的库)。

#

##如果需要同步的数据库名不同

replicate-rewrite-db=vat_db->aa  # master 上的数据库名为 zxkang_sync , slave 上的库名为 test

replicate-wild-do-table = aa.sys_permission

replicate-wild-do-table = aa.sys_permission_role

replicate-wild-do-table = aa.sys_role

replicate-wild-do-table = aa.sys_role_user

replicate-wild-do-table = aa.sys_user

#replicate-wild-rewrite-table = test_master.ts_task_type->test_slave.ts_task_type

#

replicate-ignore-db=mysql  #不同步mysql系统数据库

slave-skip-errors = all  #跳过所有的错误错误,继续执行复制操作

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

# Recommended in standard MySQL setup

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid


同步设置:

1、主数据库

    查看状态:show master status;

    结果:

2、从数据库

a、停止slave:stop slave;

b、设置同步:

CHANGE MASTER TO MASTER_HOST='主数据库IP',MASTER_USER='root',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000008',MASTER_LOG_POS=604046735;

c、重启slave:

    start slave;

3、重启数据库

    service mysqld restart;

4、查看同步情况:

    show slave status:

    

你可能感兴趣的:(数据库主从配置)