一、数据库主从复制基本要求:

1、操作系统版本一致(建议)。
2、数据库版本一致(建议)。
3、配置文件参数一致(是指一些性能参数)。

二、网络拓扑

Mysql 之 主从复制_第1张图片

三、主数据库配置文件

[mysqld]
user = mysql
basedir = /app/mysql
datadir = /app/mysql/data
port=3306
server-id = 10                             #要与从库区别出来
log-bin = mysql-bin                  #必须启用binlog日志功能,主从基于日志同步
binlog_format = mixed             #binlog日志格式
expire_logs_days = 7               
#slave_skip_errors=1028

四、在主库上授权从库用户进行复制

mysql> use mysql;
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> grant replication slave on *.* to 'mysync'@'10.3.150.199' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.04 sec)

五、把主库的所有据库备份下来

[root@web01 ~]# mysqldump --skip-lock-tables --single-transaction --flush-logs --hex-blob --master-data=2 -A > ~/test.sql

#检查文件的头部并记下MASTER_LOG_FILE和MASTER_LOG_POS的值
head test.sql -n80 | grep "MASTER_LOG_POS"

六、修改从库的配置文件

[mysqld]
user = mysql
basedir = /app/mysql
datadir = /app/mysql/data
port=3306
server-id = 22          #从库必须要与主库不一致
binlog-format = mixed
log_bin = mysql-bin
relay-log = mysql-relay-bin

七、在从库上创建上设置同步帐户

mysql> change master to
    -> master_host='10.3.150.198',
    -> master_user='mysync',
    -> master_password='123456',
        -> master_log_file='mysql-bin.000014',
        -> master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.08 sec)

八、导入主库备份过来的test.sql文件

    mysql> source /root/test.sql;
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    | test               |
    +--------------------+
    5 rows in set (0.00 sec)

九、启动主从同步

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.3.150.198
                  Master_User: mysync
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000011
          Read_Master_Log_Pos: 154
               Relay_Log_File: web02-relay-bin.000007
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000011
             Slave_IO_Running: Yes           #代表同步成功
            Slave_SQL_Running: Yes         #代表同步成功

九、关于主从同步的的一些异常处理方法

1、提示错误 :Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work."

主要原因:主从服务器都是克隆出来的,导致两个MYSQL的auto.cnf文件里的server-uuid值完全相同。

2、提示错误:Slave SQL: Error 'Column 'type_id' cannot be null' on query. error1048

主要原因为:主库允许插入null值,而从库不允许,只需要把explicit_defaults_for_timestamp 设置为off就可以

3、提示表不存错误

主要原因:经排查从库该表的确存在,但启用了大小写区分,导致了出现异常,可在配置文件里增加:
lower_case_table_names=1

十、主从同步状态监控(Zabbix)

slaveio.sh
#!/bin.bash
slaveio=$(mysql -uroot -pXXXXXXX -hXXXXX -e 'show slave status\G' | sed -n '12p' | cut -b 32-34)
if [ "$slaveio" = "Yes" ];
        then
                slaveio=1
        else
                slaveio=0
fi
echo $slaveio

slavesql.sh
#!/bin.bash
slavesql=$(mysql -uroot -pXXXXX -hXXXXX -e 'show slave status\G' | sed -n '13p' | cut -b 32-34)
if [ "$slavesql" = "Yes" ];
        then
                slavesql=1
        else
                slavesql=0
fi
echo $slavesql