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.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL主从复制MySQL数据库中非常重要的功能之一,它可以实现将一个MySQL服务器上的数据自动复制到其他MySQL服务器上,从而实现数据的备份、负载均衡、分布式数据处理等功能。下面是MySQL主从复制的原理详解: 1. 主从服务器的配置 MySQL主从服务器的配置分为两个步骤:一是在主服务器上开启二进制日志(Binary Log),二是在从服务器上设置从属关系(Slave)。 开启二进制日志的命令是: ``` mysql> SET GLOBAL binlog_format = 'ROW'; mysql> SET GLOBAL log_bin = 'mysql-bin'; ``` 设置从属关系的命令是: ``` mysql> CHANGE MASTER TO -> MASTER_HOST='master_host_name', -> MASTER_USER='replication_user_name', -> MASTER_PASSWORD='replication_password', -> MASTER_LOG_FILE='recorded_log_file_name', -> MASTER_LOG_POS=recorded_log_position; ``` 2. 主服务器的操作 在主服务器上,所有的写操作都会被写入到二进制日志中,这些日志文件会被保存在主服务器上。 3. 从服务器的操作 在从服务器上,首先要连接到主服务器上,获取二进制日志文件并进行解析,然后将解析后的内容应用到自己的数据库中。从服务器会定期地从主服务器上获取新的二进制日志文件,并更新自己的数据。 4. 数据同步的方式 MySQL主从复制有两种同步方式:基于语句的复制和基于行的复制。基于语句的复制是指从服务器将主服务器上执行的SQL语句复制到自己的数据库中,这种方式比较简单,但是有些复杂的SQL语句可能会导致从服务器上的数据不一致。基于行的复制是指从服务器将主服务器上每一行数据的变化复制到自己的数据库中,这种方式比较安全,但是需要更多的网络带宽和存储空间。 5. 数据库的故障处理 如果主服务器发生故障,从服务器可以自动切换到其他可用的主服务器上,尽量避免数据的丢失和服务的中断。 以上就是MySQL主从复制的原理详解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值