<返回更多

mysql搭建及主从同步+读写分离

2022-04-08    java小悠
加入收藏

从库生成两个线程,一个I/O线程,一个SQL线程;

i/o线程去请求主库 的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中;

主库会生成一个 log dump 线程,用来给从库 i/o线程传binlog;

SQL 线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,而最终数据一致;

规划

MySQL01

192.168.226.127

mysql02

192.168.226.128

mysql03

192.168.226.129

mysql04

192.168.226.130

centos7.6

mysql-5.7.17.tar

mysql-proxy-0.8.5-linux-debian6.0-x86-64bit.tar.gz

mysql01 主 mysql02从 mysql03 读写分离 mysql04测试机

mysql搭建

1.解压

[root@mysql01 ~]# ls
anaconda-ks.cfg  mysql-5.7.17.tar
[root@mysql01 ~]# tar -xvf mysql-5.7.17.tar 
./mysql-community-client-5.7.17-1.el7.x86_64.rpm
./mysql-community-common-5.7.17-1.el7.x86_64.rpm
./mysql-community-devel-5.7.17-1.el7.x86_64.rpm
./mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
./mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
./mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
./mysql-community-libs-5.7.17-1.el7.x86_64.rpm
./mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
./mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
./mysql-community-server-5.7.17-1.el7.x86_64.rpm
./mysql-community-test-5.7.17-1.el7.x86_64.rpm

2.安装

[root@mysql01 ~]# ls
anaconda-ks.cfg
mysql-5.7.17.tar
mysql-community-client-5.7.17-1.el7.x86_64.rpm    #安装
mysql-community-common-5.7.17-1.el7.x86_64.rpm    #安装
mysql-community-devel-5.7.17-1.el7.x86_64.rpm  
mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
mysql-community-libs-5.7.17-1.el7.x86_64.rpm      #安装
mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm  #安装
mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
mysql-community-server-5.7.17-1.el7.x86_64.rpm   #安装
mysql-community-test-5.7.17-1.el7.x86_64.rpm

[root@mysql01 ~]#yum install -y mysql-community-client-5.7.17-1.el7.x86_64.rpm mysql-community-common-5.7.17-1.el7.x86_64.rpm mysql-community-libs-5.7.17-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm mysql-community-server-5.7.17-1.el7.x86_64.rpm

3.启动mysql服务

[root@mysql01 ~]# systemctl start mysqld
[root@mysql01 ~]# cat /var/log/mysqld.log | grep password
2022-03-11T13:48:31.592028Z 1 [Note] A temporary password is generated for root@localhost: +i2yM_qvqtxj

4 修改密码,否则不能正常使用

[root@mysql01 ~]# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: 初始密码

The existing password for the user account root has expired. Please set a new password.

New password:  新密码

Re-enter new password: 再次输入新密码

Re-enter new password: 
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : 

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the.NETwork.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

5 登录mysql

[root@mysql01 ~]# mysql -uroot -p123qqq...A
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 6
Server version: 5.7.17 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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>

主从搭建

mysql01和mysql02完成以上数据库部署

1 mysql01主操作

[root@mysql01 ~]# vim /etc/my.cnf
log-bin=mysql-bin  # 启用二进制功能,主从复制基础
server-id=1        # id唯一
[root@mysql01 ~]# systemctl restart mysqld

2 验证配置是否生效

mysql> show variables like "server_id";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1     |  #为配置文件配置的server_id
+---------------+-------+
1 row in set (0.01 sec)
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |  #为on
+---------------+-------+
1 row in set (0.00 sec)
mysql> show variables like '%skip_networking%';
#skip_networking默认是OFF关闭状态,启用后主从将无法通信
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| skip_networking | OFF   |
+-----------------+-------+
1 row in set (0.00 sec)

3 mysql02从服务器操作

[root@mysql02 ~]# vim /etc/my.cnf
server-id=3  添加  
[root@mysql02 ~]# systemctl restart mysqld

4 验证配置是否生效

mysql> show variables like "server_id";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 3     |
+---------------+-------+
1 row in set (0.01 sec)

5 gitd实现主从同步

相比于传统的主从复制优点:不需要知道复制哪个文件,也不需要知道从哪个号开始复制 ​

​5.1 mysql01​

[root@mysql01 ~]# vim /etc/my.cnf
gtid_mode =ON
enforce-gtid-consistency=true
[root@mysql01 ~]# systemctl restart mysqld

mysql> grant replication slave on *.* to repl@'192.168.226.%' identified by '123qqq...A';
Query OK, 0 rows affected, 1 warning (0.00 sec)
#  为服务器创建一个连接账户并授予权限,*.*表示所有权限;192.168.226.%表示这个网段的所有用户都有这个权限

