Linux下mysql主从搭建

环境

centos7.4

mysql5.7.3

node1:192.168.199.240

node2:192.168.199.241

1.安装依赖包(两个节点都需要安装)

yum install -y  gcc gcc-c++ cmake ncurses ncurses-devel bison

2.安装mysql数据库(两个节点都需要安装)

[root@node1 lxl]# mkdir -p /opt/mysql/data
[root@node1 lxl]# groupadd mysql
[root@node1 lxl]# useradd -r -g mysql mysql
[root@node1 lxl]# tar -zxvf mysql-boost-5.7.31.tar.gz 
[root@node1 mysql-5.7.31]#cmake -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=boost -DCMAKE_INSTALL_PREFIX=/opt/mysql/ -DMYSQL_DATADIR=/opt/mysql/data
[root@node1 mysql-5.7.31]#make
[root@node1 mysql-5.7.31]#make install
初始化是数据库
[root@node1 bin]# ./mysql_install_db --basedir=/opt/mysql --datadir=/opt/mysql/data --user=mysql
2020-11-27 13:43:14 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2020-11-27 13:43:17 [WARNING] The bootstrap log isn't empty:
2020-11-27 13:43:17 [WARNING] 2020-11-27T05:43:14.708673Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2020-11-27T05:43:14.709505Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2020-11-27T05:43:14.709512Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
[root@node1 mysql]# cd support-files/
[root@node1 support-files]# ll
total 24
-rw-r--r-- 1 mysql mysql   773 Jun  2 19:05 magic
-rwxr-xr-x 1 mysql mysql  1061 Nov 27 09:57 mysqld_multi.server
-rwxr-xr-x 1 mysql mysql   864 Nov 27 09:57 mysql-log-rotate
-rwxr-xr-x 1 mysql mysql 10540 Nov 27 09:57 mysql.server
[root@node1 support-files]# pwd
/opt/mysql/support-files
[root@node1 support-files]# cp mysql.server /etc/init.d/mysqld
[root@node1 support-files]# 
配置配置文件--需要配置二进制日志
[root@node1 mariadb]# vi /etc/my.cnf
[mysqld]
datadir=/opt/mysql/data
socket=/tmp/mysql.sock
port=3306
user=mysql
#binlog
log_bin=binlog
expire_logs_days=10
max_binlog_size=100M
server_id=1
[mysqld_safe]
log-error=/opt/mysql/mariadb/mariadb.log
pid-file=/opt/mysql/mariadb/mariadb.pid
使用mysqld_safe启动数据库
[mysql@node1 bin]$ ./mysqld_safe --user-mysql &
[1] 57303
[mysql@node1 bin]$ 2020-11-27T05:58:43.923998Z mysqld_safe Logging to '/opt/mysql/mariadb/mariadb.log'.
2020-11-27T05:58:43.950039Z mysqld_safe Starting mysqld daemon with databases from /opt/mysql/data
2020-11-27T05:58:45.934145Z mysqld_safe mysqld from pid file /opt/mysql/mariadb/mariadb.pid ended
启动数据库
[root@node1 mariadb]# service mysqld start
Starting MySQL. SUCCESS! 

配置环境变量
[root@node1 mysql-5.7.31]# vi /etc/profile
MYSQL_HOME=/opt/mysql
PATH=$PATH:$MYSQL_HOME/bin
export PATH
[root@node1 mysql-5.7.31]# source /etc/profile
进入数据库
[root@node1 mysql-5.7.31]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.31-log Source distribution

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> 

2.主库创建复制用户

mysql> grant replication slave on *.* to 'rep1'@'192.168.199.240' identified by '123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
#设置锁定有效,保持数据的一直性
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)
#查看当前二进制日志位置
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000001 |      603 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
利用mysqldump拷贝主库数据到从库
主库:
[root@node1 ~]# mysqldump -h localhost -u root -p --all-databases >mysql2.sql
Enter password: 
[root@node1 ~]# scp mysql2.sql root@node2:/opt
root@node2's password: 
mysql2.sql    
从库:
[root@node2 opt]# mysql -u root -p <mysql2.sql 
Enter password: 
主库:
mysql> unlock tables;

3.修改备库的配置文件

#server_id修改为2,其他配置与主库相同
[mysqld]
server_id=2

4.配置备库

#登入数据库,
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
#在从库配置主节点的信息
mysql> change master to 
    -> master_host='192.168.199.240',
    -> master_user='rep1',
    -> master_password='123',
    -> master_log_file='binlog.000001',
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
#启用slave线程
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
#查看备节点信息 (\G后面不要加;会显示报错:ERROR: No query specified)
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.199.240
                  Master_User: rep1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: node2-relay-bin.000002
                Relay_Log_Pos: 317
        Relay_Master_Log_File: binlog.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           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: 524
              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: ffd99d4a-29b5-11eb-adf6-000c295a2449
             Master_Info_File: /opt/mysql/data/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)

5.验证同步是否成功

主节点插入数据
mysql> use test_db;
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> select * from test;
+------+------+
| a    | b    |
+------+------+
|    1 | 2    |
+------+------+
1 row in set (0.00 sec)
mysql> insert into test values('2','3');
Query OK, 1 row affected (0.01 sec)

mysql> select * from test;
+------+------+
| a    | b    |
+------+------+
|    1 | 2    |
|    2 | 3    |
+------+------+
2 rows in set (0.00 sec)
备节点查询数据是否同步
mysql> use test_db;
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> select * from test;
+------+------+
| a    | b    |
+------+------+
|    1 | 2    |
+------+------+
1 row in set (0.00 sec)

mysql> select * from test;
+------+------+
| a    | b    |
+------+------+
|    1 | 2    |
|    2 | 3    |
+------+------+
2 rows in set (0.00 sec)
数据已经同步!证明配置成功!!!

 


版权声明:本文为silenceray原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。