主库更新,从库会同步更新。从库更新,主库一般是不会同步更新的,如果发生主库也同步更新,可能出现短暂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)
– 主库需要创建一个从账户并分配可以读取binlog日志的权限 - 在从mysql中配置中继日志文件,用来保存读取到的mysql主的 binlog 日志
– 从库可以开启主从复制,从指定的主库的binlog文件中加载日志缓存到自己的relaylog文件中,最后通过一个sql线程将relaylog文件中的日志replay到自己的库表中
– 从库需要使用主库提供的账号和主库的binlog文件建立连接
3.1、创建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状态:
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状态:
[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、主机中创建slave用户:
-- 创建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主机上创建mydb1
此时刷新3316从数据库,发现没有mydb1
7、在3306主机上创建mydb2
此时刷新3316从数据库,发现从机复制了主机中的mydb2数据库到从机中
8、在3306主机上创建mydb3
9、在3306主机上创建mydb4
到此这篇关于mysql搭建主从复制的实现步骤的文章就介绍到这了,更多相关mysql搭建主从复制内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论