MYSQL 主从搭建详细步骤和测试

MySQL主备搭建
1.主库配置
以下所有操作均在主服务器上执行
1)创建用户并授权

create user slave identified with mysql_native_password by '123456'
mysql>GRANT REPLICATION SLAVE ON *.* to 'slave'@'%' identified by '123456'; //如果用户已存在,会改密码,如果不存在会直接创建这个用户,所以不需要create user单独创建用户
mysql>FLUSH PRIVILEGES;
2)修改主库配置文件
开启binlog,并设置server-id,每次修改配置文件后都要重启mysql服务才会生效

vim /etc/my.cnf
#同步的日志路径及文件名,一定注意这个目录要是mysql有权限写入的
log-bin=/var/lib/mysql/binlog 
#master端的id号,不唯一即可
server-id=1 
#指定同步的数据库,如果不写,默认是同步所有数据库
binlog-do-db = 数据库
#指定不同步的数据库  
binlog-ignore-db=mysql
#binlog日志的保留时间
expire_logs_days=7
#指定binlog日志的格式
binlog_format=ROW 
改配置文件后,重启服务:
service mysqld restart

3)查看主服务器当前二进制日志名和偏移量

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

到此主服务器已经配置好:
2.从库配置
以下所有操作均在从服务器上执行
1)修改从库配置文件

vim /etc/my.cnf
#master端的id号,不唯一即可
server-id=99
#同步的日志路径及文件名,一定注意这个目录要是mysql有权限写入的
log-bin=mysql-bin
#复制哪个库可以不用填写
replicate-do-db=webkit
replicate-do-db=dgp
replicate-do-db=dses_etldb
replicate-do-db=dses_metadb
replicate-do-db=dcf
replicate-do-db=etl
replicate-do-db=metadata
#不复制哪个库
replicate-ignore-db=mysql,information_schema,performance_schema
#这两个是启用relaylog的自动修复功能,避免由于网络之类的外因造成日志损坏,主从停止,保证数据写入的一致性
master_info_repository=table
relay_log_info_repository=table
改配置文件后,重启服务:
service mysqld restart


[root@localhost:(none)]>CHANGE MASTER TO MASTER_HOST='10.153.119.5',
    -> MASTER_PORT=3306,
    -> MASTER_USER='slave',
    -> MASTER_PASSWORD='123456',
    -> MASTER_LOG_FILE='mysql-bin.000014',
    -> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.03 sec)

注:MASTER_LOG_FILE和MASTER_LOG_POS的值为master节点中执行show master status查询到的信息 

2)启动slave进程

mysql> start slave;
Query OK, 0 rows affected (0.04 sec)
3)查看slave状态

mysql> show slave status\G;(后面的分号不去的会报错,但无影响)

如果下面两项值为YES,则表示配置正确:
Slave_IO_Running: No
Slave_SQL_Running: Yes

[root@localhost:mytest]>show slave status\G;  
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 10.153.119.5
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000014
          Read_Master_Log_Pos: 154
               Relay_Log_File: t3-dtpoc-dtpoc-web05-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000014
             Slave_IO_Running: No
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: mysql,information_schema,performance_schema
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 154
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1593
                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.
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 230911 11:05:41
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

我们可以看到如下报错:
注意:1、主从的server_id 一定不能重复;
2、主从的uuid 不能重复;SELECT UUID()/show variables like '%uuid';
如果重复可以在/opt/module/mysql8/datas/mysql/atuo.cnf 删除或者修改重启服务;
                Last_IO_Errno: 1593
                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> show variables like '%uuid';
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | 6797f03c-2122-11ee-842b-00505695c6d5 |
+---------------+--------------------------------------+
1 row in set (0.01 sec)

备:
[root@localhost:mytest]>show variables like '%uuid';
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | 6797f03c-2122-11ee-842b-00505695c6d5 |
+---------------+--------------------------------------+
1 row in set (0.01 sec)

vi /testdata/mysql/auto.cnf
[auto]
server-uuid=6797f03c-2122-11ee-842b-00505695c6d5
应该是在做物理备份测试的时候把/testdata/mysql/所有文件都COPY过来了导致的

我们在备机把server-uuid=6797f03c-2122-11ee-842b-00505695c6d5这一行删掉重启服务就可以了
[root@localhost:(none)]>show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.153.119.5
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000014
          Read_Master_Log_Pos: 481
               Relay_Log_File: t3-dtpoc-dtpoc-web05-relay-bin.000004
                Relay_Log_Pos: 647
        Relay_Master_Log_File: mysql-bin.000014
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: mysql,information_schema,performance_schema
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 481
              Relay_Log_Space: 869
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 6797f03c-2122-11ee-842b-00505695c6d5
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified


可以看到主从搭建成功了,我们在主库插入一条数据测试下:
主:
mysql> insert into t1 values(88888888,'test');
Query OK, 1 row affected (0.00 sec)、

备:
[root@localhost:mytest]>select * from t1;
+----------+----------+
| id       | name     |
+----------+----------+
|        1 | xiaoming |
|        2 | xiaohong |
|        3 | xiaoli   |
|        3 | xiaoli   |
|        4 | xiaozhao |
| 88888888 | test     |
| 88888888 | test     |
+----------+----------+
7 rows in set (0.00 sec)


