操作系统 :CentOS 7
数据库版本:MySQL 5.6.43
主机A:192.168.81.129 (Master)
主机B:192.168.81.130 (Slave)
2.1 在Linux环境找到 /etc/my.cnf
,并用命令打开 vi /etc/my.cnf 文件
,在该文件下指定Master的配置如下:
log-bin=mysql-bin
binlog-ignore-db=information_schema
binlog-ignore-db=master
binlog-ignore-db=mysql
binlog-do-db=master
server-id=1
log-bin=mysql-bin: binlog日志,必须要加
server-id:数据库的标识唯一,这里设置为1,设置从库的时候设置为其他值
binlog-ignore-db:无需开启二进制日志文件的数据库,多个数据库则要重复设置
binlog-do-db:指定需要同步的数据库(也就是从表数据库)
2.2 正确配置截图( 配置属性应配置在 [mysqld] 中 )
2.3 重启mysql:service mysqld restart
进入mysql,赋予从库权限帐号,允许用户在主库上读取日志
在Master数据库命令行中输入:
mysql>GRANT FILE ON *.* TO 'root'@'192.168.81.130' IDENTIFIED BY '123456';
mysql>GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.81.130' IDENTIFIED BY '123456';
mysql>FLUSH PRIVILEGES;
2.4 重启mysql:service mysqld restart
,登录mysql,显示主库信息
mysql> show master status;
正确截图如下
如果输入 mysql> show master status;
出现 Empty set(0.00 sec) ,就是Master的配置没有配置正确(请回到2.1进行对比)
2.5 查看binlog日志是否开启
mysql> show variables like 'log_%'; +----------------------------------------+---------------------------------------+ | Variable_name | Value | +----------------------------------------+---------------------------------------+ | log_bin | ON | ---> ON表示已经开启binlog日志 | log_bin_basename | /usr/local/mysql/data/mysql-bin | | log_bin_index | /usr/local/mysql/data/mysql-bin.index | | log_bin_trust_function_creators | OFF | | log_bin_use_v1_row_events | OFF | | log_error | /usr/local/mysql/data/martin.err | | log_output | FILE | | log_queries_not_using_indexes | OFF | | log_slave_updates | OFF | | log_slow_admin_statements | OFF | | log_slow_slave_statements | OFF | | log_throttle_queries_not_using_indexes | 0 | | log_warnings | 1 | +----------------------------------------+---------------------------------------+
到此步为止主表配置成功
3.1修改配置文件:vi /etc/my.cnf 如下:
log-bin=mysql-bin
server-id=2
binlog-ignore-db=information_schema
binlog-ignore-db=master
binlog-ignore-db=mysql
replicate-do-db=master
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60
3.2 正确配置截图
3.2 重启mysql:service mysqld restart
在Slave(从)数据库命令行中输入:
mysql> stop slave; #关闭Slave
mysql> change master to master_host='192.168.81.129',master_user='root',master_password='123456',master_log_file='mysql-bin.0000012', master_log_pos=120724;
mysql> start slave; #开启Slave
在这里指定Master(主数据库)的信息,master_log_file是在配置Master的时候的File选项, master_log_pos是在配置Master的Position 选项,这里要进行对应
在从
从数据库中 mysql> show slave status;
查看从数据库配置的信息:
mysql> show slave status;
+----------------------------------+----------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position |
+----------------------------------+----------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+
| Waiting for master to send event | 192.168.81.129 | root | 3306 | 60 | mysql-bin.000012 | 156844 | mysqld-relay-bin.000036 | 154943 | mysql-bin.000012 | Yes | Yes | master | mysql | | | | | 0 | | 0 | 156844 | 157344 | None | | 0 | No | | | | | | 0 | No | 0 | | 0 | | | 2 | aaac4d3f-5112-11e9-b384-000c2951e86d | /var/lib/mysql/master.info | 0 | NULL | Slave has read all relay log; waiting for the slave I/O thread to update it | 86400 | | | | | | | | 0 |
+----------------------------------+----------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+
1 row in set (0.00 sec)
Slave_IO_Running = Yes
Slave_SQL_Running = Yes
从表配置成功,如果 Slave_IO_Running = NO,Slave_SQL_Running = No ,那就开启Slave
mysql> start slave; #开启Slave
ok 配置成功