MySQL主从介绍
MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的
MySQL主从是基于binlog的,主上须开启binlog才能进行主从。
主从过程大致有3个步骤
1)主将更改操作记录到binlog里
2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里
3)从根据relaylog里面的sql语句按顺序执行
主上有一个log dump线程,用来和从的I/O线程传递binlog
从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另外一个SQL线程用来把relaylog里面的sql语句落地
操作实例:
主 服务器 192.168.4.81 从服务器: 192.168.4.82
192.168.4.81, 192.168.4.82 :
安装 mysql
MySQL安装
yum install -y perl-Module-Install.noarch
cd /usr/local/src
tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
cd /usr/local/mysql
useradd -s /sbin/nologin mysql
mkdir -p /data/mysql; chown -R mysql:mysql /data/mysql
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
vim /etc/init.d/mysqld
定义basedir和datadir
basedir=/usr/local/mysql
datadir=/data/mysql
/etc/init.d/mysqld start
chkconfig --add mysqld
chkconfig mysqld on
再将路径 /usr/local/mysql/bin 加入到环境变量 /etc/profile里面
source /etc/profile
登录主:
创建数据库 db1;
[root@81 mysql]# mysql -uroot
mysql> create database db1;
mysql> create table t1(c1 int);
Query OK, 0 rows affected (0.03 sec)
mysql> create table t2(c1 int);
Query OK, 0 rows affected (0.06 sec)
mysql> create table t3(c1 int);
Query OK, 0 rows affected (0.01 sec)
备份数据库 db1 到 db1.sql
[root@81 mysql]# mysqldump -uroot db1 > /tmp/db1.sql
并传输sql 文件到从 82 服务器
scp /tmp/db1.sql root@192.168.4.82:/tmp
编辑主mysql 配置文件
datadir = /data/mysql
# port = .....
server_id = 81
log-bin=weifeng1
socket = /tmp/mysql.sock
service mysqld restart 重新 mysql 服务
登录主:
mysql> grant replication slave on *.* to 'repl'@'192.168.4.82' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
创建用户 权限 replication, 用户名 repl, 密码 123456
mysql> flush privileges;
刷新权限
Query OK, 0 rows affected (0.00 sec)
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
表读锁死
mysql> show master status; 查看主状态
+----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| weifeng.000002 | 408 | db1,db2 | mysql | |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
登录从mysql :
修改配置文件:
datadir = /data/mysql
# port = .....
server_id = 82
socket = /tmp/mysql.sock
重启 mysql 数据库
service mysqld restart
登录从数据库:
mysql -uroot
创建数据库 db1;
mysql> create database db1;
还原数据库db1
[root@localhost mysql]# mysql -uroot db1 < /tmp/db1.sql
登录从:
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='192.168.4.81',master_port=3306,master_user='repl',master_password='123456',master_log_file='weifeng.000002',master_log_pos=408;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave statusG;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.81
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: weifeng.000002
Read_Master_Log_Pos: 408
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 281
Relay_Master_Log_File: weifeng.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
若running处 2个都为yes表示配置成功
测试主从
mysql>unlock tables;
mysql>use db1;
mysql>show tables;
mysql>drop table help_category;
从上查看没有这个表格
mysql>drop table proc;
从上查看没有这个表格
mysql>show create table userG:
mysql>创建weifeng的表格
从上查看可以看到这个表格
mysql>drop database db1;
mysql>show databases;
从上就没有这个库