我们的搭建基础是在主备数据库和数据都一样的情况下,如果主库有数据库mytet,而备份搭建的时候并没有这个数据库,那么主库已有的数据库和数据会自动复制过来吗?我们来测试下
在备库删除Mytet数据库后重建复制:
[root@localhost:mytest]>drop database mytest;
Query OK, 0 rows affected (0.00 sec)

[root@localhost:(none)]>CHANGE MASTER TO MASTER_HOST='10.153.119.5',
    ->      MASTER_PORT=3306,
    ->      MASTER_USER='slave',
    ->     MASTER_PASSWORD='123456',
    ->     MASTER_LOG_FILE='mysql-bin.000014',
    ->      MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

发现报错Error executing row event: 'Table ',mytest.t1' doesn't exist',在执行插入mytest.t1 values(88888888,'test')sql时报错。
[root@localhost:(none)]>start slave;
Query OK, 0 rows affected (0.01 sec)

[root@localhost:(none)]>show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.153.119.5
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000014
          Read_Master_Log_Pos: 808
               Relay_Log_File: t3-dtpoc-dtpoc-web05-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000014
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: mysql,information_schema,performance_schema
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1146
                   Last_Error: Error executing row event: 'Table 'mytest.t1' doesn't exist'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 1196
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 1146
               Last_SQL_Error: Error executing row event: 'Table 'mytest.t1' doesn't exist'
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 6797f03c-2122-11ee-842b-00505695c6d5
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 230911 11:29:49
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 


我们跳过这个sql,然后重建复制呢?

主库:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000014 |      808 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

备库:
[root@localhost:(none)]>stop slave;
Query OK, 0 rows affected (0.00 sec)

[root@localhost:(none)]>CHANGE MASTER TO MASTER_HOST='10.153.119.5',
    ->      MASTER_PORT=3306,
    ->      MASTER_USER='slave',
    ->     MASTER_PASSWORD='123456',
    ->     MASTER_LOG_FILE='mysql-bin.000014',
    ->      MASTER_LOG_POS=808;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

[root@localhost:(none)]>start slave;
Query OK, 0 rows affected (0.00 sec)

[root@localhost:(none)]>show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.153.119.5
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000014
          Read_Master_Log_Pos: 808
               Relay_Log_File: t3-dtpoc-dtpoc-web05-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000014
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: mysql,information_schema,performance_schema
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 808
              Relay_Log_Space: 542
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 6797f03c-2122-11ee-842b-00505695c6d5
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.01 sec)

我们才测试下,原库创建新库新表,然后插入新数据,发现建库建表DDL和插入数据都复制过去了

原库:
mysql> create database mytest1;
Query OK, 1 row affected (0.00 sec)

mysql> use mytest1;
Database changed
mysql> create table t1(id int,name varchar(20));       
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values(8888,'tsss');
Query OK, 1 row affected (0.00 sec)

备库:
[root@localhost:(none)]>use mytest1;
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
[root@localhost:mytest1]>select * from t1;
+------+------+
| id   | name |
+------+------+
| 8888 | tsss |
+------+------+
1 row in set (0.00 sec)

[root@localhost:mytest1]>
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL主从复制是一种常用的数据库复制技术,用于实现数据的备份、读写分离和负载均衡。下面是一种简单的MySQL主从搭建步骤: 1. 确保主服务器和从服务器上都安装了MySQL数据库,并且版本一致。 2. 在主服务器上编辑MySQL配置文件(my.cnf),启用二进制日志功能。在配置文件中添加以下内容: ``` [mysqld] log-bin=mysql-bin server-id=1 ``` `log-bin`参数指定二进制日志文件的名称,`server-id`参数指定主服务器的唯一ID。 3. 重启主服务器使配置生效。 4. 在主服务器上创建一个用于复制的用户,并授予适当的权限。例如,可以使用以下命令创建用户并授予权限: ``` CREATE USER 'replication_user'@'slave_ip' IDENTIFIED BY 'password'; GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'slave_ip'; FLUSH PRIVILEGES; ``` `slave_ip`是从服务器的IP地址,`password`是用户的密码。 5. 在主服务器上执行以下命令,获取当前二进制日志文件和位置的信息: ``` SHOW MASTER STATUS; ``` 记下输出结果中的文件名和位置信息,稍后在从服务器上配置时会使用。 6. 在从服务器上编辑MySQL配置文件(my.cnf),启用复制功能。在配置文件中添加以下内容: ``` [mysqld] server-id=2 ``` `server-id`参数指定从服务器的唯一ID,与主服务器不同即可。 7. 重启从服务器使配置生效。 8. 在从服务器上执行以下命令,配置从服务器连接到主服务器并开始复制: ``` CHANGE MASTER TO MASTER_HOST='master_ip', MASTER_USER='replication_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='master_log_file', MASTER_LOG_POS=master_log_pos; ``` `master_ip`是主服务器的IP地址,`replication_user`和`password`是之前创建的复制用户的用户名和密码,`master_log_file`和`master_log_pos`是之前在主服务器上获取的二进制日志文件和位置信息。 9. 启动从服务器上的复制进程: ``` START SLAVE; ``` 10. 在从服务器上执行以下命令,检查复制状态: ``` SHOW SLAVE STATUS\G ``` 如果输出结果中的`Slave_IO_Running`和`Slave_SQL_Running`都为`Yes`,表示主从复制已成功搭建。 这只是一个简单的MySQL主从搭建步骤,实际环境中可能还需要考虑更多因素,如网络配置、防火墙设置等。具体操作还需要根据实际情况进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值