mysql主从复制模式

发布时间 2023-06-25 15:30:22作者: 往事已成昨天

1.尝试安装平台软件rpc远程调用模块,解决lib库不全和配置文件地址不对的问题,测试rpc程序运行是否正常。
2.尝试安装平台软件monitor模块,解决lib库不全和配置文件地址不对的问题,测试monitor模块运行是否正常。
change master to
master_host='192.168.3.111',
master_log_pos=3266,
master_log_file='master-bin.000001',
master_password='SlaveAdmin',
master_user='data_cp';

主从复制实例:

ubuntu1804 mysql主配置文件的目录:/etc/mysql/mysql.conf.d/
主配置文件:/etc/mysql/mysql.conf.d/mysqld.cnf
一:主mysql服务器的操作如下:
vim /etc/mysql/mysql.conf.d/mysqld.cnf

#[mysqld]
#pid-file = /var/run/mysqld/mysqld.pid
#socket = /var/run/mysqld/mysqld.sock
#datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
#bind-address = 0.0.0.0
log-bin=master-bin #如果版本是5.7,则添加此四行
server-id=201
validate_password_length=6
validate_password_policy=0
#############
如果是8.0版本,添加的是一下四行:
log-bin=master-bin #添加此四行
server-id=201
validate_password.length=6
validate_password.policy=0

[root@gitlab ~]# mysql -V
mysql Ver 8.0.33 for Linux on x86_64 (MySQL Community Server - GPL)

 

service mysql restart
主添加登陆账号及主从复制的权限
create user sync@'%' identified by '123456';
grant replication slave on *.* to sync@'%' ;
show master status
-> ;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| slave-bin.000001 | 1219 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

 

二: 从mysql服务器的配置如下:
vim /etc/mysql/mysql.conf.d/mysqld.cnf
server-id=111
service mysql restart

change master to
master_host='192.168.3.201',
master_log_pos=692,
master_log_file='master-bin.000004',
master_password='123456',
master_user='sync';


start slave
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.3.112
Master_User: sync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: slave-bin.000001
Read_Master_Log_Pos: 1219
Relay_Log_File: ubuntu19-relay-bin.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: slave-bin.000001
Slave_IO_Running: Yes #主要看这两个地方为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: 1219
Relay_Log_Space: 539
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: 112
Master_UUID: d109bff1-6344-11ed-8286-000c29e564cb
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica 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:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.01 sec)

ERROR:
No query specified


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
测试主从复制:
主服务器上面添加数据:
mysql> select * from employee;
+-------------+---------------+------------------+
| employee_id | employee_name | employee_contact |
+-------------+---------------+------------------+
| 1 | xiaoming | 11111111111 |
| 2 | jianjun | abc12345678 |
| 3 | 张三 | 222222222 |
| 4 | 张三 | 1234567890 |
+-------------+---------------+------------------+
4 rows in set (0.01 sec)

mysql> update employee set employee_name="李四" where employee_contact="1234567890"
-> ;
Query OK, 1 row affected (0.35 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from employee;
+-------------+---------------+------------------+
| employee_id | employee_name | employee_contact |
+-------------+---------------+------------------+
| 1 | xiaoming | 11111111111 |
| 2 | jianjun | abc12345678 |
| 3 | 张三 | 222222222 |
| 4 | 李四 | 1234567890 |
+-------------+---------------+------------------+
从服务器上面查看数据:
mysql> show tables;
+---------------+
| Tables_in_hr2 |
+---------------+
| employee |
+---------------+
1 row in set (0.01 sec)

mysql> select * from employee;
+-------------+---------------+------------------+
| employee_id | employee_name | employee_contact |
+-------------+---------------+------------------+
| 1 | xiaoming | 11111111111 |
| 2 | jianjun | abc12345678 |
| 3 | 张三 | 222222222 |
| 4 | 张三 | 1234567890 |
+-------------+---------------+------------------+
4 rows in set (0.00 sec)

mysql> select * from employee;
+-------------+---------------+------------------+
| employee_id | employee_name | employee_contact |
+-------------+---------------+------------------+
| 1 | xiaoming | 11111111111 |
| 2 | jianjun | abc12345678 |
| 3 | 张三 | 222222222 |
| 4 | 李四 | 1234567890 |
+-------------+---------------+------------------+
4 rows in set (0.00 sec)