之前做过一次在单台机器上的多实例的mysql,这次分开做,使用两台主机。
这里使用的主机地址分别为:
MASTER:192.168.214.135
SLAVE : 192.168.214.128
这两个主机已经安装过mysql,但是两个数据库数据不一样
主从复制大致步奏
配置MySQL复制基本步骤:
一、master
1、启用二进制日志
log-bin = master-bin
log-bin-index = master-bin.index
2、选择一个惟一server-id
server-id = {0-2^32}
3、创建具有复制权限的用户
REPLICATION SLAVE
二、slave
1、启用中继日志
relay-log = relay-log
relay-log-index =
2、选择一个惟一的server-id
server-id = {0-2^32}
3、连接至主服务器,并开始复制数据;
mysql> CHANGER MASTER TO MASTER_HOST=‘‘,MASTER_PORT=‘‘,MASTER_LOG_FILE=‘‘,MASTER_LOG_FIEL_POS=‘‘,MASTER_USER=‘‘,MASTER_PASSWORD=‘‘;
mysql> START SLAVE;
mysql> START SLAVE IO_Thread;
mysql> START SLAVE SQL_Thread;
下面检查主服务器MASTER的数据库配置文件
[mysqld] port = 3306 socket = /tmp/mysql.sock skip-external-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 256 sort_buffer_size = 1M read_buffer_size = 1M read_rnd_buffer_size = 4M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size= 16M thread_concurrency = 8 datadir=/mydata/data log-bin=master-bin log-bin-index=master-bin.index binlog_format=mixed #relay-log = relay-mysql #relay-log-index = relay-mysql.index #auto-increment-increment = 2 #auto-increment-offset = 1 server-id = 1
下面配置SLAVE的配置文件
[mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysqldata socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 server-id = 2 relay-log = relay-log relay-log-index = relay-log.index主从数据库的其他配置项有所不同,这里先不管
一: 上面主从库的配置文件都设置好后,登录到MASTER库上,创建一个具有复制权限的用户
grant replication slave on *.* to ‘feng‘@‘192.168.%.%‘ identified by ‘fsz123‘;
flush privileges;
slave添加master信息前先查看master库的bin-log日志记录位置节点,master库中执行:
mysql> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000029 | 107 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.01 sec)
二:登录从库SLAVE 添加master信息
mysql> change master to master_host=‘192.168.214.135‘,master_user=‘feng‘,master_password=‘fsz123‘,master_log_file=‘master-bin.000029‘,master_log_pos=323; Query OK, 0 rows affected (0.15 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
三:启动从库复制:
mysql> SLAVE START;
执行: show slave status\G 查看复制状态
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.214.135 Master_User: feng Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000029 Read_Master_Log_Pos: 323 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 254 Relay_Master_Log_File: master-bin.000029 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: 从库的IO线程和SQL线程已经开启下面执行show databases; 查看数据库同步状况
查看从库的数据信息
Database | +--------------------+ | information_schema | | demo | | demo222 | | demo333 | | mysql | | performance_schema | | test 主库里新建的demo222,demo333已经到从库中
//////////////////////////////////////////////////////////////////////////////////////////////////////
mysql的双主模式配置
在上面的基础上,再增加双主复制的实现,实现双主模式,在从库中也要开启二进制日志文件,同时主库中也增加slave中继日志功能
编辑从库my.cnf
log-bin=master-bin
log-bin-index=master-bin.index
binlog_format=mixed
relay-log = relay-mysql
relay-log-index = relay-mysql.index
auto-increment-increment = 2
auto-increment-offset = 2
编辑主库my.cnf 添加slave日志
log-bin=master-bin log-bin-index=master-bin.index binlog_format=mixed auto-increment-increment = 2 #步进值auto_imcrement。一般有n台主MySQL就填nauto-increment-offset = 1 #步进值auto_imcrement。一般有n台主MySQL就填n
#binlog-ignore=mysql #忽略mysql库【或者其他不需同步复制的库】
# replicate-do-db=aa #要同步的数据库,默认所有库【或者其他指定复制的库】
relay-log = relay-mysql relay-log-index = relay-mysql.index
在主库中执行change master to 指令
change master to master_host=‘192.168.214.128‘,master_user=‘feng‘,master_password=‘fsz123‘,master_log_file=‘master-bin.000001‘,master_log_pos=107; mysql> flush privileges;
启动主库中的复制功能
start slave;
此时,mysql双主复制完成。
原文地址:https://www.cnblogs.com/anay/p/8260661.html