这里我们是在同一台的多实例上配置的
修改下面两个参数
vi /data/3306/my.conf
[mysqld]
server-id = 1#用于同步每台机器或实例server-id都不能相同
log-bin = /data/3306/mysql-bin
binlog_format = ROW
expire_logs_days = 30
#检查配置参数后的结果
[root@mysql backup]# egrep "server-id|log-bin" /data/3306/my.cnf
log-bin = /data/3306/mysql-bin
server-id = 1
#重启mysql服务
/data/3306/mysql restart
##也可以选择只同步某些库或者某些表
master端:
binlog-do-db = test 二进制日志记录的数据库(多数据库用逗号,隔开)
binlog-ignore-db = test 二进制日志中忽略数据库 (多数据库用逗号,隔开)
slave端
replicate-do-db 设定需要复制的数据库(多数据库使用逗号,隔开)
replicate-ignore-db 设定需要忽略的复制数据库 (多数据库使用逗号,隔开)
replicate-do-table 设定需要复制的表
replicate-ignore-table 设定需要忽略的复制表
replicate-wild-do-table 同replication-do-table功能一样,但是可以通配符
replicate-wild-ignore-table 同replication-ignore-table功能一样,但是可以加通配符
mysql -uroot -pdongshufeng123 –S /data/3306/mysql.sock
mysql> grant replication slave on *.* to 'rep@10.0.0.%' identified by 'dongshufeng123'
检查创建情况
mysql> select user,host from mysql.user where user='rep';
+------+----------+
| user | host|
+------+----------+
| rep | 10.0.0.% |
+------+----------+
1 row in set (0.01 sec)
##注意:如果主库不可以锁表,可以使用xtrabackup工具备份,并恢复数据
mysql> flush table with read lock;
query OK, 0 rows affected (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 506 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.09 sec)
查看到的两个值等会会用到
注:新开个ssh窗口
mkdir -p /server/backup
mysqldump -S /data/3306/mysql.sock --events -A -B |gzip >/server/backup/mysql_bak.$(date +%F).sql.gz
解锁主库,恢复可写
/data/3306/mysql>unlock tables;
检查一下备份是否存在
ls -l /server/backup/mysql_bak.$(date +%F).sql
修改下面两个参数
vi /data/3307/my.cony
[mysqld]
server-id = 3#用于同步每台机器或实例server-id都不能相同
#log-bin = /data/3306/mysql-bin #如果有注释掉,没有可以忽略
#重启库
/data/3307/mysql restart
cd /server/backup
ls -l
gzip -d mysql_bak.$(date +%F).sql.gz
mysql -S /data/3307/mysql.sock <mysql_bak.$(date +%F).sql
mysql -S /data/3307/mysql.sock << EOF
CHANGE MASTER TO
MASTER_HOST='10.0.0.52', #此处为主库ip
MASTER_PORT=3306,#主库端口号
MASTER_USER='rep',
MASTER_PASSWORD='dongshufeng123', #主库密码
MASTER_LOG_FILE='mysql-bin.000001',#黄色处为前面 show mast status时看到二进制日志名称
MASTER_LOG_POS=506; #黄色处为前面 show mast status时看到二进制日志偏移量
EOF
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)
查看同步状态
mysql> show slave status\G
[root@mysql backup]# cat /data/3307/data/master.info
23
mysql-bin.000001
506
10.0.0.52
rep
dongshufeng123
3306
60
0
#省掉若干
主从配置是否成功,下面三个参数最为重要,查看是否一样
[root@mysql backup]# mysql -S /data/3307/mysql.sock -e "show slave status\G;"|egrep "IO_Running|SQL_Running|_Behind_Master"
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
#在主库中创建一个库
mysql -S /data/3306/mysql.sock -e "create database dongshufeng"
#在从库中查看是否有
[root@mysql backup]# mysql -S /data/3307/mysql.sock -e "show databases like 'dongshufeng';"
+-------------------+
| Database (dongshufeng) |
+-------------------+
| dongshufeng |
+-------------------+
#删掉主库的dongshufeng库,查看从库是否也随之没了
[root@mysql backup]# mysql -S /data/3306/mysql.sock -e "drop database dongshufeng"
[root@mysql backup]# mysql -S /data/3306/mysql.sock -e "show databases like 'dongshufeng';"