一、主从服务器的IP地址
Master IP:172.16.1.1
Salve IP:172.16.1.64
二、安装mysql并配置好Master与Slave
1、安装musql服务器
#yum install mysql-server -y
2、创建MySQL数据存放位置
mkdir /mydata/data -pv
mkdir /mydata/binlogs -pv
mkdir /mydata/reaylogs -pv (从服务器上创建)
3、修改目录的属主、属组
chown -R mysql.mysql /mydata/*
Mster Server:
#vim /etc/mysql/my.cnf
datadir = /mydata/data
log-bin=/mydata/binlogs/master-bin
Salve Server:
vim /etc/mysql/my.cnf
datadir = /mydata/data
relay-log = /mydata/reaylogs/relay-log
#log-bin=mysql-bin
#binlog_format=mixed
server-id = 11
***主从服务器的Server-id的值不同
三、为Slave创建连接Master的账号:
MariaDB [(none)]> grant replication slave, replication client on *.* to repluser@‘172.16.%.%‘ identified by ‘mageedu‘;
MariaDB [(none)]> flush privileges;
查看主节点的status信息:
MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000007 | 245 | | |
+-------------------+----------+--------------+------------------+
四、在Salev上连接Master:
1、连接主服务器时如果报:
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘172.16.1.1‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘mageedu‘,MASTER_LOG_FILE=‘master-log.000007‘,MASTER_LOG_POS=245;
ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MariaDB error log
解决方法:
MariaDB [(none)]> reset slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘172.16.1.1‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘mageedu‘,MASTER_LOG_FILE=‘master-log.000007‘,MASTER_LOG_POS=245;
查看从节点状态:
MariaDB [(none)]> show slave status\G
2、启动IO_THREAD、SQL_THREAD线程:
MariaDB [(none)]> START SLAVE IO_THREAD;
MariaDB [(none)]> START SLAVE SQL_THREAD;
或者使用
MariaDB [(none)]>START SLAVE;
3、再次查看从节点状态:
MariaDB [(none)]> SHOW SLAVE STATUS\G
对比启动SLAVE前后的区别
在主节点上创建数据库
MariaDB [(none)]>create database NAME;
在从节点查看:
MariaDB [(none)]>show databases;
#################################################################
###############补充部分############################################
1、停止从节点复制功能方法:
MariaDB [(none)]> stop slave;
2、如从服务器中途加入,则需要从Master导入数据
在Master上:#mysqldump --all-databases --lock-all-tables --routines --triggers --master-data=2 --flush-logs > /tmp/all.sql
在备份的数据导入到Slave上:#scp /tmp/all/sql root@172.16.1.64:/tmp
导入备份文件后再Slave上进行如下操作:
#mysql < /tmp/all.sql
3、如何限制从服务器只读?
MariaDB [mysql]> SET GLOBAL read_only = 1;
*************************
[mysqld]
read_only = 1
4、阻止所有用户执行写操作:
MariaDB [mysql]> flush tables with read lock;
5、如何保证主从复制时的事务安全?
前提:mysql对二进制日志事件数据会有缓冲;
在master上设置如下参数:
sync_binlog = 1
本文出自 “菜鸟前行之路” 博客,谢绝转载!