一、复制准备
1、定义服务器角色
主库(mysql master):[ip 为 10.0.0.7 port 为 3306]
从库(mysql slave):[ip 为 10.0.0.7 port 为 3307]
#特别提示:
#1.一般常规做主从复制,主从服务器多数在不同的机器上,并且监听的端口均为默认的3306.
#2.本文主从复制技术时针对前的课程以但数据库多实例环境来讲解的。
#3.第一点虽然不在一个机器上,但是步骤和过程和2点是一样的。
#4.读者在掌握了本文单数据多实例的同步方法后,可以自己适当扩展,完成异机相同端口之间主从复制
2、数据库环境准备
1)具备单机单数据库多实例的环境。
2)或俩台服务器每个机器一个数据库的环境
3、数据库读法的约定
主库,也可称为master 3306端口
从库,也可称为slave 3307端口
二、主库上执行操作
1、设置server-id值开启binlog参数
[root@localhost ~]# ll /data/ 总用量 12 drwxr-xr-x. 3 mysql mysql 4096 9月 18 18:30 3306 drwxr-xr-x. 3 mysql mysql 4096 9月 18 18:23 3307 drwxr-xr-x. 3 mysql mysql 4096 9月 19 00:53 3308 [root@localhost ~]# ps -ef |grep mysql root 10294 9823 0 13:02 pts/1 00:00:00 grep mysql [root@localhost ~]# killall mysqld* mysqld*: 没有进程被杀死 [root@localhost ~]# /data/3306/mysql start Starting MySQL... [root@localhost ~]# /data/3307/mysql start Starting MySQL... [root@localhost ~]# ss -lnt|grep 330 LISTEN 0 128 *:3306 *:* LISTEN 0 128 *:3307 *:* [root@localhost ~]# vim /data/3306/my my.cnf mysql-bin.000002 mysql-bin.000005 mysqld.pid mysql mysql-bin.000003 mysql-bin.000006 mysql_oldboy3306.err mysql-bin.000001 mysql-bin.000004 mysql-bin.index mysql.sock [root@localhost ~]# vim /data/3306/my.cnf #检查配置结果
[root@localhost ~]# egrep "log-bin|server-id" /data/3306/my.cnf log-bin = /data/3306/mysql-bin server-id = 1 #这俩个参数都在[mysqld]模块下 [root@localhost ~]# ll /data/3306/ 总用量 56 drwxr-xr-x. 6 mysql mysql 4096 9月 22 13:03 data -rw-r--r--. 1 mysql mysql 1899 9月 22 13:06 my.cnf -rwx------. 1 root root 1310 9月 18 17:48 mysql -rw-rw----. 1 mysql mysql 126 9月 18 14:45 mysql-bin.000001 -rw-rw----. 1 mysql mysql 126 9月 18 15:13 mysql-bin.000002 -rw-rw----. 1 mysql mysql 349 9月 18 17:53 mysql-bin.000003 -rw-rw----. 1 mysql mysql 126 9月 18 18:23 mysql-bin.000004 -rw-rw----. 1 mysql mysql 126 9月 18 18:30 mysql-bin.000005 -rw-rw----. 1 mysql mysql 107 9月 22 13:03 mysql-bin.000006 -rw-rw----. 1 mysql mysql 168 9月 22 13:03 mysql-bin.index -rw-rw----. 1 mysql mysql 6 9月 22 13:03 mysqld.pid -rw-r-----. 1 mysql root 10574 9月 22 13:03 mysql_oldboy3306.err srwxrwxrwx. 1 mysql mysql 0 9月 22 13:03 mysql.sock [root@localhost ~]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e "show variables like 'log_bin';" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | +---------------+-------+
2、建立用语从库复制的账号rep:
[root@localhost ~]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.32-log Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> grant replication slave on *.* to 'rep'@'10.0.0.%' identified by 'oldboy123'; Query OK, 0 rows affected (0.37 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
3、对数据库锁表只读(当前窗口不要关掉),导出数据。然后解表。
mysql> flush table with read lock; Query OK, 0 rows affected (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000006 | 333 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) mysql> show master logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 126 | | mysql-bin.000002 | 126 | | mysql-bin.000003 | 349 | | mysql-bin.000004 | 126 | | mysql-bin.000005 | 126 | | mysql-bin.000006 | 333 | +------------------+-----------+ 6 rows in set (0.00 sec)
#上面可以不进行,可以通过下面加参数 -S 锁表
[root@localhost ~]# mysqldump -uroot -poldboy123 -S /data/3306/mysql.sock -A -B --event |gzip > /opt/rep.sql.gz
[root@localhost ~]# mysqldump -uroot -poldboy123 -S /data/3306/mysql.sock -A -B --event --master-data=2 > /opt/rep.sql
[root@localhost ~]# vim /opt/rep.sql
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
4、把数据同步到SLAVE
[root@localhost ~]# mysql -uroot -poldboy123 -S /data/3307/mysql.sock
三、从库上执行
1、设置server-id值关闭binlog参数
[root@localhost ~]# vim /data/3307/my.cnf [root@localhost ~]# egrep "log-bin|server-id" /data/3307/my.cnf #log-bin = /data/3307/mysql-bin server-id = 3
2、登陆从库配置同步参数
[root@localhost ~]# mysql -uroot -poldboy123 -S /data/3307/mysql.sock Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.32 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> CHANGE MASTER TO
MASTER_HOST='10.0.0.7',
MASTER_PORT=3306, MASTER_USER='rep',
MASTER_PASSWORD='oldboy123',
MASTER_LOG_FILE='mysql-bin.000006',
MASTER_LOG_POS=333; Query OK, 0 rows affected (0.11 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec)
3、启动从库同步开关,并查看同步状态
mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.7 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000006 Read_Master_Log_Pos: 333 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000006 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: mysql 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: 333 Relay_Log_Space: 403 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)
[root@localhost data]# cat master.info 18 mysql-bin.000006 333 10.0.0.7 rep oldboy123 3306 60 0 0 1800.000 0
四、测试
1、主库
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | d3306 | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) mysql> create database oldboy; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | d3306 | | mysql | | oldboy | | performance_schema | | test | +--------------------+ 6 rows in set (0.00 sec)
2、从库
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | d3306 | | d3307 | | mysql | | oldboy | | performance_schema | | test | +--------------------+ 7 rows in set (0.00 sec) [root@localhost 3307]# cat relay-log.info /data/3307/relay-bin.000002 340 mysql-bin.000006 420 [root@localhost 3307]# cat data/master.info 18 mysql-bin.000006 420 10.0.0.7 rep oldboy123 3306 60 0 0 1800.000 0
附加:
[root@localhost ~]# mysqldump -uroot -poldboy123 -S /data/3306/mysql.sock -A -B --event --master-data=2 > /opt/rep.sql #之前这里讲过如果 --master-data=1 导出的日志信息显示的位置和pos值 就不会被注释,所有下面的蓝色部分就不用指定
mysql> CHANGE MASTER TO
MASTER_HOST='10.0.0.7',
MASTER_PORT=3306, MASTER_USER='rep',
MASTER_PASSWORD='oldboy123',
MASTER_LOG_FILE='mysql-bin.000006',
MASTER_LOG_POS=333;
主从复制配置过程