1 为什么要主从复制
主从复制、读写分离一般是一起使用的。目的很简单,就是为了提高数据库的并发性能。你想,假设是单机,读写都在一台MySQL上面完成,性能肯定不高。如果有三台MySQL,一台mater只负责写操作,两台salve只负责读操作,性能就能大大提高了。
所以主从复制、读写分离就是为了数据库能支持更大的并发、提高数据库的可用性
。
2 主从复制步骤
2.1 环境准备
MySQL主机:
- IP地址:10.0.0.1
- 端口号:3306
- 版本:5.5
MySQL从机:
- IP地址:10.0.0.2
- 端口号:3306
- 版本:5.5
2.2 修改配置文件
MySQL主机:
[mysqld] log-bin=/home/mysql/binlog server-id=1 复制代码
MySQL从机:
[mysqld] server-id=2 log-bin=/home/mysql/binlog relay-log=/home/mysql/relaylog 复制代码
2.3 在MySQL中进行操作
首先重启两个MySQL
MySQL主机:
[root@iZ2ze4m2ri7irkf6h6n8zoZ mysql]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.62-log MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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> CREATE USER ymx IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec) mysql> grant replication slave on *.* to 'ymx'@'10.0.0.2' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> show master status; +---------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +---------------+----------+--------------+------------------+ | binlog.000003 | 594 | | | +---------------+----------+--------------+------------------+ 1 row in set (0.00 sec) 复制代码
MySQL从机:
[root@iZ1608aqb7ntn9Z ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.62-log MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | college_work | | db_chat | | db_stu_work_mg | | mysql | | performance_schema | | sysbooking | | test | | yn_db | +--------------------+ 9 rows in set (0.00 sec) mysql> change master to master_host='10.0.0.1',master_user='ymx',master_password='123456',master_port=3306,master_log_file='binlog.000003',master_log_pos=594; Query OK, 0 rows affected (0.01 sec)file='binlog.000003',master_log_pos=594; mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.1 Master_User: ymx Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000003 Read_Master_Log_Pos: 594 Relay_Log_File: relaylog.000002 Relay_Log_Pos: 250 Relay_Master_Log_File: binlog.000003 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: 594 Relay_Log_Space: 399 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 1 row in set (0.00 sec) 复制代码
2.4 测试
MySQL主机:
mysql> create database slave_db; Query OK, 1 row affected (0.00 sec) mysql> use slave_db; Database changed mysql> show tables; Empty set (0.00 sec) mysql> create table user( id int(20),name varchar(200)); Query OK, 0 rows affected (0.01 sec) mysql> show tables; +--------------------+ | Tables_in_slave_db | +--------------------+ | user | +--------------------+ 1 row in set (0.00 sec) 复制代码
MySQL从机:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | college_work | | db_chat | | db_stu_work_mg | | mysql | | performance_schema | | slave_db | | sysbooking | | test | | yn_db | +--------------------+ 10 rows in set (0.00 sec) mysql> use slave_db; Database changed mysql> show tables; Empty set (0.00 sec) mysql> show tables; +--------------------+ | Tables_in_slave_db | +--------------------+ | user | +--------------------+ 1 row in set (0.00 sec) 复制代码
3 MySQL主从复制原理
网络异常,图片无法展示
|