首页 > 代码库 > Mysql 主从 主主简单配置
Mysql 主从 主主简单配置
##创建两台版本相同的mysql
1.主节点修改配置文件 /etc/mysql/my.cf
log-bin=master-bin #启用二进制日志 默认可以不改
# binary logging format - mixed recommended
binlog_format=mixed
# 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 #serverid 默认可以不改
修改后重启数据库
2.mysql主服务器创建一个只用于复制的用户:
MariaDB [(none)]> grant replication client,replication slave on *.* to ‘repluser‘@‘192.168.%.%‘ identified by ‘replpass‘;
MariaDB [(none)]> flush privileges;
3.修改从节点服务器配置文件:
# Replication Master Server (default)
# binary logging is required for replication
#log-bin=mysql-bin #关闭二进制
# binary logging format - mixed recommended
binlog_format=mixed
# 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 = 20 #修改id编号
relay-log = relay-bin #添加中继服务日志
read-only = on #添加只读选项
MariaDB [(none)]> show global variables like ‘%only%‘; 查看只读参数
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only | OFF |
+---------------+-------+
1 row in set (0.00 sec)
MariaDB [(none)]>
4.然后修改数据库中继配置:
MASTER_HOST = ‘host_name‘ 主节点地址
MASTER_USER = ‘user_name‘ 有复制权限用户名称
MASTER_PASSWORD = ‘password‘ 有复制权限用户的密码
master_heartbeat_period=2;心跳信息时间间隔
master_log_file=‘master-bin.000001 主服务器二进制日志文件 # 查看主节点当前二进制日志文件位置
MariaDB [mysql]> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 | 497 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
master_log_pos=497 主服务器二进制日志开始位置
从服务器:
MariaDB [(none)]> change master to master_host=‘192.168.1.146‘,master_user=‘repluser‘,master_password=‘replpass‘,master_log_file=‘master-bin.000001‘,master_log_pos=497,master_connect_retry=5,master_heartbeat_period=2;
Query OK, 0 rows affected (0.06 sec)
MariaDB [(none)]> start slave
创建完成后已经成功了;查看从节点状态:
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.146
Master_User: repluser
Master_Port: 3306
Connect_Retry: 5
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 497
Relay_Log_File: relay-bin.000003
Relay_Log_Pos: 530
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 497
Relay_Log_Space: 818
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
两个主要线程是否启动:如果没启动执行 start slave 命令
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
5.配置完成,查看是否有日志:
[root@localhost mydata]# ll
total 29776
-rw-rw----. 1 mysql mysql 16384 Nov 23 19:44 aria_log.00000001
-rw-rw----. 1 mysql mysql 52 Nov 23 19:44 aria_log_control
-rw-rw----. 1 mysql mysql 18874368 Nov 23 19:44 ibdata1
-rw-rw----. 1 mysql mysql 5242880 Nov 23 19:44 ib_logfile0
-rw-rw----. 1 mysql mysql 5242880 Nov 23 19:04 ib_logfile1
-rw-rw----. 1 mysql mysql 5 Nov 23 19:44 localhost.pid
-rw-rw----. 1 mysql mysql 82 Nov 23 20:05 master.info
drwx------. 2 mysql root 4096 Nov 23 19:03 mysql
-rw-rw----. 1 mysql mysql 28229 Nov 23 19:03 mysql-bin.000001
-rw-rw----. 1 mysql mysql 1038814 Nov 23 19:03 mysql-bin.000002
-rw-rw----. 1 mysql mysql 264 Nov 23 19:44 mysql-bin.000003
-rw-rw----. 1 mysql mysql 57 Nov 23 19:04 mysql-bin.index
drwx------. 2 mysql mysql 4096 Nov 23 19:03 performance_schema
-rw-rw----. 1 mysql mysql 245 Nov 23 20:05 relay-bin.000001
-rw-rw----. 1 mysql mysql 19 Nov 23 20:05 relay-bin.index
-rw-rw----. 1 mysql mysql 43 Nov 23 20:05 relay-log.info
drwx------. 2 mysql root 4096 Nov 23 19:03 test
[root@localhost mydata]#
6.主节点创建库或表查看从节点是否有复制;
MariaDB [mydb]> create database mydb;
MariaDB [mydb]> create table tb1(id int);
双主模式配置:
主主:
互为主从:
1、数据不一致;
2、自动增长id
定义一个节点使用奇数id
auto_increment_offset=1
auto_increment_increment=2
定义另一个节点使用偶数id
auto_increment_offset=2
auto_increment_increment=2
(1) 各自使用不同的server id
(2) 都启用binlog和relay log
(3) 定义自动增长的id字段的增长方式
(4) 都授权有复制权限的用户账号
(5) 各自把对方指定为主服务器
节点1 my.cf配置:
server-id = 10
log-bin=master-bin
relay-log=relay-bin
binlog_format=mixed
auto_increment_offset=1
auto_increment_increment=2
节点2 my.cf配置:
server-id = 20
log-bin=master-bin
relay-log=relay-bin
binlog_format=mixed
auto_increment_offset=1
auto_increment_increment=2
两个节点创建可以复制权限的用户:
grant replication slave,replication client on *.* to ‘repluser‘@‘192.168.%.%‘ identified by ‘replpass‘;
查看两个节点的二进制信息:
节点一:
MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 | 507 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [mydb]> change master to master_host=‘192.168.1.109‘,master_user=‘repluser‘,master_password=‘replpass‘,master_log_file=‘master-bin.000001‘,master_log_pos=507;
启动进程:start slave;
节点二:
MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 | 507 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [mydb]> change master to master_host=‘192.168.1.146‘,master_user=‘repluser‘,master_password=‘replpass‘,master_log_file=‘master-bin.000003‘,master_log_pos=507;
启动进程:start slave;
配置完成创建数据查看状态:
MariaDB [mydb]> create table tb1 (id int unsigned not null auto_increment primary key,name char(30));
MariaDB [mydb]> insert into tb1 (name) values (‘tom‘),(‘jerry‘);
复制如何开始?
主节点运行很长时间,且已经有一定规模的数据,如何启动复制?
在主节点做一个完全备份,并记录二进制日志文件及位置;
在从节点恢复此完全备份,并在启动复制时从记录的二进制日志文件和位置开始;
复制时应该注意的问题:
1、如何限制从服务器只读?
在从服务器启动read_only;但仅对非具有SUPER权限的用户有效;
阻止所有用户 :MariaDB> FLUSH TABLES WITH READ LOCK;
2、如何保证主从复制时的事务安全?
在master节点启用参数:
sync_binlog = on #让从服务器也对尽早的读到二进制日志文件
如果用到的为InnoDB存储引擎:
innodb_flush_logs_at_trx_commit #表示innodb事务提交时立即刷写到数据磁盘中
innodb_support_xa=on #表示让innodb支持分布式事务
在slave节点:
skip_slave_start #表示跳过slave启动
主节点(不考虑io情况下)可以启动以下选项:
sync_master_info = 1
从节点:
sync_relay_log = 1
sync_relay_log_info = 1
本文出自 “眼眸刻着你的微笑” 博客,请务必保留此出处http://dengaosky.blog.51cto.com/9215128/1856617
Mysql 主从 主主简单配置