mysql> show master status;
+------------------+----------+--------------+------------------+----------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                      |
+------------------+----------+--------------+------------------+----------------------------------------+
| mysql-bin.000002 |      449 |              |                  | f02849a0-a141-11ec-a0a1-000c29a6904c:1 |
+------------------+----------+--------------+------------------+----------------------------------------+
1 row in set (0.00 sec)

5.2 mysql02

[root@mysql02 ~]# vim /etc/my.cnf
gtid_mode =ON
enforce-gtid-consistency=true
[root@mysql02 ~]# systemctl restart mysqld

mysql> change master to master_host='192.168.226.127',master_user='repl',master_password='123qqq...A',MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 2 warnings (0.09 sec)
# 改变slave服务器用于连接master服务器的参数,此处把MASTER值设为动态
mysql> start slave ;
mysql> show slave status G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.226.127
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 449
               Relay_Log_File: mysql02-relay-bin.000002
                Relay_Log_Pos: 662
        Relay_Master_Log_File: mysql-bin.000002
             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: 449
              Relay_Log_Space: 871
              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: f02849a0-a141-11ec-a0a1-000c29a6904c
             Master_Info_File: /var/lib/mysql/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: f02849a0-a141-11ec-a0a1-000c29a6904c:1
            Executed_Gtid_Set: f02849a0-a141-11ec-a0a1-000c29a6904c:1
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)
change master to master_host='192.168.226.127',master_user='repl',master_password='123qqq...A',master_log_file='mysql-bin.000002',master_log_pos=449;
不用gitd用这个命令

测试

mysql01

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> use test;

mysql> create table tese01(name varchar(10) not null, age varchar(5) not null);
Query OK, 0 rows affected (0.03 sec)

mysql> insert into tese01 values("tom","18");

mysql> select * from tese01;
+------+-----+
| name | age |
+------+-----+
| tom  | 18  |
+------+-----+
1 row in set (0.00 sec)

mysql02

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> use test;
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> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tese01         |
+----------------+
1 row in set (0.00 sec)

mysql> select * from tese01;
+------+-----+
| name | age |
+------+-----+
| tom  | 18  |
+------+-----+
1 row in set (0.01 sec)

mysql-proxy安装

mysql03

[root@mysql03 ~]# ls
anaconda-ks.cfg  mysql-5.7.17.tar  mysql-proxy-0.8.5-linux-debian6.0-x86-64bit.tar.gz
[root@mysql03 ~]# tar -zxvf mysql-proxy-0.8.5-linux-debian6.0-x86-64bit.tar.gz
[root@mysql03 ~]# mv mysql-proxy-0.8.5-linux-debian6.0-x86-64bit /usr/local/mysql-proxy
[root@mysql03 ~]# cd /usr/local/mysql-proxy/
[root@mysql03 mysql-proxy]# ls
bin  include  lib  libexec  licenses  share
[root@mysql03 mysql-proxy]# mkdir conf logs
[root@mysql03 mysql-proxy]# cd conf/
[root@mysql03 conf]# vim mysql-proxy.conf
[mysql-proxy]
 user=root   运行mysql-proxy用户
 proxy-address=0.0.0.0:3306  
 proxy-backend-addresses=172.25.44.1:3306 #指定后端主master写入数据
 proxy-read-only-backend-addresses=172.25.44.2:3306  #指定后端从slave读取数据
 proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua    #指定读写分离配置文件位置
 log-file=/usr/local/mysql-proxy/logs/mysql-proxy.log    #日志位置
 log-level=debug  #定义log日志级别,由高到低分别有(error|warning|info|message|debug)

[root@mysql03 conf]#chmod 660 /usr/local/mysql-proxy/conf/mysql-proxy.conf
[root@mysql03 conf]#mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf

测试

mysql01

mysql> grant  all privileges on *.* to root@'%' identified by'123qqq...A';

mysql04

[root@mysql04 ~]# yum install mysql -y
[root@mysql04 ~]# mysql -h 192.168.226.129 -uroot -p123qqq...A
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MySQL connection id is 14
Server version: 5.7.17-log MySQL Community Server (GPL)

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

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

MySQL [(none)]>

验证

mysql01数据库建库建表,在mysql04上可以查看到。

mysql03自动抓包

[root@mysql03 conf]# mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf
    server default db: 
    client default db: test
    syncronizing

原文链接:
https://www.tuicool.com/articles/6jmyQvi

声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>