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


1 mysql01主操作[root@mysql01 ~]# vim /etc/my.cnflog-bin=mysql-bin# 启用二进制功能,主从复制基础server-id=1# id唯一[root@mysql01 ~]# systemctl restart mysqld2 验证配置是否生效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.cnfserver-id=3添加[root@mysql02 ~]# systemctl restart mysqld4 验证配置是否生效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.cnfgtid_mode =ONenforce-gtid-consistency=true[root@mysql01 ~]# systemctl restart mysqldmysql> 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.cnfgtid_mode =ONenforce-gtid-consistency=true[root@mysql02 ~]# systemctl restart mysqldmysql> 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 eventMaster_Host: 192.168.226.127Master_User: replMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000002Read_Master_Log_Pos: 449Relay_Log_File: mysql02-relay-bin.000002Relay_Log_Pos: 662Relay_Master_Log_File: mysql-bin.000002Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 449Relay_Log_Space: 871Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 1Master_UUID: f02849a0-a141-11ec-a0a1-000c29a6904cMaster_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set: f02849a0-a141-11ec-a0a1-000c29a6904c:1Executed_Gtid_Set: f02849a0-a141-11ec-a0a1-000c29a6904c:1Auto_Position: 1Replicate_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用这个命令测试mysql01mysql> 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)mysql02mysql> 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 namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> 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)


推荐阅读