Mysql主从
MYSQL一主一从
实现环境:
主机 | IP | Mysql版本 | 作用 |
Mysql-master | 192.168.1.250 | 5.7.20 | 主 |
Mysql-test01 | 192.168.1.251 | 5.7.20 | 从 |
创建相同的数据库,表名。根据实际需要来建立;
mysql> show databases;
+--------------------+
|Database |
+--------------------+
|information_schema |
|book |
|mysql |
|performance_schema |
|sys |
+--------------------+
5 rows inset (0.05 sec)
mysql> create database HA;
Query OK,1 row affected (0.00 sec)
mysql> use HA;
Databasechanged
mysql> create table T1 (id int,name varchar(40));
Query OK,0 rows affected (0.04 sec)
分别在主和从都执行上面的命令
Mysql-master 主库配置
授权:
mysql>mysql replication slave on *.* to test@'192.168.1.%' identified by'Lzq690813425@163.com';
编辑my.cnf 配置文件:
在最后一行加入以下参数
#开启二进制日志
log-bin=mysql-bin-master
#本机数据库日志ID(唯一)
server-id=1
#允许同步的库
binlog-do-db=HA
#拒绝同步的库
binlog-ignore-db=mysql
配置完成后重启数据库服务
[root@mysql-master~]# systemctl restart mysqld(如果起不来,就说明配置文件出现错误)
mysql> showmaster status \G
***************************1. row ***************************
File: mysql-bin-master.000001
Position: 154
Binlog_Do_DB: HA
Binlog_Ignore_DB: mysql
Executed_Gtid_Set:
1 row inset (0.00 sec)
查看master状态
Mysql-test01从库配置
测试主库上授权账户能否正常登陆:
[root@mysql-test01~]# mysql -utest -pLzq690813425@163.com -h 192.168.1.250
编辑my.cnf配置文件:
[root@mysql-test01~]# vim /etc/my.cnf
#从数据库ID(唯一)
server-id=2
#在配置文件最后加入server-id=2这一行内容
登陆本机数据库授权主账户同步信息:
[root@mysql-test01~]# mysql -uroot -pLzq690813425@163.com
mysql:[Warning] Using a password on the command line interface can be insecure.
Welcometo the MySQL monitor. Commands end with; or \g.
YourMySQL connection id is 3
Serverversion: 5.7.20 MySQL Community Server (GPL)
Copyright(c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle isa 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> stopslave;
Query OK,0 rows affected, 1 warning (0.00 sec)
设置主服务授权账户密码:
mysql> startslave;
Query OK,0 rows affected (0.01 sec)
mysql> showslave status \G
***************************1. row ***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 192.168.1.250
Master_User: test
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:mysql-bin-master.000001
Read_Master_Log_Pos: 154
Relay_Log_File:mysql-test01-relay-bin.000002
Relay_Log_Pos: 381
Relay_Master_Log_File:mysql-bin-master.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: 595
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:b4c2852b-4abc-11e8-94be-000c295029d1
Master_Info_File:/var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has readall 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 inset (0.00 sec)
查看slave状态是否正常重点参数为yes说明搭建成功
验证架构是否成功
登陆主数据库,在主数据库操作:
[root@mysql-master ~]# mysql -uroot-pLzq690813425@163.com
mysql> show processlist \G
***************************1. row ***************************
Id: 3
User: root
Host: localhost
db: NULL
Command:Query
Time: 0
State: starting
Info: show processlist
***************************2. row ***************************
Id: 5
User: test
Host: 192.168.1.251:58530
db: NULL
Command:Binlog Dump
Time: 580
State: Master has sent all binlog to slave; waitingfor more updates
Info: NULL
2 rows inset (0.00 sec)
mysql> use HA;
Readingtable information for completion of table and column names
You canturn off this feature to get a quicker startup with -A
Databasechanged
mysql> insert into T1values (1,'lizongqun');
Query OK,1 row affected (0.01 sec)
mysql> insert into T1values (2,'wuhongjing');
Query OK,1 row affected (0.01 sec)
mysql> select * from T1 \G
*************************** 1.row ***************************
id: 1
name: lizongqun
*************************** 2.row ***************************
id: 2
name: wuhongjing
2 rows in set (0.00 sec)
主库插入数据后再去从库查看:
[root@mysql-test01 ~]# mysql -uroot-pLzq690813425@163.com
mysql> selectdatabase();
+------------+
|database() |
+------------+
| NULL |
+------------+
1 row inset (0.00 sec)
mysql> use HA;
Readingtable information for completion of table and column names
You canturn off this feature to get a quicker startup with -A
Databasechanged
mysql> showtables;
+--------------+
| Tables_in_HA|
+--------------+
| T1 |
+--------------+
1 row inset (0.00 sec)
mysql> select * from T1 \G
*************************** 1. row ***************************
id: 1
name: lizongqun
*************************** 2. row ***************************
id: 2
name: wuhongjing
2 rows in set (0.00 sec)
黄底黑色部分信息一直,表示主服务器上在HA库插入的数据从库都能查询出来。