环境:
192.168.1.8 master
192.168.1.9 slave
1 关闭防火墙
[root@bogon ~]# systemctl stop firewalld
[root@bogon ~]# setenforce 0
2 安装mysql
yum -y install mariadb mariadb-server
3 编辑 master my.conf
information_schema,它提供了访问数据库元数据的方式。
什么是元数据呢?元数据是关于数据的数据,如数据库名或表名,列的数据类型,或访问权限等。
PERFORMANCE_SCHEMA,主要用于收集数据库服务器性能参数
mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=1
log-bin=mysql-bin
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
MariaDB [(none)]> GRANT all ON *.* TO 'slave'@'192.168.1.9' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show master status;
+------------------+----------+--------------+---------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+---------------------------------------------+
| mysql-bin.000003 | 459 | | information_schema,performance_schema,mysql |
+------------------+----------+--------------+---------------------------------------------+
4 编辑 slave my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-bin=myql-bin
server-id=2
MariaDB [(none)]> change master to master_host='192.168.1.8',master_user='slave',master_password='123456',master_log_file='mysql-bin.000003', master_log_pos=459;
Query OK, 0 rows affected (0.05 sec)
MariaDB [(none)]> slave start;
ariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.8
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 459
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
查看binlog方法一:
MariaDB [(none)]> show binlog events;
#查看指定binlog文件的内容
MariaDB [(none)]> show binlog events in 'mysql-bin.000002';
查看binlog方法二:mysqlbinlog
[root@server01 mysql]# mysqlbinlog /var/lib/mysql/mysql-bin.000001 |more
mysql主从复制的三种类型:
binlog有三种格式:Statement、Row以及Mixed。
–基于SQL语句的复制(statement-based replication,SBR),
–基于行的复制(row-based replication,RBR),
–混合模式复制(mixed-based replication,MBR)。
查看当前日志格式:
MariaDB [(none)]> show variables like 'binlog_format';
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1、STATMENT模式:基于SQL语句的复制(statement-based replication, SBR),
每一条会修改数据的sql语句会记录到binlog中。
优点:不需要记录每一条SQL语句与每行的数据变化,这样子binlog的日志也会比较少,减少了磁盘IO,提高性能。
缺点:在某些情况下会导致master-slave中的数据不一致(如sleep()函数, last_insert_id(),以及user-defined functions(udf)等会出现问题)
2、基于行的复制(row-based replication, RBR):
不记录每一条SQL语句的上下文信息,仅需记录哪条数据被修改了,修改成了什么样子了。(不记录sql执行过程,只记录结果!)
优点:不会出现某些特定情况下的存储过程、或function、或trigger的调用和触发无法被正确复制的问题。
缺点:会产生大量的日志,尤其是alter table的时候会让日志暴涨。
3、混合模式复制(mixed-based replication, MBR):以上两种模式的混合使用,
一般的复制使用STATEMENT模式保存binlog,对于STATEMENT模式无法复制的操作使用ROW模式保存binlog,MySQL会根据执行的SQL语句选择日志保存方式。
mysql 主从复制的作用:
1、主数据库出现问题,可以切换到从数据库。
2、可以进行数据库层面的读写分离。
3、可以在从数据库上进行日常备份。