mysql搭建主從復制的實現步驟
主庫更新,從庫會同步更新。從庫更新,主庫一般是不會同步更新的,如果發(fā)生主庫也同步更新,可能出現短暫bug,或者主從配置有問題。
mysql集群:
單臺設備的負載壓力:主從復制
集群:分攤訪問壓力和存儲壓力
需求:使用 3306
mysql當作主, 3316
mysql 當作從,在3306中對 mydb2/mydb3
數據庫所有的操作,希望能夠主從復制同步到3316,其他的數據庫操作不同步。
1、準備主服務器
docker run -d \ --name spzx-mysql \ -p 3306:3306 \ -v mysql_data:/var/lib/mysql \ -v mysql_conf:/etc/mysql \ --restart=always \ --privileged=true \ -e MYSQL_ROOT_PASSWORD=123456 \ mysql:8
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS ab66508d9441 mysql:8 "docker-entrypoint.s…" 8 months ago Up 9 days 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp spzx-mysql
此時我已經有一個主服務器 spzx-mysql
2、準備從服務器
docker run -d \ -p 3316:3306 \ -v mysql-slave1-conf:/etc/mysql/conf.d \ -v mysql-slave1-data:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=123456 \ --name atguigu-mysql-slave1 \ mysql:8
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c236f876ae40 mysql:8 "docker-entrypoint.s…" 10 seconds ago Up 3 seconds 33060/tcp, 0.0.0.0:3316->3306/tcp, :::3316->3306/tcp atguigu-mysql-slave1 ab66508d9441 mysql:8 "docker-entrypoint.s…" 8 months ago Up 9 days 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp spzx-mysql
3、主庫配置
- 先在主mysql中配置 記錄mydb2/mydb3庫的操作日志到binlog日志文件中
– 主庫寫操作會按照配置記錄到二進制文件中(binlog)
– 主庫需要創(chuàng)建一個從賬戶并分配可以讀取binlog日志的權限 - 在從mysql中配置中繼日志文件,用來保存讀取到的mysql主的 binlog 日志
– 從庫可以開啟主從復制,從指定的主庫的binlog文件中加載日志緩存到自己的relaylog文件中,最后通過一個sql線程將relaylog文件中的日志replay到自己的庫表中
– 從庫需要使用主庫提供的賬號和主庫的binlog文件建立連接
3.1、創(chuàng)建MySQL主服務器配置文件:
[root@localhost ~]# docker inspect spzx-mysql
"Mounts": [ { "Type": "volume", "Name": "mysql_conf", "Source": "/var/lib/docker/volumes/mysql_conf/_data", "Destination": "/etc/mysql", "Driver": "local", "Mode": "z", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "mysql_data", "Source": "/var/lib/docker/volumes/mysql_data/_data", "Destination": "/var/lib/mysql", "Driver": "local", "Mode": "z", "RW": true, "Propagation": "" } ],
[root@localhost _data]# cd /var/lib/docker/volumes/mysql_conf/_data [root@localhost _data]# ll 總用量 8 drwxrwxr-x. 2 root root 41 12月 26 2023 conf.d -rw-rw-r--. 1 root root 1080 12月 21 2021 my.cnf -rw-r--r--. 1 root root 1448 9月 28 2021 my.cnf.fallback
[root@localhost _data]# vim my.cnf
配置如下內容:
[mysqld] # 服務器唯一id,默認值1 server-id=1 # 設置日志格式,默認值ROW。row(記錄行數據) statement(記錄sql) mixed(混合模式) binlog_format=STATEMENT # 二進制日志名,默認binlog # log-bin=binlog log-bin=spzxbinlog # 設置需要復制的數據庫,默認復制全部數據庫 binlog-do-db=mydb2 binlog-do-db=mydb3 # 設置不需要復制的數據庫 binlog-ignore-db=mydb4 #binlog-ignore-db=infomation_schema
[root@localhost _data]# docker restart spzx-mysql spzx-mysql
[root@localhost _data]# ll ../../mysql_data/_data/
4、從庫配置
[root@localhost _data]# docker inspect atguigu-mysql-slave1
vim /var/lib/docker/volumes/mysql-slave1-conf/_data/my.cnf
配置如下內容:
[mysqld] # 服務器唯一id,每臺服務器的id必須不同,如果配置其他從機,注意修改id server-id=2 # 中繼日志名,默認xxxxxxxxxxxx-relay-bin #relay-log=relay-bin
[root@localhost _data]# docker restart atguigu-mysql-slave1 atguigu-mysql-slave1
5、搭建主從&測試
5.1、使用命令行登錄MySQL主服務器
[root@localhost _data]# docker exec -it spzx-mysql /bin/bash root@ab66508d9441:/# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.27 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. 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.
5.2、主機中查詢master狀態(tài):
mysql> show master status; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | spzxbinlog.000001 | 156 | mydb2,mydb3 | mydb4 | | +-------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.01 sec)
5.3、從機中查詢slave狀態(tài):
[root@localhost ~]# docker exec -it atguigu-mysql-slave1 /bin/bash ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) root@c236f876ae40:/# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.0.27 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. 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>
mysql> show slave status; Empty set, 1 warning (0.02 sec)
從庫必須和主庫主動建立連接 開啟自己的sql和io線程
5.4、主機中創(chuàng)建slave用戶:
-- 創(chuàng)建slave用戶 CREATE USER 'atguigu_slave'@'%'; -- 設置密碼 ALTER USER 'atguigu_slave'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; -- 授予復制權限 GRANT REPLICATION SLAVE ON *.* TO 'atguigu_slave'@'%'; -- 刷新權限 FLUSH PRIVILEGES;
mysql> show master status; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | spzxbinlog.000001 | 1074 | mydb2,mydb3 | mydb4 | | +-------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
5.5、在從機上配置主從關系:
CHANGE MASTER TO MASTER_HOST='192.168.74.148', MASTER_USER='atguigu_slave',MASTER_PASSWORD='123456', MASTER_PORT=3306, MASTER_LOG_FILE='spzxbinlog.000001',MASTER_LOG_POS=1074;
mysql> CHANGE MASTER TO MASTER_HOST='192.168.74.148', -> MASTER_USER='atguigu_slave',MASTER_PASSWORD='123456', MASTER_PORT=3306, -> MASTER_LOG_FILE='spzxbinlog.000001',MASTER_LOG_POS=1074; Query OK, 0 rows affected, 9 warnings (0.05 sec)
mysql> show slave status; +----------------+----------------+---------------+-------------+---------------+-------------------+---------------------+-------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+-------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+-------------------+ | Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version | Master_public_key_path | Get_master_public_key | Network_Namespace | +----------------+----------------+---------------+-------------+---------------+-------------------+---------------------+-------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+-------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+-------------------+ | | 192.168.74.148 | atguigu_slave | 3306 | 60 | spzxbinlog.000001 | 1074 | c236f876ae40-relay-bin.000001 | 4 | spzxbinlog.000001 | No | No | | | | | | | 0 | | 0 | 1074 | 156 | None | | 0 | No | | | | | | NULL | No | 0 | | 0 | | | 0 | | mysql.slave_master_info | 0 | NULL | | 86400 | | | | | | | | 0 | | | | | 0 | | +----------------+----------------+---------------+-------------+---------------+-------------------+---------------------+-------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+-------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+-------------------+ 1 row in set, 1 warning (0.00 sec)
5.6、啟動從庫的io和sql線程:都啟動成功主從才搭建成功
mysql> start slave; Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> show slave status; +----------------------------------+----------------+---------------+-------------+---------------+-------------------+---------------------+-------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+----------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+-------------------+ | Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version | Master_public_key_path | Get_master_public_key | Network_Namespace | +----------------------------------+----------------+---------------+-------------+---------------+-------------------+---------------------+-------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+----------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+-------------------+ | Waiting for source to send event | 192.168.74.148 | atguigu_slave | 3306 | 60 | spzxbinlog.000001 | 1074 | c236f876ae40-relay-bin.000002 | 325 | spzxbinlog.000001 | Yes | Yes | | | | | | | 0 | | 0 | 1074 | 541 | None | | 0 | No | | | | | | 0 | No | 0 | | 0 | | | 1 | af98f4d4-a3ca-11ee-b194-0242ac110002 | mysql.slave_master_info | 0 | NULL | Replica has read all relay log; waiting for more updates | 86400 | | | | | | | | 0 | | | | | 0 | | +----------------------------------+----------------+---------------+-------------+---------------+-------------------+---------------------+-------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+----------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+-------------------+ 1 row in set, 1 warning (0.01 sec)
6、在3306主機上創(chuàng)建mydb1
此時刷新3316從數據庫,發(fā)現沒有mydb1
7、在3306主機上創(chuàng)建mydb2
此時刷新3316從數據庫,發(fā)現從機復制了主機中的mydb2數據庫到從機中
8、在3306主機上創(chuàng)建mydb3
9、在3306主機上創(chuàng)建mydb4
到此這篇關于mysql搭建主從復制的實現步驟的文章就介紹到這了,更多相關mysql搭建主從復制內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MYSQL的REPLACE和ON DUPLICATE KEY UPDATE語句介紹解決問題實例
這篇文章主要介紹了MYSQL的REPLACE和ON DUPLICATE KEY UPDATE語句介紹解決問題實例,需要的朋友可以參考下2014-04-04ubuntu 16.04下mysql5.7.17開放遠程3306端口
這篇文章主要介紹了ubuntu 16.04下mysql5.7.17開放遠程3306端口的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01