MySQL主从又叫作Replication、AB复制。简单讲就是A和B两台机器作主从后,在A上写数据,另一台B也会跟着写数据,二者数据实时同步;
MySQL主从是基于binlog的,主上须开启binlog才能进行主从;
主从过程大体有3个步骤:
1)主将更改操做记录到binlog里
2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里
3)从根据relaylog里面的sql语句按顺序执行
主上有一个log dump线程,用来和从的I/O线程传递binlong;
从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另一个sql线程用来把relaylog里面的sql语句落地。mysql
配置主从须要两台机器,或者再搭建一个MySQL
,使用3307端口或者其余。这里使用另外一台机器搭建MySQL
。
配置MySQL
可参考以前博客:
https://blog.51cto.com/3069201/2073238
主IP:192.168.242.128
从IP:192.168.242.129linux
[root@zlinux ~]# vim /etc/my.cnf //按下面修改 [mysqld] #skip-grant # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as requiredii. basedir = /usr/local/mysql datadir = /data/mysql port = 3306 server_id = 128 #可自定义,这里就使用了ip末尾 log_bin=zlinux01 #指定log前缀 socket = /tmp/mysql.sock
[root@zlinux ~]# /etc/init.d/mysqld restart //重启mysql Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! [root@zlinux ~]# ls -lt /data/mysql/ // 重启后生成两个前缀为zlinu01的文件,必须有这两个文件 总用量 110756 -rw-rw----. 1 mysql mysql 50331648 4月 2 19:47 ib_logfile0 -rw-rw----. 1 mysql mysql 12582912 4月 2 19:47 ibdata1 -rw-rw----. 1 mysql mysql 128079 4月 2 19:47 zlinux.err -rw-rw----. 1 mysql mysql 5 4月 2 19:47 zlinux.pid -rw-rw----. 1 mysql mysql 120 4月 2 19:47 zlinux01.000001 -rw-rw----. 1 mysql mysql 18 4月 2 19:47 zlinux01.index drwx------. 2 mysql mysql 4096 3月 30 17:25 zrlog drwx------. 2 mysql mysql 96 3月 23 20:54 db1 drwx------. 2 mysql mysql 4096 3月 23 20:49 db2 drwx------. 2 mysql mysql 4096 3月 15 21:09 mysql drwx------. 2 mysql mysql 4096 3月 15 21:09 performance_schema -rw-rw----. 1 mysql mysql 1802 3月 15 21:03 localhost.localdomain.err -rw-rw----. 1 mysql mysql 56 3月 15 21:03 auto.cnf -rw-rw----. 1 mysql mysql 50331648 3月 15 21:03 ib_logfile1 drwx------. 2 mysql mysql 6 3月 15 21:03 test
[root@zlinux ~]# mysqldump -uroot -pzlinux123456 mysql > /tmp/mysql.sql //备份一个数据库 Warning: Using a password on the command line interface can be insecure. [root@zlinux ~]# mysql -uroot -pzlinux123456 -e "create database rpeltest" //创建新数据库 Warning: Using a password on the command line interface can be insecure. [root@zlinux ~]# mysql -uroot -pzlinux123456 rpeltest < /tmp/mysql.sql //将备份的数据库导入新数据库 Warning: Using a password on the command line interface can be insecure.
[root@zlinux ~]# mysql -uroot -pzlinux123456 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.6.36-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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> show databases -> ; +--------------------+ | Database | +--------------------+ | information_schema | | db1 | | db2 | | mysql | | performance_schema | | rpeltest | | test | | zrlog | +--------------------+ 8 rows in set (0.01 sec) mysql> grant replication slave on *.* to 'repl'@'192.168.242.129' identified by 'zlinux123456'; #建立用户(IP为“从”的IP) Query OK, 0 rows affected (0.00 sec) mysql> flush tables with read lock; #锁定数据表(目的是暂时使其不能继续写,保持现有状态用于同步) Query OK, 0 rows affected (0.01 sec) mysql> show master status; #记住file和position(设置主从同步时会使用) +-----------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-----------------+----------+--------------+------------------+-------------------+ | zlinux01.000003 | 120 | | | | +-----------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
[root@zlinux02 ~]# vim /etc/my.cnf basedir = /usr/local/mysql datadir = /data/mysql port = 3306 server_id = 129 log_bin=zlinux02 socket = /tmp/mysql.sock [root@zlinux02 ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!
[root@zlinux02 ~]# scp 192.168.242.128:/tmp/*.sql /tmp/ //远程复制主上的备份数据库到从上 The authenticity of host '192.168.242.128 (192.168.242.128)' can't be established. ECDSA key fingerprint is 22:fb:63:5d:8c:78:4e:74:99:f7:b1:b3:a3:70:8d:d3. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.242.128' (ECDSA) to the list of known hosts. root@192.168.242.128's password: mysql.sql 100% 648KB 647.7KB/s 00:00 [root@zlinux02 ~]# ls /tmp/ mysql.sock mysql.sql systemd-private-54b5c27d65e84794bceab836e24705c4-vmtoolsd.service-90ptAK [root@zlinux02 ~]# mysql -uroot -pzlinux123456 -e "create database rpeltest" //在从上面建立和主同样的数据库 Warning: Using a password on the command line interface can be insecure. [root@zlinux02 ~]# mysql -uroot -pzlinux123456 rpeltest < /tmp/mysql.sql //将备份数据库导入从的数据库 Warning: Using a password on the command line interface can be insecure. #该过程要保证主从数据库内容的一致
mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> change master to master_host='192.168.242.128',master_user='repl',master_password='zlinux123456',master_log_file='zlinux01.000003',master_log_pos=120; //IP为主的IP Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G //如下两个YES说明配置成功 Slave_IO_Running: Yes Slave_SQL_Running: Yes
而后在主上解锁表:sql
mysql> unlock tables;
[root@zlinux ~]# mysql -uroot -pzlinux123456 //主上 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.6.36-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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> use rpeltest; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +---------------------------+ | Tables_in_rpeltest | +---------------------------+ | columns_priv | | db | | event | | func | | general_log | | help_category | | help_keyword | | help_relation | | help_topic | | innodb_index_stats | | innodb_table_stats | | ndb_binlog_index | | plugin | | proc | | procs_priv | | proxies_priv | | servers | | slave_master_info | | slave_relay_log_info | | slave_worker_info | | slow_log | | tables_priv | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | +---------------------------+ 28 rows in set (0.00 sec) mysql> create table testuser(`id` int(4)); //主上建立testuser表 Query OK, 0 rows affected (0.04 sec) mysql> show tables; +---------------------------+ | Tables_in_rpeltest | +---------------------------+ | columns_priv | | db | | event | | func | | general_log | | help_category | | help_keyword | | help_relation | | help_topic | | innodb_index_stats | | innodb_table_stats | | ndb_binlog_index | | plugin | | proc | | procs_priv | | proxies_priv | | servers | | slave_master_info | | slave_relay_log_info | | slave_worker_info | | slow_log | | tables_priv | | testuser | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | +---------------------------+ 29 rows in set (0.00 sec)
在从上查看:数据库
mysql> show tables; +---------------------------+ | Tables_in_rpeltest | +---------------------------+ | columns_priv | | db | | event | | func | | general_log | | help_category | | help_keyword | | help_relation | | help_topic | | innodb_index_stats | | innodb_table_stats | | ndb_binlog_index | | plugin | | proc | | procs_priv | | proxies_priv | | servers | | slave_master_info | | slave_relay_log_info | | slave_worker_info | | slow_log | | tables_priv | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | +---------------------------+ 28 rows in set (0.00 sec) mysql> show tables; //多出一个表 +---------------------------+ | Tables_in_rpeltest | +---------------------------+ | columns_priv | | db | | event | | func | | general_log | | help_category | | help_keyword | | help_relation | | help_topic | | innodb_index_stats | | innodb_table_stats | | ndb_binlog_index | | plugin | | proc | | procs_priv | | proxies_priv | | servers | | slave_master_info | | slave_relay_log_info | | slave_worker_info | | slow_log | | tables_priv | | testuser | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | +---------------------------+ 29 rows in set (0.00 sec)