MYSQL主从复制的简单实现

系统环境:CENTOS7

MYSQL版本,status;第一行,mysql  Ver 14.14 Distrib 5.7.22, for linux-glibc2.12 (x86_64) using  EditLine wrapper

主库:192.168.156.75

从库:192.168.156.73

1、主库操作:

修改/etc/my.cnf,添加如下内容:

log-bin = mysql-bin

server-id =1

innodb-file-per-table =ON

skip_name_resolve=ON

重启mysql服务

主库上面创建并授权同步的用户:

mysql> grant replication slave,replication client on *.* to 'repluser'@'192.168.156.73' identified by 'yourpassword';

Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

 

在主库上面查得当前的BINLOG文件及Position,在从库上要同步要用到

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |     626 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

 

 

2、从库操作:

 

修改/etc/my.cnf

添加如下内容,开启中继日志

 

relay-log=relay-log

relay-log-index=relay-log.index

server-id=2

innodb_file_per_table=ON

skip_name_resolve=ON

 

重启mysql服务

 

在从库上配置同步

mysql> CHANGE MASTER TO MASTER_HOST='192.168.156.75',MASTER_USER='repluser',MASTER_PASSWORD='yourpassword', MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=626;
ERROR 3021 (HY000): This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD FOR CHANNEL '' first.
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

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

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 | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version |
+----------------------------------+----------------+-------------+-------------+---------------+------------------+---------------------+------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-----------------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Waiting for master to send event | 192.168.156.75 | repluser    |        3306 |            60 | mysql-bin.000003 |                 626 | relay-log.000010 |           320 | mysql-bin.000003      | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                 626 |            1206 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 | No                            |             0 |               |              0 |                |                             |                1 | 731ddd80-d725-11e8-949c-000c29efe4dc | /usr/local/mysql/data/master.info |         0 |                NULL | Slave has read all relay log; waiting for more updates |              86400 |             |                         |                          |                |                    |                    |                   |             0 |                      |              |                    |
+----------------------------------+----------------+-------------+-------------+---------------+------------------+---------------------+------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-----------------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
1 row in set (0.00 sec)
 

限制从库只读:

mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)

 

3、此时再验证,在主库上面创建数据库和表,看看从库上面是不是马上同步过来

 

 

更快速的安装并同步的方法:

主库IP为192.168.156.72,从库IP为192.168.156.77

1、在两台机器上分别执行安装,并设置好ROOT的密码

yum install mariadb-server mariadb -y

[root@v72 ~]# mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> grant ALL PRIVILEGES on *.* to replutil@"192.168.156.77" Identified by "111111"; #这一步只在主库运行      
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY 'root的密码' WITH GRANT OPTION; 

 

2、编辑/etc/my.cnf

主库:

[root@v72 ~]# cat /etc/my.cnf | grep -v ^#
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0

log-bin = mysql-bin
server-id =1
innodb-file-per-table =ON
skip_name_resolve=ON

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

!includedir /etc/my.cnf.d

从库:

[root@v77 mysql-utilities-1.5.3]# cat /etc/my.cnf | grep -v ^#
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
server-id =2

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

!includedir /etc/my.cnf.d

3、安装Mysql Utilities

可以在主从任意一台执行,我是在从库操作的

wget http://dev.mysql.com/get/Downloads/MySQLGUITools/mysql-utilities-1.5.3.zip

[root@v77 tar.gz]# unzip mysql-utilities-1.5.3.zip -d /tmp/mysql-utilities/

[root@v77 tar.gz]# cd /tmp/mysql-utilities/mysql-utilities-1.5.3/

[root@v77 mysql-utilities-1.5.3]# python ./setup.py build

[root@v77 mysql-utilities-1.5.3]# python ./setup.py install

[root@v77 mysql-utilities-1.5.3]# mysqlreplicate --master=root:root的密码@192.168.156.72 --slave=root:root的密码@192.168.156.77 --rpl-user=replutil:111111
WARNING: Using a password on the command line interface can be insecure.
# master on 192.168.156.72: ... connected.
# slave on 192.168.156.77: ... connected.
# Checking for binary logging on master...
# Setting up replication...
# ...done.

此时可以验证数据了,在主库建立和添加的数据马上会同步到从库上面

MYSQL主从复制的简单实现_第1张图片

你可能感兴趣的:(MYSQL主从复制的简单实现)