mysql主从配置
第一步 配置主从
一、准备工作:
1.主从数据库版本最好一致
2.主从数据库内数据保持一致
主数据库:192.168.32.132 /linux
从数据库:192.168.32.133 /linux
二、主数据库master修改:
1.修改mysql配置
找到主数据库的配置文件my.cnf,我的在/etc/my.cnf,在[mysqld]部分插入如下两行:
log-bin=/data/mydata/log_bin/mysql_bin
server-id=1
注:路径根据自己MySQL的数据存放路径自定义。
2.重启mysql,创建用于同步的用户账号
创建用户并授权:user=xd password=123456
mysql> create user 'xd'@'192.168.32.133' identified by '123456';
Query OK, 0 rows affected (0.02 sec)
mysql> grant replication slave on *.* to 'xd'@'192.168.32.133';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
3.查看master状态,记录二进制文件名(mysql_bin.000002)和位置(775):
mysql> mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000002 | 775 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
三、从服务器slave修改:
1.修改mysql配置
同样找到my.cnf配置文件,添加server-id
[mysqld]
server-id=2 #设置server-id,必须唯一
2.重启mysql,打开mysql会话,执行同步SQL语句(需要主服务器主机名,登陆凭据,二进制文件的名称和位置):
mysql>
mysql>
mysql> change master to
-> master_host='192.168.32.132',
-> master_user='xd',
-> master_password='123456',
-> master_log_file='mysql_bin.000002',
-> master_log_pos=775;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
3.启动slave同步进程:
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
4.查看slave状态:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.32.132
Master_User: xd
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000002
Read_Master_Log_Pos: 775
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 320
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: 775
Relay_Log_Space: 531
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: 0f8c5e6b-2784-11eb-b1e9-000c294da724
Master_Info_File: /data/mydata/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.01 sec)
当Slave_IO_Running和Slave_SQL_Running都为YES的时候就表示主从同步设置成功了。接下来就可以进行一些验证了,比如在主master数据库的test数据库的一张表中插入一条数据,在slave的test库的相同数据表中查看是否有新增的数据即可验证主从复制功能是否有效,还可以关闭slave(mysql>stop slave;),然后再修改master,看slave是否也相应修改(停止slave后,master的修改不会同步到slave),就可以完成主从复制功能的验证了。