From d64571c6545f3a796fcd5b2259001eb5fa99a4db Mon Sep 17 00:00:00 2001 From: tu <2843180578@qq.com> Date: Wed, 29 May 2024 10:20:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20mysql-MD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mysql-MD/Mysql常见配置.md | 173 +++ mysql-MD/第七章:读写分离.md | 521 ++++++++ mysql-MD/第二章:数据库管理及数据类型.md | 861 +++++++++++++ mysql-MD/第六章:主从复制及读写分离.md | 1501 ++++++++++++++++++++++ 4 files changed, 3056 insertions(+) create mode 100644 mysql-MD/Mysql常见配置.md create mode 100644 mysql-MD/第七章:读写分离.md create mode 100644 mysql-MD/第二章:数据库管理及数据类型.md create mode 100644 mysql-MD/第六章:主从复制及读写分离.md diff --git a/mysql-MD/Mysql常见配置.md b/mysql-MD/Mysql常见配置.md new file mode 100644 index 0000000..d4130b2 --- /dev/null +++ b/mysql-MD/Mysql常见配置.md @@ -0,0 +1,173 @@ +

Mysql常见配置

+ +作者:行癫(盗版必究) + +------ + +``` +back_log = 600 +#在MYSQL暂时停止响应新请求之前,短时间内的多少个请求可以被存在堆栈中。如果系统在短时间内有很多连接,则需要增大该参数的值,该参数值指定到来的TCP/IP连接的监听队列的大小。默认值50。 + +max_connections = 3000 +#MySQL允许最大的进程连接数,如果经常出现Too Many Connections的错误提示,则需要增大此值。 + +max_connect_errors = 6000 +#设置每个主机的连接请求异常中断的最大次数,当超过该次数,MYSQL服务器将禁止host的连接请求,直到mysql服务器重启或通过flush hosts命令清空此host的相关信息。 + +table_cache = 614 +#指示表调整缓冲区大小。# table_cache 参数设置表高速缓存的数目。每个连接进来,都会至少打开一个表缓存。#因此, table_cache 的大小应与 max_connections 的设置有关。例如,对于 200 个#并行运行的连接,应该让表的缓存至少有 200 × N ,这里 N 是应用可以执行的查询#的一个联接中表的最大数量。此外,还需要为临时表和文件保留一些额外的文件描述符。 +# 当 Mysql 访问一个表时,如果该表在缓存中已经被打开,则可以直接访问缓存;如果#还没有被缓存,但是在 Mysql 表缓冲区中还有空间,那么这个表就被打开并放入表缓#冲区;如果表缓存满了,则会按照一定的规则将当前未用的表释放,或者临时扩大表缓存来存放,使用表缓存的好处是可以更快速地访问表中的内容。执行 flush tables 会#清空缓存的内容。一般来说,可以通过查看数据库运行峰值时间的状态值 Open_tables #和 Opened_tables ,判断是否需要增加 table_cache 的值(其中 open_tables 是当#前打开的表的数量, Opened_tables 则是已经打开的表的数量)。即如果open_tables接近table_cache的时候,并且Opened_tables这个值在逐步增加,那就要考虑增加这个#值的大小了。还有就是Table_locks_waited比较高的时候,也需要增加table_cache。 + + +external-locking = FALSE +#使用–skip-external-locking MySQL选项以避免外部锁定。该选项默认开启 + +max_allowed_packet = 32M +#设置在网络传输中一次消息传输量的最大值。系统默认值 为1MB,最大值是1GB,必须设置1024的倍数。 + +sort_buffer_size = 2M +# Sort_Buffer_Size 是一个connection级参数,在每个connection(session)第一次需要使用这个buffer的时候,一次性分配设置的内存。 +#Sort_Buffer_Size 并不是越大越好,由于是connection级的参数,过大的设置+高并发可能会耗尽系统内存资源。例如:500个连接将会消耗 500*sort_buffer_size(8M)=4G内存 +#Sort_Buffer_Size 超过2KB的时候,就会使用mmap() 而不是 malloc() 来进行内存分配,导致效率降低。 +#技术导读 http://blog.webshuo.com/2011/02/16/mysql-sort_buffer_size/ +#dev-doc: http://dev.mysql.com/doc/refman/5.5/en/server-parameters.html +#explain select*from table where order limit;出现filesort +#属重点优化参数 + +join_buffer_size = 2M +#用于表间关联缓存的大小,和sort_buffer_size一样,该参数对应的分配内存也是每个连接独享。 + +thread_cache_size = 300 +# 服务器线程缓存这个值表示可以重新利用保存在缓存中线程的数量,当断开连接时如果缓存中还有空间,那么客户端的线程将被放到缓存中,如果线程重新被请求,那么请求将从缓存中读取,如果缓存中是空的或者是新的请求,那么这个线程将被重新创建,如果有很多新的线程,增加这个值可以改善系统性能.通过比较 Connections 和 Threads_created 状态的变量,可以看到这个变量的作用。设置规则如下:1GB 内存配置为8,2GB配置为16,3GB配置为32,4GB或更高内存,可配置更大。 + +thread_concurrency = 8 +# 设置thread_concurrency的值的正确与否, 对mysql的性能影响很大, 在多个cpu(或多核)的情况下,错误设置了thread_concurrency的值, 会导致mysql不能充分利用多cpu(或多核), 出现同一时刻只能一个cpu(或核)在工作的情况。thread_concurrency应设为CPU核数的2倍. 比如有一个双核的CPU, 那么thread_concurrency的应该为4; 2个双核的cpu, thread_concurrency的值应为8 +#属重点优化参数 + +query_cache_size = 64M +## 对于使用MySQL的用户,对于这个变量大家一定不会陌生。前几年的MyISAM引擎优化中,这个参数也是一个重要的优化参数。但随着发展,这个参数也爆露出来一些问题。机器的内存越来越大,人们也都习惯性的把以前有用的参数分配的值越来越大。这个参数加大后也引发了一系列问题。我们首先分析一下 query_cache_size的工作原理:一个SELECT查询在DB中工作后,DB会把该语句缓存下来,当同样的一个SQL再次来到DB里调用时,DB在该表没发生变化的情况下把结果从缓存中返回给Client。这里有一个关建点,就是DB在利用Query_cache工作时,要求该语句涉及的表在这段时间内没有发生变更。那如果该表在发生变更时,Query_cache里的数据又怎么处理呢?首先要把Query_cache和该表相关的语句全部置为失效,然后在写入更新。那么如果Query_cache非常大,该表的查询结构又比较多,查询语句失效也慢,一个更新或是Insert就会很慢,这样看到的就是Update或是Insert怎么这么慢了。所以在数据库写入量或是更新量也比较大的系统,该参数不适合分配过大。而且在高并发,写入量大的系统,建议把该功能禁掉。 +#重点优化参数(主库 增删改-MyISAM) + +query_cache_limit = 4M +#指定单个查询能够使用的缓冲区大小,缺省为1M + +query_cache_min_res_unit = 2k +#默认是4KB,设置值大对大数据查询有好处,但如果你的查询都是小数据查询,就容易造成内存碎片和浪费 +#查询缓存碎片率 = Qcache_free_blocks / Qcache_total_blocks * 100% +#如果查询缓存碎片率超过20%,可以用FLUSH QUERY CACHE整理缓存碎片,或者试试减小query_cache_min_res_unit,如果你的查询都是小数据量的话。 +#查询缓存利用率 = (query_cache_size – Qcache_free_memory) / query_cache_size * 100% +#查询缓存利用率在25%以下的话说明query_cache_size设置的过大,可适当减小;查询缓存利用率在80%以上而且Qcache_lowmem_prunes > 50的话说明query_cache_size可能有点小,要不就是碎片太多。 +#查询缓存命中率 = (Qcache_hits – Qcache_inserts) / Qcache_hits * 100% + +default-storage-engine = MyISAM +#default_table_type = InnoDB + +thread_stack = 192K +#设置MYSQL每个线程的堆栈大小,默认值足够大,可满足普通操作。可设置范围为128K至4GB,默认为192KB。 + +transaction_isolation = READ-COMMITTED +# 设定默认的事务隔离级别.可用的级别如下: +# READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, SERIALIZABLE +# 1.READ UNCOMMITTED-读未提交2.READ COMMITTE-读已提交3.REPEATABLE READ -可重复读4.SERIALIZABLE -串行 + +tmp_table_size = 256M +# tmp_table_size 的默认大小是 32M。如果一张临时表超出该大小,MySQL产生一个 The table tbl_name is full 形式的错误,如果你做很多高级 GROUP BY 查询,增加 tmp_table_size 值。如果超过该值,则会将临时表写入磁盘。 +max_heap_table_size = 256M +long_query_time = 2 +log_long_format +log-slow-queries=/data/3306/slow-log.log +#log-bin = /data/3306/mysql-bin +log-bin +binlog_cache_size = 4M +max_binlog_cache_size = 8M +max_binlog_size = 512M + +expire_logs_days = 7 +key_buffer_size = 2048M +#批定用于索引的缓冲区大小,增加它可以得到更好的索引处理性能,对于内存在4GB左右的服务器来说,该参数可设置为256MB或384MB。 + +read_buffer_size = 1M +# MySql读入缓冲区大小。对表进行顺序扫描的请求将分配一个读入缓冲区,MySql会为它分配一段内存缓冲区。read_buffer_size变量控制这一缓冲区的大小。如果对表的顺序扫描请求非常频繁,并且你认为频繁扫描进行得太慢,可以通过增加该变量值以及内存缓冲区大小提高其性能。和sort_buffer_size一样,该参数对应的分配内存也是每个连接独享。 + +read_rnd_buffer_size = 16M +# MySql的随机读(查询操作)缓冲区大小。当按任意顺序读取行时(例如,按照排序顺序),将分配一个随机读缓存区。进行排序查询时,MySql会首先扫描一遍该缓冲,以避免磁盘搜索,提高查询速度,如果需要排序大量数据,可适当调高该值。但MySql会为每个客户连接发放该缓冲空间,所以应尽量适当设置该值,以避免内存开销过大。 + +bulk_insert_buffer_size = 64M +#批量插入数据缓存大小,可以有效提高插入效率,默认为8M + +myisam_sort_buffer_size = 128M +# MyISAM表发生变化时重新排序所需的缓冲 + +myisam_max_sort_file_size = 10G +# MySQL重建索引时所允许的最大临时文件的大小 (当 REPAIR, ALTER TABLE 或者 LOAD DATA INFILE). +# 如果文件大小比此值更大,索引会通过键值缓冲创建(更慢) + +myisam_max_extra_sort_file_size = 10G +myisam_repair_threads = 1 +# 如果一个表拥有超过一个索引, MyISAM 可以通过并行排序使用超过一个线程去修复他们. +# 这对于拥有多个CPU以及大量内存情况的用户,是一个很好的选择. + +myisam_recover +#自动检查和修复没有适当关闭的 MyISAM 表 +skip-name-resolve +lower_case_table_names = 1 + +server-id = 1 + +innodb_additional_mem_pool_size = 16M +#这个参数用来设置 InnoDB 存储的数据目录信息和其它内部数据结构的内存池大小,类似于Oracle的library cache。这不是一个强制参数,可以被突破。 + +innodb_buffer_pool_size = 2048M +# 这对Innodb表来说非常重要。Innodb相比MyISAM表对缓冲更为敏感。MyISAM可以在默认的 key_buffer_size 设置下运行的可以,然而Innodb在默认的 innodb_buffer_pool_size 设置下却跟蜗牛似的。由于Innodb把数据和索引都缓存起来,无需留给操作系统太多的内存,因此如果只需要用Innodb的话则可以设置它高达 70-80% 的可用内存。一些应用于 key_buffer 的规则有 — 如果你的数据量不大,并且不会暴增,那么无需把 innodb_buffer_pool_size 设置的太大了 + +innodb_data_file_path = ibdata1:1024M:autoextend +#表空间文件 重要数据 + +innodb_file_io_threads = 4 +#文件IO的线程数,一般为 4,但是在 Windows 下,可以设置得较大。 + +innodb_thread_concurrency = 8 +#服务器有几个CPU就设置为几,建议用默认设置,一般为8. + +innodb_flush_log_at_trx_commit = 2 +# 如果将此参数设置为1,将在每次提交事务后将日志写入磁盘。为提供性能,可以设置为0或2,但要承担在发生故障时丢失数据的风险。设置为0表示事务日志写入日志文件,而日志文件每秒刷新到磁盘一次。设置为2表示事务日志将在提交时写入日志,但日志文件每次刷新到磁盘一次。 + +innodb_log_buffer_size = 16M +#此参数确定些日志文件所用的内存大小,以M为单位。缓冲区更大能提高性能,但意外的故障将会丢失数据.MySQL开发人员建议设置为1-8M之间 + +innodb_log_file_size = 128M +#此参数确定数据日志文件的大小,以M为单位,更大的设置可以提高性能,但也会增加恢复故障数据库所需的时间 + +innodb_log_files_in_group = 3 +#为提高性能,MySQL可以以循环方式将日志文件写到多个文件。推荐设置为3M + +innodb_max_dirty_pages_pct = 90 +#推荐阅读 http://www.taobaodba.com/html/221_innodb_max_dirty_pages_pct_checkpoint.html +# Buffer_Pool中Dirty_Page所占的数量,直接影响InnoDB的关闭时间。参数innodb_max_dirty_pages_pct 可以直接控制了Dirty_Page在Buffer_Pool中所占的比率,而且幸运的是innodb_max_dirty_pages_pct是可以动态改变的。所以,在关闭InnoDB之前先将innodb_max_dirty_pages_pct调小,强制数据块Flush一段时间,则能够大大缩短 MySQL关闭的时间。 + +innodb_lock_wait_timeout = 120 +# InnoDB 有其内置的死锁检测机制,能导致未完成的事务回滚。但是,如果结合InnoDB使用MyISAM的lock tables 语句或第三方事务引擎,则InnoDB无法识别死锁。为消除这种可能性,可以将innodb_lock_wait_timeout设置为一个整数值,指示 MySQL在允许其他事务修改那些最终受事务回滚的数据之前要等待多长时间(秒数) + +innodb_file_per_table = 0 +#独享表空间(关闭) + +[mysqldump] +quick +max_allowed_packet = 32M + +[mysqld_safe] +log-error=/data/3306/mysql_oldboy.err +pid-file=/data/3306/mysqld.pid + + + +#补充 +#wait_timeout = 10 +#指定一个请求的最大连接时间,对于4GB左右的内存服务器来说,可以将其设置为5-10。 +#skip_networking +#开启该选可以彻底关闭MYSQL的TCP/IP连接方式,如果WEB服务器是以远程连接的方式访问MYSQL数据库服务器的,则不要开启该选项,否则将无法正常连接。 + +#log-queries-not-using-indexes +将没有使用索引的查询也记录下来 +``` + diff --git a/mysql-MD/第七章:读写分离.md b/mysql-MD/第七章:读写分离.md new file mode 100644 index 0000000..767d910 --- /dev/null +++ b/mysql-MD/第七章:读写分离.md @@ -0,0 +1,521 @@ +

读写分离

+ +**作者:行癫(盗版必究)** + +------ + +## 一:读写分离部署 + +#### 1.环境介绍 + +![image-20221006131313211](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20221006131313211.png) + +#### 2.读写分离集群部署 + +##### A:数据库集群部署(略) + +​ 单主单从;多主多从等均可 + +##### B:Mycat部署 + +​ 新机器,不需要安装mysql + +安装jdk环境: + +```shell +[root@mycat ~]# tar xf jdk-8u211-linux-x64.tar.gz -C /usr/local/ +[root@mycat ~]# mv /usr/local/jdk1.8.0_211/ /usr/local/java +``` + +设置环境变量: + +```shell +[root@mycat ~]# vi /etc/profile +JAVA_HOME=/usr/local/java +PATH=$JAVA_HOME/bin:$PATH +export JAVA_HOME PATH +[root@mycat ~]# source /etc/profile +[root@mycat ~]# java -version +java version "1.8.0_211" +Java(TM) SE Runtime Environment (build 1.8.0_211-b12) +Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode) +``` + +安装mycat: + +```shell +[root@mycat ~]# tar xf Mycat-server-1.6.7.1-release-20190627191042-linux.tar.gz -C /usr/local/ +``` + +设置环境变量: + +```shell +[root@mycat ~]# vi ~/.bash_profile +PATH=$PATH:$HOME/bin:/usr/local/mycat/bin +[root@mycat ~]# source ~/.bash_profile +``` + +##### C:数据库部署 + +Mysql中添加数据库和账户: + +```shell +[root@master-1 ~]# mysql -u root -pQianFeng@123 +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 10 +Server version: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> create database shop; +Query OK, 1 row affected (0.00 sec) + +mysql> create database bbs; +Query OK, 1 row affected (0.00 sec) + +mysql> create database blog; +Query OK, 1 row affected (0.00 sec) + +mysql> grant all on shop.* to shop@'%' identified by 'QianFeng@123'; +Query OK, 0 rows affected, 1 warning (0.00 sec) + +mysql> grant all on bbs.* to bbs@'%' identified by 'QianFeng@123'; +Query OK, 0 rows affected, 1 warning (0.00 sec) + +mysql> grant all on blog.* to blog@'%' identified by 'QianFeng@123'; +Query OK, 0 rows affected, 1 warning (0.00 sec) +``` + +注意: + +​ 创建的库跟mycat关联,原则上一个库对应一个项目,根据实际情况创建 + +​ 创建的库需要单独授权用户进行管理,用户名和库名根据实际情况决定 + +##### D:Mycat配置 + +​ server.xml:Mycat的配置文件,设置账号、参数等 + +```shell +[root@mycat conf]# vim server.xml + + + + + 0 + 1 + 0 + 0 + 2 + (?:(\s*next\s+value\s+for\s*MYCATSEQ_(\w+))(,|\)|\s)*)+ + false + 0 + 0 + 0 + 64k + 1k + 0 + 384m + false + false + true + + + 123456 + TESTDB + + + 123456 + shop + + + 123456 + bbs + + + 123456 + blog + + +``` + +注意: + +```shell +user 用户配置节点 +–name 登录的用户名,也就是连接Mycat的用户名 +–password 登录的密码,也就是连接Mycat的密码 +–schemas 数据库名,这里会和schema.xml中的配置关联,多个用逗号分开,例如需要这个用户需要管理两个数据库db1,db2,则配置db1,db2 +``` + +​ schema.xml:Mycat对应的物理数据库和数据库表的配置 + +```shell +[root@mycat conf]# cat schema.xml + + + + + + + + + + + + show status like 'wsrep%' + + + + + + + + + + + show status like 'wsrep%' + + + + + + + + + + + show status like 'wsrep%' + + + + + + + + + + +``` + +注意: + +```shell +balance=1 开启读写分离机制,所有读操作都发送到当前备用的 writeHost 上。 +wirteType=0 所有写操作发送到第一个writeHost,第一个挂了切换到第二个 +switchType=3 基于MySQL Galera cluster的切换机制,心跳语句为show status like 'wsrep%' +``` + +##### E:启动服务 + +```shell +[root@mycat conf]# mycat start +Starting Mycat-server... +[root@mycat conf]# jps +1494 WrapperSimpleApp +1528 Jps +``` + +查看端口: + +```shell +[root@mycat conf]# ss -antpl +State Recv-Q Send-Q Local Address:Port Peer Address:Port +LISTEN 0 128 *:22 *:* users:(("sshd",pid=837,fd=3)) +LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=933,fd=13)) +LISTEN 0 1 127.0.0.1:32000 *:* users:(("java",pid=1494,fd=4)) +LISTEN 0 50 :::37680 :::* users:(("java",pid=1494,fd=57)) +LISTEN 0 128 :::22 :::* users:(("sshd",pid=837,fd=4)) +LISTEN 0 100 ::1:25 :::* users:(("master",pid=933,fd=14)) +LISTEN 0 50 :::1984 :::* users:(("java",pid=1494,fd=58)) +LISTEN 0 100 :::8066 :::* users:(("java",pid=1494,fd=79)) +LISTEN 0 50 :::32834 :::* users:(("java",pid=1494,fd=59)) +LISTEN 0 100 :::9066 :::* users:(("java",pid=1494,fd=75)) +``` + +##### F:客户端测试 + +mycat连接测试: + +```shell +[root@master-1 ~]# mysql -u shop -p123456 -P 8066 -h 10.0.0.47 +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 1 +Server version: 5.6.29-mycat-1.6.7.1-release-20190627191042 MyCat Server (OpenCloudDB) + +Copyright (c) 2000, 2022, 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> show databases; ++----------+ +| DATABASE | ++----------+ +| shop | ++----------+ +1 row in set (0.00 sec) + +mysql> use shop +Database changed +mysql> create table t1(id int); +Query OK, 0 rows affected (0.01 sec) + +mysql> show tables; ++----------------+ +| Tables_in_shop | ++----------------+ +| t1 | ++----------------+ +1 row in set (0.01 sec) + +mysql> insert into t1 values(1); +Query OK, 1 row affected (0.03 sec) + +mysql> insert into t1 values(2); +Query OK, 1 row affected (0.01 sec) +``` + +从服务数据验证: + +```shell +[root@slave-2 ~]# mysql -u root -pQianFeng@123 +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 7 +Server version: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| mysql | +| performance_schema | +| qfcloud | +| sys | ++--------------------+ +5 rows in set (0.00 sec) + +mysql> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| bbs | +| blog | +| mysql | +| performance_schema | +| qfcloud | +| shop | +| sys | ++--------------------+ +8 rows in set (0.00 sec) + +mysql> use shop +Reading table information for completion of table and column names +You can turn off this feature to get a quicker startup with -A + +Database changed +mysql> show tables; ++----------------+ +| Tables_in_shop | ++----------------+ +| t1 | ++----------------+ +1 row in set (0.00 sec) + +mysql> select * from t1; ++------+ +| id | ++------+ +| 1 | +| 2 | ++------+ +2 rows in set (0.00 sec) +``` + +#### 3.Mycat配置文件 + +##### server.xml + +```shell +一:server.xml 配置文件 +1.privileges标签 +对用户的 schema以及表进行精细化的DML(数据操纵语言)权限控制 + --check 表示是否开启DML权限检查。默认是关闭。 +--dml 顺序说明:insert,update,select,delete + +
+
+db1的权限是update,select。 +tb01的权限是啥都不能干。 +tb02的权限是insert,update,select,delete。 +其他表默认是udpate,select。 + +2.system标签 +这个标签内嵌套的所有 property 标签都与系统配置有关。 +utf8 +字符集 +1 +处理线程数量,默认是cpu数量。 +4096 +每次读取留的数量,默认4096。 +409600 +创建共享buffer需要占用的总空间大小。processorBufferChunk*processors*100。 +0 +默认为0。0表示DirectByteBufferPool,1表示ByteBufferArena。 +100 +二级共享buffer是processorBufferPool的百分比,这里设置的是百分比。 +100 +全局ID生成方式。(0:为本地文件方式,1:为数据库方式;2:为时间戳序列方式;3:为ZK生成ID;4:为ZK递增ID生成。 +1 +是否开启mysql压缩协议。1为开启,0为关闭,默认关闭。 +4 +指定 Mysql 协议中的报文头长度。默认 4。 +16M +指定 Mysql 协议可以携带的数据最大长度。默认 16M。 +1800000 +指定连接的空闲超时时间。某连接在发起空闲检查下,发现距离上次使用超过了空闲时间,那么这个连接会被回收,就是被直接的关闭掉。默认 30 分钟,单位毫秒。 +3 +前端连接的初始化事务隔离级别,只在初始化的时候使用,后续会根据客户端传递过来的属性对后端数据库连接进行同步。默认为 REPEATED_READ,设置值为数字默认 3。 +READ_UNCOMMITTED = 1; +READ_COMMITTED = 2; +REPEATED_READ = 3; +SERIALIZABLE = 4; +300 +SQL 执行超时的时间,Mycat 会检查连接上最后一次执行 SQL 的时间,若超过这个时间则会直接关闭这连接。默认时间为 300 秒,单位秒。 +1000 +清理 NIOProcessor 上前后端空闲、超时和关闭连接的间隔时间。默认是 1 秒,单 +位毫秒。 +300000 +对后端连接进行空闲、超时检查的时间间隔,默认是 300 秒,单位毫秒。 +10000 +对后端所有读、写库发起心跳的间隔时间,默认是 10 秒,单位毫秒。 +0.0.0.0 +mycat 服务监听的 IP 地址,默认值为 0.0.0.0。 +8066 +定义 mycat 的使用端口,默认值为 8066。 +9066 +定义 mycat 的管理端口,默认值为 9066。 +5.6 +mycat 模拟的 mysql 版本号,默认值为 5.6 版本,如非特需,不要修改这个值,目前支持设置 5.5,5.6,5.7 版本,其他版本可能会有问题。 +0 +是否开启实时统计。1为开启;0为关闭 。 +0 +是否开启全局表一致性检测。1为开启;0为关闭 。 +0 +分布式事务开关。0为不过滤分布式事务;1为过滤分布式事务;2 为不过滤分布式事务,但是记录分布式事务日志。 +65535 +默认是65535。 64K 用于sql解析时最大文本长度 +以上举例的属性仅仅是一部分,可以配置的变量很多。 +System标签下的属性,一般是上线后,需要根据实际运行的情况,分析后调优的时候进行修改。 + +3. Firewall标签 +防火墙的设置,也就是在网络层对请求的地址进行限制,主要是从安全角度来保证Mycat不被匿名IP进行访问 + + + + + + + + +``` + +##### schema.xml + +```shell +一:schema.xml +–schema 数据库设置,此数据库为逻辑数据库,name与server.xml中schema对应 +–dataNode 分片信息,也就是分库相关配置 +–dataHost 物理数据库,真正存储数据的数据库 + +1、schema 标签 + +schema标签用来定义mycat实例中的逻辑库,mycat可以有多个逻辑库,每个逻辑库都有自己的相关配置。可以使用schema标签来划分这些不同的逻辑库,如果不配置schema标签,所有表的配置会属于同一个默认的逻辑库。逻辑库的概念和MySql的database的概念一样,我们在查询两个不同逻辑库中的表的时候,需要切换到该逻辑库下进行查询。 +–name 逻辑数据库名,与server.xml中的schema对应 +–checkSQLschema 数据库前缀相关设置,当该值为true时,例如我们执行语句select * from TESTDB.company 。mycat会把语句修改为 select * from company 去掉TESTDB。 +–sqlMaxLimit 当该值设置为某个数值时,每条执行的sql语句,如果没有加上limit语句,Mycat会自动加上对应的值。不写的话,默认返回所有的值。需要自己sql语句加limit。 + +2、dataNode标签 + +datanode标签定义了mycat中的数据节点,也就是数据分片。一个datanode标签就是一个独立的数据分片。 +localhost1数据库实例上的db1物理数据库,这就组成一个数据分片,最后我们用dn1来标示这个分片。 +–name 定义数据节点的名字,这个名字需要唯一。我们在table标签上用这个名字来建立表与分片对应的关系 +–dataHost 用于定义该分片属于哪个数据库实例,属性与datahost标签上定义的name对应 +–database 用于定义该分片属于数据库实例上 的具体库。 + +3、dataHost标签 +这个标签直接定义了具体数据库实例,读写分离配置和心跳语句。 + +select user() + + + + + +–name 唯一标示dataHost标签,供上层使用 +–maxCon 指定每个读写实例连接池的最大连接。 +–minCon 指定每个读写实例连接池的最小连接,初始化连接池的大小 +–balance 负载均称类型 +balance=“0”:不开启读写分离机制,所有读操作都发送到当前可用的writeHost上 +balance=“1”:全部的readHost与stand by writeHost参与select语句的负载均衡,简单的说,当双主双从模式(M1-S1,M2-S2 并且M1 M2互为主备),正常情况下,M2,S1,S2都参与select语句的负载均衡。 +balance=“2”:所有读操作都随机的在writeHost、readHost上分发 +balance=“3”:所有读请求随机的分发到writeHst对应的readHost执行,writeHost不负担读写压力。 +–writeType 负载均衡类型。 +writeType=“0”, 所有写操作发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties . +writeType=“1”,所有写操作都随机的发送到配置的 writeHost。1.5以后版本废弃不推荐。 +–switchType -1不自动切换 +1 默认值 自动切换 +2 基于MySql主从同步的状态决定是否切换心跳语句为 show slave status +3 基于mysql galary cluster 的切换机制(适合集群) 心跳语句为 show status like ‘wsrep%’ +–dbType 指定后端链接的数据库类型目前支持二进制的mysql协议,还有其他使用jdbc链接的数据库,例如:mongodb,oracle,spark等 +–dbDriver 指定连接后段数据库使用的driver,目前可选的值有native和JDBC。使用native的话,因为这个值执行的是二进制的mysql协议,所以可以使用mysql和maridb,其他类型的则需要使用JDBC驱动来支持。 +如果使用JDBC的话需要符合JDBC4标准的驱动jar 放到mycat\lib目录下,并检查驱动jar包中包括如下目录结构文件 META-INF\services\java.sql.Driver。 在这个文件写上具体的driver类名,例如com.mysql.jdbc.Driver +writeHost readHost指定后端数据库的相关配置给mycat,用于实例化后端连接池。 +–tempReadHostAvailable +如果配置了这个属性 writeHost 下面的 readHost 仍旧可用,默认 0 可配置(0、1)。 +1)heartbeat标签 +这个标签内指明用于和后端数据库进行心跳检查的语句。 +例如:MYSQL 可以使用 select user(),Oracle 可以使用 select 1 from dual 等。 +2) writeHost /readHost 标签 +这两个标签都指定后端数据库的相关配置,用于实例化后端连接池。唯一不同的是,writeHost 指定写实例、readHost 指定读实例。 +在一个 dataHost 内可以定义多个 writeHost 和 readHost。但是,如果 writeHost 指定的后端数据库宕机,那么这个 writeHost 绑定的所有 readHost 都将不可用。 +另一方面,由于这个 writeHost 宕机,系统会自动的检测到,并切换到备用的 writeHost 上去。这两个标签的属性相同,这里就一起介绍。 +–host 用于标识不同实例,一般 writeHost 我们使用M1,readHost 我们用S1。 +–url 后端实例连接地址。Native:地址:端口 JDBC:jdbc的url +–password 后端存储实例需要的密码 +–user 后端存储实例需要的用户名字 +–weight 权重 配置在 readhost 中作为读节点的权重 +–usingDecrypt 是否对密码加密,默认0。 +``` + + + + + + + diff --git a/mysql-MD/第二章:数据库管理及数据类型.md b/mysql-MD/第二章:数据库管理及数据类型.md new file mode 100644 index 0000000..61bcb2e --- /dev/null +++ b/mysql-MD/第二章:数据库管理及数据类型.md @@ -0,0 +1,861 @@ +

数据库管理及数据类型

+ +**作者:行癫(盗版必究)** + +------ + +## 一:数据类型 + +#### 1.数值类型 + +##### 整数类型 + +​ 整数类型:TINYINT SMALLINT MEDIUMINT INT BIGINT + +​ 作用:用于存储用户的年龄、游戏的Level、经验值等 + +![image-20220920124821420](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220920124821420.png) + +##### 浮点数类型 + +​ 浮点数类型:FLOAT DOUBLE + +​ 作用:用于存储用户的身高、体重、薪水等 + +![image-20220920124919373](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220920124919373.png) + +```shell +float(5,3) 5宽度 3精度 +注意: + 宽度不算小数点 + 宽度-精度=点前 +案例: +MySQL [(none)]> create database diandian; +Query OK, 1 row affected (0.00 sec) + +MySQL [(none)]> use diandian +Database changed +MySQL [diandian]> create table t1(id float(6,2)); +Query OK, 0 rows affected (0.24 sec) + +MySQL [diandian]> insert into t1 values('2.22'); +``` + +##### 定点数类型 + +​ 定点数类型:DEC + +​ 定点数在MySQL内部以字符串形式存储,比浮点数更精确,适合用来表示货币等精度高的数据 + +##### 位类型 + +​ 位类型:BIT + +​ BIT(M)可以用来存放多位二进制数,M范围从1~64,如果不写默认为1位 + +#### 2.字符串类型 + +​ CHAR系列 CHAR VARCHAR + +​ TEXT系列 TINYTEXT TEXT MEDIUMTEXT LONGTEXT + +​ BLOB 系列 TINYBLOB BLOB MEDIUMBLOB LONGBLOB + +​ BINARY系列 BINARY VARBINARY + +![image-20220920132114919](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220920132114919.png) + +##### 枚举类型 + +​ 枚举类型:枚举列可以把一些不重复的字符串存储成一个预定义的集合 + +```shell +mysql> create table enum_table( e ENUM('fish','apple','dog')); +Query OK, 0 rows affected (0.35 sec) +mysql> insert into enum_table(e) values('fish'); +Query OK, 1 row affected (0.11 sec) + +mysql> select * from enum_table; ++------+ +| e | ++------+ +| fish | ++------+ +1 row in set (0.00 sec) + +mysql> insert into enum_table(e) values('nihao'); +ERROR 1265 (01000): Data truncated for column 'e' at row 1 +``` + +##### 时间和日期类型 + +​ 时间和日期类型:DATE TIME DATETIME TIMESTAMP YEAR + +​ 作用:用于存储用户的注册时间,文章的发布时间,文章的更新时间,员工的入职时间等 + +![image-20220920132630856](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220920132630856.png) + +```shell +mysql> create table t8 ( + id1 timestamp NOT NULL default CURRENT_TIMESTAMP, + id2 datetime default NULL +); + + timestamp 类型的列还有个特性:默认情况下,在 insert, update 数据时,timestamp 列会自动以当前时间(CURRENT_TIMESTAMP)填充/更新。“自动”的意思就是,你不去管它,MySQL 会替你去处理。 + +mysql> insert into t8(id1) values('20180109000000'); +mysql> select * from t8; ++---------------------+------+ +| id1 | d2 | ++---------------------+------+ +| 2018-01-09 00:00:00 | NULL | ++---------------------+------+ +1 row in set (0.00 sec) + +扩展: +select now();查看当前时间 +``` + +## 二:表操作 + +#### 1.案例 + +表::school.student1 + +``` +字段 字段 字段 +id name sex age +1 tom male 23 记录 +2 jack male 21 记录 +3 alice female 19 记录 +``` + +语法: + +```shell +create table 表名(自定义)( + 字段名1 类型[(宽度) 约束条件], + 字段名2 类型[(宽度) 约束条件], + 字段名3 类型[(宽度) 约束条件] +)[存储引擎 字符集]; +==在同一张表中,字段名是不能相同 +==宽度和约束条件可选 +==字段名和类型是必须的 +``` + +创建库表: + +```shell +mysql> CREATE DATABASE school; //创建数据库school +mysql> use school; +mysql> create table student1( + id int, + name varchar(50), + sex enum('m','f'), + age int + ); +Query OK, 0 rows affected (0.03 sec) +``` + +查看库: + +```shell +mysql> show tables; ++------------------+ +| Tables_in_school | ++------------------+ +| student1 | ++------------------+ +1 row in set (0.00 sec) +``` + +插入语法: + +``` +insert into 表名(字段1,字段2...) values(字段值列表...); +``` + +插入数据: + +```shell +mysql> insert into student1(id,name,sex,age) values(1,'xingdia','m','26'); +``` + +查看表结构: + +```shell +mysql> desc student1; ++-------+---------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+---------------+------+-----+---------+-------+ +| id | int(11) | YES | | NULL | | +| name | varchar(50) | YES | | NULL | | +| sex | enum('m','f') | YES | | NULL | | +| age | int(11) | YES | | NULL | | ++-------+---------------+------+-----+---------+-------+ +4 rows in set (0.00 sec) +``` + +查询数据: + +```shell +mysql> select id,name,sex,age from student1; //查询表中所有字段的值 +Empty set (0.00 sec) + +mysql> select * from student1; /查询表中所有字段的值 +Empty set (0.00 sec) + +mysql> select name,age from student1; //查询表中指定字段的值 +Empty set (0.00 sec) +``` + +扩展插入: + +```shell +mysql> insert into student1 values (1,'xingdian','m',33),(2,'alice','m',20),(3,'jack','m',40); //顺序插入 +Query OK, 3 rows affected (0.14 sec) +Records: 3 Duplicates: 0 Warnings: 0 + +mysql> insert into student1(name,age) values ('zhuzhu',10),('gougou',20); //只向指定的字段插入值 +Query OK, 2 rows affected (0.12 sec) +Records: 2 Duplicates: 0 Warnings: 0 +``` + +#### 2.案例 + +表:school.student2 + +```shell + 字段名 数据类型 +编号 id int +姓名 name varchar(50) +出生年份 born_year year +生日 birthday date +上课时间 class_time time +注册时间 reg_time datetime +``` + +创建表: + +```shell +mysql> create table student2( + id int, + name varchar(50), + born_year year, + birthday date, + class_time time, + reg_time datetime + ); +``` + +插入数据: + +```shell +mysql> insert into student2 values(1,'tom',now(),now(),now(),now()); +mysql> insert into student2 values(2,'jack',1982,19821120,123000,20140415162545); +``` + +表:school.student3 + +```shell +id id int +姓名 name varchar(50) +性别 sex enum('male','female') +爱好 hobby set('music','book','game','disc') +``` + +创建表: + +```shell +mysql> create table student3( + id int, + name varchar(50), + sex enum('male','female'), + hobby set('music','book','game','disc') + ); +``` + +查看表结构: + +```shell +mysql> desc student3; +mysql> show create table student3\G +``` + +插入数据: + +```shell +mysql> insert into student3 values (1,'tom','male','book,game'); +mysql> insert into student3 values (2,'jack','male','film'); +``` + +注意: + +​ DESCRIBE查看表结构 + +```shell +DESCRIBE 表名; +DESC 表名; +``` + +​ 查看表详细结构 + +```shell +SHOW CREATE TABLE 表名; +``` + +## 三:表完整性约束 + +#### 1.作用 + +​ 用于保证数据的完整性和一致性 + +#### 2.约束条件 + +PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录,不可以为空 UNIQUE + NOT NULL + +FOREIGN KEY (FK) 标识该字段为该表的外键,实现表与表(父表主键/子表1外键/子表2外键)之间的关联 + +NOT NULL 标识该字段不能为空 + +UNIQUE KEY (UK) 标识该字段的值是唯一的,可以为空,一个表中可以有多个UNIQUE KEY + +AUTO_INCREMENT 标识该字段的值自动增长(整数类型,而且为主键) + +DEFAULT 为该字段设置默认值 + +注意: + +​ 是否允许为空,默认NULL,可设置NOT NULL,字段不允许为空,必须赋值 + +​ 字段是否有默认值,缺省的默认值是NULL,如果插入记录时不给字段赋值,此字段使用默认值 + +```shell +MySQL [(none)]> sex enum('male','female') not null default 'male' +MySQL [(none)]> age int unsigned NOT NULL default 20 必须为正值(无符号) 不允许为空 默认是20 +``` + +​ 是否是key 主键 primary key 外键 forengn key + +#### 3.NOT NULL + +表:school.student4 + +创建表:(注意前提需要有库) + +```shell +mysql> create table school.student4( + id int not null, + name varchar(50) not null, + sex enum('m','f') default 'm' not null, + age int unsigned default 18 not null, + hobby set('music','disc','dance','book') default 'book,dance' + ); +``` + +插入数据:(注意观察查询到的数据) + +```shell +MySQL [(none)]> insert into school.student4(id,name) values(2,'robin'); +Query OK, 1 row affected (0.08 sec) + +MySQL [(none)]> select * from school.student4; ++----+-------+-----+-----+------------+ +| id | name | sex | age | hobby | ++----+-------+-----+-----+------------+ +| 2 | robin | m | 18 | dance,book | ++----+-------+-----+-----+------------+ +1 row in set (0.00 sec) +``` + +注意报错的原因: + +```shell +MySQL [(none)]> insert into school.student4 values(3,NULL,'m',40,'book'); +ERROR 1048 (23000): Column 'name' cannot be null +``` + +#### 4.唯一约束 + +作用: + +​ MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度 + +表:company.department1 + +创建表: + +```shell +MySQL [(none)]> create database company; +Query OK, 1 row affected (0.01 sec) +MySQL [(none)]> CREATE TABLE company.department1 (dept_id INT,dept_name VARCHAR(30) UNIQUE,comment VARCHAR(50)); +Query OK, 0 rows affected (0.34 sec) +``` + +查看表结构: + +```shell +MySQL [(none)]> desc company.department1; ++-----------+-------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-----------+-------------+------+-----+---------+-------+ +| dept_id | int(11) | YES | | NULL | | +| dept_name | varchar(30) | YES | UNI | NULL | | +| comment | varchar(50) | YES | | NULL | | ++-----------+-------------+------+-----+---------+-------+ +3 rows in set (0.00 sec) +``` + +数据插入:(注意查看插入数据时的提示) + +```shell +MySQL [(none)]> insert into company.department1 values ("1","xingdian","hr"); +Query OK, 1 row affected (0.04 sec) + +MySQL [(none)]> insert into company.department1 values ("1","xingdian","hr"); +ERROR 1062 (23000): Duplicate entry 'xingdian' for key 'dept_name' +``` + +#### 5.主键约束 + +注意:primary key 字段的值是不允许重复,且不允许不NULL(UNIQUE + NOT NULL) + +表:school.student6 + +创建表: + +```shell +MySQL [(none)]> create table school.student6( + id int primary key not null auto_increment, + name varchar(50) not null, + sex enum('male','female') not null default 'male', + age int not null default 18 + ); +Query OK, 0 rows affected (0.47 sec) +``` + +插入数据: + +```shell +MySQL [(none)]> insert into school.student6 values (1,'alice','female',22); +Query OK, 1 row affected (0.18 sec) + +MySQL [(none)]> insert into school.student6(name,sex,age) values + -> ('jack','male',19), + -> ('tom','male',23); +Query OK, 2 rows affected (0.14 sec) +Records: 2 Duplicates: 0 Warnings: 0 + +MySQL [(none)]> select * from school.student6; ++----+-------+--------+-----+ +| id | name | sex | age | ++----+-------+--------+-----+ +| 1 | alice | female | 22 | +| 2 | jack | male | 19 | +| 3 | tom | male | 23 | ++----+-------+--------+-----+ +3 rows in set (0.00 sec) + +``` + +## 四:修改表 + +语法格式: + +修改表名 + +​ ALTER TABLE 表名 RENAME 新表名; + +增加字段 + +​ ALTER TABLE 表名 + +​ ADD 字段名 数据类型 [完整性约束条件…], + +​ ADD 字段名 数据类型 [完整性约束条件…]; + +​ ALTER TABLE 表名 + +​ ADD 字段名 数据类型 [完整性约束条件…] AFTER 字段名; +删除字段 + +​ ALTER TABLE 表名 DROP 字段名; + +修改字段 + +​ ALTER TABLE 表名 MODIFY 字段名 数据类型 [完整性约束条件…]; + +​ ALTER TABLE 表名 CHANGE 旧字段名 新字段名 旧数据类型 [完整性约束条件…]; + +​ ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型 [完整性约束条件…]; + +#### 1.修改数据库引擎 + +```shell +mysql> alter table service engine=innodb; //engine=myisam|memory|.... +``` + +#### 2.添加字段 + +```shell +mysql> create table student10 (id int); +mysql> alter table student10 add name varchar(20) not null, add age int not null default 22; + +mysql> alter table student10 add stu_num int not null after name; //添加name字段之后 + +mysql> alter table student10 add sex enum('male','female') default 'male' first; //添加到最前面 +``` + +#### 3.删除字段 + +```shell +mysql> alter table student10 drop sex; +``` + +#### 4.修改字段类型 + +```shell +MySQL [school]> desc student10; ++-------+---------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+---------+------+-----+---------+-------+ +| id | int(11) | YES | | NULL | | +| age | int(11) | YES | | NULL | | ++-------+---------+------+-----+---------+-------+ +2 rows in set (0.01 sec) + +MySQL [school]> alter table student10 modify age tinyint not null ; +Query OK, 0 rows affected (0.04 sec) +Records: 0 Duplicates: 0 Warnings: 0 + +MySQL [school]> desc student10; ++-------+------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+------------+------+-----+---------+-------+ +| id | int(11) | YES | | NULL | | +| age | tinyint(4) | NO | | NULL | | ++-------+------------+------+-----+---------+-------+ +2 rows in set (0.00 sec) + +MySQL [school]> alter table student10 modify id int not null primary key ; ////修改字段类型、约束、主键 +Query OK, 0 rows affected (0.02 sec) +Records: 0 Duplicates: 0 Warnings: 0 + +MySQL [school]> desc student10; ++-------+------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+------------+------+-----+---------+-------+ +| id | int(11) | NO | PRI | NULL | | +| age | tinyint(4) | NO | | NULL | | ++-------+------------+------+-----+---------+-------+ +``` + +#### 5.增加约束 + +```shell +MySQL [school]> alter table student10 modify id int not null primary key ; +Query OK, 0 rows affected (0.02 sec) +Records: 0 Duplicates: 0 Warnings: 0 + +MySQL [school]> desc student10; ++-------+------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+------------+------+-----+---------+-------+ +| id | int(11) | NO | PRI | NULL | | +| age | tinyint(4) | NO | | NULL | | ++-------+------------+------+-----+---------+-------+ +2 rows in set (0.00 sec) + +MySQL [school]> alter table student10 modify id int not null primary key auto_increment; +ERROR 1068 (42000): Multiple primary key defined //错误,该字段已经是primary key + +MySQL [school]> alter table student10 modify id int not null auto_increment; +Query OK, 0 rows affected (0.04 sec) +Records: 0 Duplicates: 0 Warnings: 0 + +MySQL [school]> desc student10; ++-------+------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++-------+------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| age | tinyint(4) | NO | | NULL | | ++-------+------------+------+-----+---------+----------------+ +``` + +#### 6.增加主键 + +```shell +MySQL [school]> desc student1; ++-------+---------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+---------+------+-----+---------+-------+ +| id | int(11) | YES | | NULL | | +| age | int(11) | YES | | NULL | | +| name | char(1) | YES | | NULL | | ++-------+---------+------+-----+---------+-------+ +3 rows in set (0.00 sec) + +MySQL [school]> alter table student1 add primary key(id); + +MySQL [school]> desc student1; ++-------+---------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+---------+------+-----+---------+-------+ +| id | int(11) | NO | PRI | NULL | | +| age | int(11) | YES | | NULL | | +| name | char(1) | YES | | NULL | | ++-------+---------+------+-----+---------+-------+ +3 rows in set (0.00 sec) +``` + +#### 7.修改主键和自增 + +```shell +MySQL [school]> alter table student1 modify id int auto_increment; +Query OK, 0 rows affected (0.03 sec) +Records: 0 Duplicates: 0 Warnings: 0 + +MySQL [school]> desc student1; ++-------+---------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++-------+---------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| age | int(11) | YES | | NULL | | +| name | char(1) | YES | | NULL | | ++-------+---------+------+-----+---------+----------------+ +``` + +#### 8.删除主键 + +```shell +MySQL [school]> desc student10; ++-------+------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++-------+------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| age | tinyint(4) | NO | | NULL | | ++-------+------------+------+-----+---------+----------------+ +2 rows in set (0.00 sec) + +MySQL [school]> alter table student10 drop primary key; +ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key + +删除自增 +ySQL [school]> alter table student10 modify id int not null; +Query OK, 0 rows affected (0.04 sec) +Records: 0 Duplicates: 0 Warnings: 0 + +MySQL [school]> desc student10; ++-------+------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+------------+------+-----+---------+-------+ +| id | int(11) | NO | PRI | NULL | | +| age | tinyint(4) | NO | | NULL | | ++-------+------------+------+-----+---------+-------+ + +MySQL [school]> alter table student10 drop primary key; +Query OK, 0 rows affected (0.03 sec) +Records: 0 Duplicates: 0 Warnings: 0 + +MySQL [school]> desc student10; ++-------+------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+------------+------+-----+---------+-------+ +| id | int(11) | NO | | NULL | | +| age | tinyint(4) | NO | | NULL | | ++-------+------------+------+-----+---------+-------+ +2 rows in set (0.00 sec) +``` + +#### 9.复制表 + +复制表结构+记录 (key不会复制: 主键、外键和索引)复制表结构/记录+表结构,不会将Key复制 + +```shell +mysql> create table new_service select * from service; +``` + +只复制表结构 + +```shell +mysql> create table new1_service select * from service where 1=2; //条件为假,查不到任何记录 +``` + +可以复制主键,只复制表结构 + +```shell +mysql> create table t4 like employees; +``` + +#### 10.删除表 + +```shell +mysql> DROP TABLE 表名; +``` + +#### 11.修改数据表中字段的值 + +语法: + +​ Update 表名 set 列名=值where 条件 + +```shell +mysql> update student set name='123' where id=1; +``` + +删除某一行: + +​ delete from 表名 where id=1 + +```shell +mysql> delete from type where id=1; +``` + +## 五:库操作 + +#### 1.简介 + +​ 系统自带库的含义及作用 + +```shell +MySQL [(none)]> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| mysql | +| performance_schema | +| sys | ++--------------------+ +``` + +information_schema:虚拟库,主要存储了系统中的一些数据库对象的信息,例如用户表信息、列信息、权限信息、字符信息等 + +performance_schema:主要存储数据库服务器的性能参数 + +mysql:授权库,主要存储系统用户的权限信息 + +sys:主要存储数据库服务器的性能参数 + +注意:information_schema + +​ SCHEMATA 存放的是系统中的库 + +```shell +MySQL [information_schema]> select * from information_schema.SCHEMATA; ++--------------+--------------------+----------------------------+------------------------+----------+ +| CATALOG_NAME | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH | ++--------------+--------------------+----------------------------+------------------------+----------+ +| def | information_schema | utf8 | utf8_general_ci | NULL | +| def | mysql | latin1 | latin1_swedish_ci | NULL | +| def | performance_schema | utf8 | utf8_general_ci | NULL | +| def | school | latin1 | latin1_swedish_ci | NULL | +| def | sys | utf8 | utf8_general_ci | NULL | ++--------------+--------------------+----------------------------+------------------------+----------+ + +目录_名称 +实际库_名称 +默认_字符_设置_名称 +默认_分类_名称 +``` + +​ TABLES 存储表名 + +```shell +MySQL [information_schema]> select * from information_schema.TABLES\G +*************************** 283. row *************************** + TABLE_CATALOG: def + TABLE_SCHEMA: xingdian + TABLE_NAME: t1 + TABLE_TYPE: BASE TABLE + ENGINE: InnoDB + VERSION: 10 + ROW_FORMAT: Dynamic + TABLE_ROWS: 1 + AVG_ROW_LENGTH: 16384 + DATA_LENGTH: 16384 +MAX_DATA_LENGTH: 0 + INDEX_LENGTH: 0 + DATA_FREE: 0 + AUTO_INCREMENT: NULL + CREATE_TIME: 2022-09-22 08:18:38 + UPDATE_TIME: 2022-09-22 08:18:54 + CHECK_TIME: NULL +TABLE_COLLATION: latin1_swedish_ci + CHECKSUM: NULL + CREATE_OPTIONS: + TABLE_COMMENT: +283 rows in set (0.02 sec) +``` + +​ COLUMNS 存储字段 + +```shell +*************************** 3083. row *************************** + TABLE_CATALOG: def + TABLE_SCHEMA: xingdian + TABLE_NAME: t1 + COLUMN_NAME: id + ORDINAL_POSITION: 1 + COLUMN_DEFAULT: NULL + IS_NULLABLE: YES + DATA_TYPE: int +CHARACTER_MAXIMUM_LENGTH: NULL + CHARACTER_OCTET_LENGTH: NULL + NUMERIC_PRECISION: 10 + NUMERIC_SCALE: 0 + DATETIME_PRECISION: NULL + CHARACTER_SET_NAME: NULL + COLLATION_NAME: NULL + COLUMN_TYPE: int(11) + COLUMN_KEY: + EXTRA: + PRIVILEGES: select,insert,update,references + COLUMN_COMMENT: + GENERATION_EXPRESSION: +3083 rows in set (0.03 sec) +``` + +#### 2.创建库 + +方案一:交互式操作 + +```shell +mysql> create database xingdian; +``` + +数据库命名规则: + +​ 区分大小写 + +​ 唯一性 + +​ 不能使用关键字如 create select + +​ 不能单独使用数字 + +方案二:非交互式 + +```shell +mysql -u root -pQianFeng@123 -e "create database diandian" +``` + +#### 3.查看数据库 + +```shell +mysql> show databases; +mysql> show create database xingdian; +mysql> select database(); 查看当前所在的库 +``` + +#### 4.切换数据库 + +```shell +mysql> use xingdian; +``` + +#### 5.删除数据库 + +```shell +mysql> DROP DATABASE 数据库名; +``` diff --git a/mysql-MD/第六章:主从复制及读写分离.md b/mysql-MD/第六章:主从复制及读写分离.md new file mode 100644 index 0000000..0090744 --- /dev/null +++ b/mysql-MD/第六章:主从复制及读写分离.md @@ -0,0 +1,1501 @@ +

主从复制及读写分离

+ +**作者:行癫(盗版必究)** + +------ + +## 一:主从复制 + +#### 1.主从复制概念 + +什么是主从复制: + +​ 主从复制,是用来建立一个和主数据库完全一样的数据库环境,称为从数据库;主数据库一般是准实时的业务数据库 + +主从复制的作用: + +​ 做数据的热备,作为后备数据库,主数据库服务器故障后,可切换到从数据库继续工作,避免数据丢失 + +​ 架构的扩展,业务量越来越大,I/O访问频率过高,单机无法满足,多库的存储,降低磁盘I/O访问的频率,提高单个机器的I/O性能 + +​ 读写分离,使数据库能支撑更大的并发 + +主从复制的原理: + +​ 数据库有个bin-log二进制文件,记录了所有sql语句 + +​ 我们的目标就是把主数据库的bin-log文件的sql语句复制到从库 + +​ 让其在从数据的relay-log(中继日志)重做日志文件中再执行一次这些sql语句即可 + +image-20220926231910240 + +总结: + +​ 从库slave生成两个线程,i/o线程和sql线程,i/o将变更记录写到二进制日志文件中,再写到中继日志中,sql线程读取中继日志,解析操作,最终数据统一 + +注意: + +​ I/O进程:负责通信 + +​ SQL进程:负责写数据,根据log日志写数据 + +#### 2.主从复制部署 + +##### 环境准备 + +| 节点 | IP地址 | +| :----: | :--------: | +| Master | 10.0.0.128 | +| Slave | 10.0.0.42 | + +注意: + +​ 所有节点关闭防火墙和selinux + +​ 保证yum仓库可用 + +​ 保证网络畅通 + +​ 如果是克隆的服务器需要修改每台数据库的server-uuid + +修改主机名:(所有节点)(可选操作) + +```shell +[root@xingdian ~]# hostnamectl set-hostname master +[root@xingdian ~]# hostnamectl set-hostname slave +``` + +添加本地解析:(所有节点)(可选操作) + +```shell +[root@master ~]# cat /etc/hosts +127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 +::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 +10.0.0.128 master +10.0.0.42 slave + +[root@slave ~]# cat /etc/hosts +127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 +::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 +10.0.0.128 master +10.0.0.42 slave +``` + +##### Master部署 + +安装数据库:(略) + +启动数据库:(略) + +修改数据库初始密码:(略) + +主服务器部署: + +```shell +[root@master ~]# vi /etc/my.cnf +log-bin = my1log +server-id = 1 +``` + +创建授权账户: + +```shell +[root@master ~]# mysql -u root -pQianFeng@123 +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 3 +Server version: 5.7.39 MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> grant all on *.* to 'slave'@'%' identified by 'QianFeng@123'; +mysql> flush privileges; +mysql> exit +Bye +``` + +重启服务: + +```shell +[root@master ~]# systemctl restart mysqld +``` + +注意: + +replication slave: + +​ 拥有此权限可以查看从服务器,从主服务器读取二进制日志 + +super权限: + +​ 允许用户使用修改全局变量的SET语句以及CHANGE MASTER语句 + +reload权限: + +​ 必须拥有reload权限,才可以执行flush [tables | logs | privileges] + +##### Slave部署 + +安装数据库:(略) + +启动数据库:(略) + +修改数据库初始密码:(略) + +从服务器部署: + +```shell +[root@slave ~]# vi /etc/my.cnf +log-bin = my2log +server-id = 2 +``` + +重启服务: + +```shell +[root@slave ~]# systemctl restart mysqld +``` + +获取主服务器信息:(主服务器操作) + +```shell +[root@master ~]# mysql -u root -pQianFeng@123 +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 2 +Server version: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> show binlog events; ++---------------+-----+----------------+-----------+-------------+---------------------------------------+ +| Log_name | Pos | Event_type | Server_id | End_log_pos | Info | ++---------------+-----+----------------+-----------+-------------+---------------------------------------+ +| my1log.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.39-log, Binlog ver: 4 | +| my1log.000001 | 123 | Previous_gtids | 1 | 154 | | ++---------------+-----+----------------+-----------+-------------+---------------------------------------+ +2 rows in set (0.00 sec) +``` + +指定主服务器信息:(从服务器操作) + +```shell +[root@slave ~]# mysql -u root -pQianFeng@123 +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 2 +Server version: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> edit + -> ; +Query OK, 0 rows affected, 2 warnings (0.01 sec) + +注意:edit中添加的内容 +CHANGE MASTER TO + MASTER_HOST='master', + MASTER_USER='slave', + MASTER_PASSWORD='QianFeng@123', + MASTER_PORT=3306, + MASTER_LOG_FILE='my1log.000001', + MASTER_LOG_POS=4, + MASTER_CONNECT_RETRY=10; + +参数解释: +CHANGE MASTER TO + MASTER_HOST='mysql-master-1.blackmed.cn/ip', + MASTER_USER='slave', //主服务器用户 + MASTER_PASSWORD='big', + MASTER_PORT=3306, + MASTER_LOG_FILE='master2-bin.001', //日志文件 + MASTER_LOG_POS=4, //日志位置 + MASTER_CONNECT_RETRY=10; //默认尝试次数 +获取参数: +mysql> help change master to +``` + +启动slave: + +```shell +mysql> start slave; +Query OK, 0 rows affected (0.00 sec) +``` + +注意: + +​ stop slave;停止slave + +​ reset master;删除所有的binglog日志文件,并将日志索引文件清空,重新开始所有新的日志文件;用于第一次进行搭建主从库时,进行主库binlog初始化工作 + +​ reset slave;用于删除SLAVE数据库的relaylog日志文件,并重新启用新的relaylog文件 + +查看主从状态: + +```shell +mysql> show slave status\G +*************************** 1. row *************************** + Slave_IO_State: Waiting for master to send event + Master_Host: master + Master_User: slave + Master_Port: 3306 + Connect_Retry: 10 + Master_Log_File: my1log.000001 + Read_Master_Log_Pos: 154 + Relay_Log_File: slave-relay-bin.000002 + Relay_Log_Pos: 361 + Relay_Master_Log_File: my1log.000001 + 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: 154 + Relay_Log_Space: 568 + 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: 85c6acc4-3db0-11ed-b302-000c29311164 + Master_Info_File: /var/lib/mysql/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.00 sec) +``` + +注意: + +​ Slave_IO_Running: Yes + +​ Slave_SQL_Running: Yes + +验证: + +​ 主服务器创建数据: + +```shell +mysql> create database t1; +Query OK, 1 row affected (0.00 sec) +``` + +​ 从服务器查看数据: + +```shell +mysql> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| mysql | +| performance_schema | +| sys | +| t1 | ++--------------------+ +5 rows in set (0.00 sec) +``` + +## 二:GTID主从复制 + +#### 1.GTID概念 + +​ GTID基于事务ID复制 + +​ GTID全局事务标识(global transaction identifiers) + +​ 是用来代替传统复制的方法,GTID复制与普通复制模式的最大不同就是不需要指定二进制文件名和位置 + +​ 不再使用MASTER_LOG_FILE+MASTER_LOG_POS开启复制。而是使用MASTER_AUTO_POSTION=1的方式开始复制 + +#### 2.GTID组成 + +​ GTID = source_id:transaction_id + +​ source_id源id,用于鉴别原服务器,即mysql服务器唯一的server_uuid,由于GTID会传递到slave,所以也可以理解为源ID + +​ transaction_id事务id,为当前服务器上已提交事务的一个序列号,通常从1开始自增长的序列,一个数值对应一个事务 + +示例: + +​ 3E11FA47-71CA-11E1-9E33-C80AA9429562:23 + +​ 前面的一串为服务器的server_uuid + +​ 后面的23为transaction_id + +#### 3.GTID工作原理 + +​ master更新数据时,会在事务前产生GTID,一同记录到binlog日志中 + +​ slave端的i/o 线程将变更的binlog,写入到本地的relay log中 + +​ sql线程从relay log中获取GTID,然后对比slave端的binlog是否有记录 + +​ 如果有记录,说明该GTID的事务已经执行,slave会忽略 + +​ 如果没有记录,slave就会从relay log中执行该GTID的事务,并记录到binlog + +#### 4.主从部署 + +​ 注意:实验之前环境初始化,不要有残留的数据 + +##### 环境准备 + +| 节点 | IP地址 | +| :----: | :--------: | +| Master | 10.0.0.128 | +| Slave | 10.0.0.42 | + +注意: + +​ 所有节点关闭防火墙和selinux + +​ 保证yum仓库可用 + +​ 保证网络畅通 + +​ 如果是克隆的服务器需要修改每台数据库的server-uuid + +修改主机名:(所有节点)(可选操作) + +```shell +[root@xingdian ~]# hostnamectl set-hostname master +[root@xingdian ~]# hostnamectl set-hostname slave +``` + +添加本地解析:(所有节点)(可选操作) + +```shell +[root@master ~]# cat /etc/hosts +127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 +::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 +10.0.0.128 master +10.0.0.42 slave + +[root@slave ~]# cat /etc/hosts +127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 +::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 +10.0.0.128 master +10.0.0.42 slave +``` + +##### Master部署 + +安装数据库:(略) + +启动数据库:(略) + +修改数据库初始密码:(略) + +主服务器部署: + +```shell +[root@master ~]# vim /etc/my.cnf +log-bin +server-id=1 +gtid_mode = ON +enforce_gtid_consistency=1 +``` + +创建授权用户: + +```shell +[root@master ~]# mysql -u root -pQianFeng@123 +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 3 +Server version: 5.7.39 MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> grant all on *.* to slave@'%' identified by 'QianFeng@123'; +Query OK, 0 rows affected, 1 warning (0.00 sec) + +mysql> flush privileges; +Query OK, 0 rows affected (0.00 sec) + +mysql> exit +Bye +``` + +重启服务: + +```shell +[root@master ~]# systemctl restart mysqld +``` + +##### Slave部署 + +安装数据库:(略) + +启动数据库:(略) + +修改数据库初始密码:(略) + +从服务器部署: + +```shell +[root@slave ~]# vim /etc/my.cnf +log-bin +server-id=2 +gtid_mode = ON +enforce_gtid_consistency=1 +relay_log_recovery = on +master-info-repository=TABLE +relay-log-info-repository=TABLE +//这两个参数会将master.info和relay.info保存在表中,默认是Myisam引擎,官方建议用 +``` + +重启服务: + +```shell +[root@slave ~]# systemctl restart mysqld +``` + +配置连接主服务器: + +```shell +[root@slave ~]# mysql -u root -pQianFeng@123 +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 2 +Server version: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> change master to + -> master_host='master', + -> master_user='slave', + -> master_password='QianFeng@123', + -> master_auto_position=1; +Query OK, 0 rows affected, 2 warnings (0.00 sec) +``` + +启动Slave: + +```shell +mysql> start slave; +Query OK, 0 rows affected (0.00 sec) +``` + +主从状态验证: + +```shell +mysql> show slave status\G +*************************** 1. row *************************** + Slave_IO_State: Waiting for master to send event + Master_Host: master + Master_User: slave + Master_Port: 3306 + Connect_Retry: 60 + Master_Log_File: master-bin.000001 + Read_Master_Log_Pos: 154 + Relay_Log_File: slave-relay-bin.000002 + Relay_Log_Pos: 369 + Relay_Master_Log_File: master-bin.000001 + 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: 154 + Relay_Log_Space: 576 + 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: 00813e87-4321-11ed-a33c-000c29311164 + Master_Info_File: mysql.slave_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: 1 + Replicate_Rewrite_DB: + Channel_Name: + Master_TLS_Version: +1 row in set (0.00 sec) +``` + +数据验证: + +主服务器创建数据: + +```shell +[root@master ~]# mysql -u root -pQianFeng@123 +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 3 +Server version: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> create database qfcloud; +Query OK, 1 row affected (0.00 sec) + +mysql> exit +Bye +``` + +从服务器查验数据: + +```shell +mysql> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| mysql | +| performance_schema | +| qfcloud | +| sys | ++--------------------+ +5 rows in set (0.00 sec) +``` + +## 三:GTID双主双从 + +#### 1.环境准备 + +注意: + +​ 实验之前环境初始化,不要有残留的数据 + +​ 先做双主,M-M互为主从,从是双主的从 + +| 节点 | IP地址 | +| :------: | :--------: | +| Master-1 | 10.0.0.128 | +| Master-2 | 10.0.0.46 | +| Slave-1 | 10.0.0.42 | +| Slave-2 | 10.0.0.45 | + +注意: + +​ 所有节点关闭防火墙和selinux + +​ 保证yum仓库可用 + +​ 保证网络畅通 + +​ 如果是克隆的服务器需要修改每台数据库的server-uuid + +#### 2.Master-1部署 + +安装数据库:(略) + +启动数据库:(略) + +修改数据库初始密码:(略) + +主服务器一部署: + +```shell +[root@master-1 ~]# vim /etc/my.cnf +log-bin = my1log +server-id = 1 +gtid_mode=ON +enforce_gtid_consistency=1 +``` + +创建授权账户: + +```shell +[root@master-1 ~]# mysql -u root -pQianFeng@123 +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 4 +Server version: 5.7.39 MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> grant all on *.* to 'slave'@'%' identified by 'QianFeng@123'; +Query OK, 0 rows affected, 1 warning (0.00 sec) + +mysql> flush privileges; +Query OK, 0 rows affected (0.00 sec) + +mysql> exit +Bye +``` + +重启服务: + +```shell +[root@master-1 ~]# systemctl restart mysqld +``` + +#### 3.Master-2部署 + +安装数据库:(略) + +启动数据库:(略) + +修改数据库初始密码:(略) + +主服务器二部署: + +```shell +[root@master-2 ~]# vim /etc/my.cnf +log-bin = my2log +server-id = 2 +gtid_mode=ON +enforce_gtid_consistency=1 +``` + +创建授权账户: + +```shell +[root@master-2 ~]# mysql -u root -pQianFeng@123 +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 4 +Server version: 5.7.39 MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> grant all on *.* to 'slave'@'%' identified by 'QianFeng@123'; +Query OK, 0 rows affected, 1 warning (0.00 sec) + +mysql> flush privileges; +Query OK, 0 rows affected (0.00 sec) + +mysql> exit +Bye +``` + +重启服务: + +```shell +[root@master-2 ~]# systemctl restart mysqld +``` + +#### 4.双主互为主从 + +Master-1: + +```shell +[root@master-1 ~]# mysql -u root -pQianFeng@123 +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 3 +Server version: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> change master to + -> master_host='master-2', + -> master_user='slave', + -> master_password='QianFeng@123', + -> master_auto_position=1; +Query OK, 0 rows affected, 2 warnings (0.01 sec) + +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: master-2 + Master_User: slave + Master_Port: 3306 + Connect_Retry: 60 + Master_Log_File: my2log.000001 + Read_Master_Log_Pos: 154 + Relay_Log_File: master-1-relay-bin.000002 + Relay_Log_Pos: 361 + Relay_Master_Log_File: my2log.000001 + 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: 154 + Relay_Log_Space: 571 + 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: 2 + Master_UUID: 1b453d94-452e-11ed-b744-000c2936b606 + Master_Info_File: /var/lib/mysql/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: 146f4cae-452e-11ed-b87f-000c29311164:1-2 + Auto_Position: 1 + Replicate_Rewrite_DB: + Channel_Name: + Master_TLS_Version: +1 row in set (0.00 sec) + +mysql> exit +Bye +``` + +Master-2: + +```shell +[root@master-2 ~]# mysql -u root -pQianFeng@123 +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 3 +Server version: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> change master to + -> master_host='master-1', + -> master_user='slave', + -> master_password='QianFeng@123', + -> master_auto_position=1; +Query OK, 0 rows affected, 2 warnings (0.01 sec) + +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: master-1 + Master_User: slave + Master_Port: 3306 + Connect_Retry: 60 + Master_Log_File: my1log.000001 + Read_Master_Log_Pos: 587 + Relay_Log_File: master-2-relay-bin.000002 + Relay_Log_Pos: 794 + Relay_Master_Log_File: my1log.000001 + 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: 587 + Relay_Log_Space: 1004 + 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: 146f4cae-452e-11ed-b87f-000c29311164 + Master_Info_File: /var/lib/mysql/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: 146f4cae-452e-11ed-b87f-000c29311164:1-2 + Executed_Gtid_Set: 146f4cae-452e-11ed-b87f-000c29311164:1-2 + Auto_Position: 1 + Replicate_Rewrite_DB: + Channel_Name: + Master_TLS_Version: +1 row in set (0.00 sec) + +mysql> exit +Bye +``` + +#### 5.Slave-1部署 + +安装数据库:(略) + +启动数据库:(略) + +修改数据库初始密码:(略) + +从服务器一部署: + +```shell +[root@slave-1 ~]# vim /etc/my.cnf +log-bin = my3log +server-id = 3 +gtid_mode=ON +enforce_gtid_consistency=1 +relay_log_info_repository = TABLE +master_info_repository = TABLE +relay_log_recovery = on + 当slave从库宕机后,假如relay-log损坏了,导致一部分中继日志没有处理,则自动放弃所有未执行的relay-log,并且重新从master上获取日志,这样就保证了relay-log的完整性 +``` + +重启服务: + +```shell +[root@slave-1 ~]# systemctl restart mysqld +``` + +从连接主服务器: + +```shell +[root@slave-1 ~]# mysql -u root -pQianFeng@123 +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 2 +Server version: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> CHANGE MASTER TO + -> MASTER_HOST='master-1', + -> MASTER_USER='slave', + -> MASTER_PASSWORD='QianFeng@123', + -> MASTER_AUTO_POSITION=1 FOR CHANNEL 'master-1'; +Query OK, 0 rows affected, 2 warnings (0.00 sec) + +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: master-1 + Master_User: slave + Master_Port: 3306 + Connect_Retry: 60 + Master_Log_File: my1log.000001 + Read_Master_Log_Pos: 587 + Relay_Log_File: slave-1-relay-bin-master@002d1.000002 + Relay_Log_Pos: 794 + Relay_Master_Log_File: my1log.000001 + 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: 587 + Relay_Log_Space: 1016 + 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: 146f4cae-452e-11ed-b87f-000c29311164 + Master_Info_File: mysql.slave_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: 146f4cae-452e-11ed-b87f-000c29311164:1-2 + Executed_Gtid_Set: 146f4cae-452e-11ed-b87f-000c29311164:1-2 + Auto_Position: 1 + Replicate_Rewrite_DB: + Channel_Name: master-1 + Master_TLS_Version: +1 row in set (0.00 sec) + + +mysql> CHANGE MASTER TO + -> MASTER_HOST='master-2', + -> MASTER_USER='slave', + -> MASTER_PASSWORD='QianFeng@123', + -> MASTER_AUTO_POSITION=1 FOR CHANNEL 'master-2'; +Query OK, 0 rows affected, 2 warnings (0.01 sec) + +mysql> start slave; +Query OK, 0 rows affected, 1 warning (0.00 sec) + +mysql> show slave status\G +*************************** 1. row *************************** + Slave_IO_State: Waiting for master to send event + Master_Host: master-1 + Master_User: slave + Master_Port: 3306 + Connect_Retry: 60 + Master_Log_File: my1log.000001 + Read_Master_Log_Pos: 587 + Relay_Log_File: slave-1-relay-bin-master@002d1.000002 + Relay_Log_Pos: 794 + Relay_Master_Log_File: my1log.000001 + 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: 587 + Relay_Log_Space: 1016 + 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: 146f4cae-452e-11ed-b87f-000c29311164 + Master_Info_File: mysql.slave_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: 146f4cae-452e-11ed-b87f-000c29311164:1-2 + Executed_Gtid_Set: 146f4cae-452e-11ed-b87f-000c29311164:1-2 + Auto_Position: 1 + Replicate_Rewrite_DB: + Channel_Name: master-1 + Master_TLS_Version: +*************************** 2. row *************************** + Slave_IO_State: Waiting for master to send event + Master_Host: master-2 + Master_User: slave + Master_Port: 3306 + Connect_Retry: 60 + Master_Log_File: my2log.000001 + Read_Master_Log_Pos: 154 + Relay_Log_File: slave-1-relay-bin-master@002d2.000002 + Relay_Log_Pos: 361 + Relay_Master_Log_File: my2log.000001 + 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: 154 + Relay_Log_Space: 583 + 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: 2 + Master_UUID: 1b453d94-452e-11ed-b744-000c2936b606 + Master_Info_File: mysql.slave_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: 146f4cae-452e-11ed-b87f-000c29311164:1-2 + Auto_Position: 1 + Replicate_Rewrite_DB: + Channel_Name: master-2 + Master_TLS_Version: +2 rows in set (0.00 sec) +``` + +#### 6.Slave-2部署 + +安装数据库:(略) + +启动数据库:(略) + +修改数据库初始密码:(略) + +从服务器二部署: + +```shell +[root@slave-2 ~]# vim /etc/my.cnf +log-bin = my4log +server-id = 4 +gtid_mode=ON +enforce_gtid_consistency=1 +relay_log_info_repository = TABLE +master_info_repository = TABLE +relay_log_recovery = on +``` + +重启服务: + +```shell +[root@slave-2 ~]# systemctl restart mysqld +``` + +从连接主服务器: + +```shell +[root@slave-2 ~]# mysql -u root -pQianFeng@123 +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 2 +Server version: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> CHANGE MASTER TO + -> MASTER_HOST='master-1', + -> MASTER_USER='slave', + -> MASTER_PASSWORD='QianFeng@123', + -> MASTER_AUTO_POSITION=1 FOR CHANNEL 'master-1'; +Query OK, 0 rows affected, 2 warnings (0.01 sec) + +mysql> start slave; +Query OK, 0 rows affected (0.00 sec) + +mysql> CHANGE MASTER TO + -> MASTER_HOST='master-2', + -> MASTER_USER='slave', + -> MASTER_PASSWORD='QianFeng@123', + -> MASTER_AUTO_POSITION=1 FOR CHANNEL 'master-2'; +Query OK, 0 rows affected, 2 warnings (0.00 sec) + +mysql> start slave; +Query OK, 0 rows affected, 1 warning (0.01 sec) + +mysql> show slave status\G +*************************** 1. row *************************** + Slave_IO_State: Waiting for master to send event + Master_Host: master-1 + Master_User: slave + Master_Port: 3306 + Connect_Retry: 60 + Master_Log_File: my1log.000001 + Read_Master_Log_Pos: 587 + Relay_Log_File: slave-2-relay-bin-master@002d1.000002 + Relay_Log_Pos: 794 + Relay_Master_Log_File: my1log.000001 + 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: 587 + Relay_Log_Space: 1016 + 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: 146f4cae-452e-11ed-b87f-000c29311164 + Master_Info_File: mysql.slave_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: 146f4cae-452e-11ed-b87f-000c29311164:1-2 + Executed_Gtid_Set: 146f4cae-452e-11ed-b87f-000c29311164:1-2 + Auto_Position: 1 + Replicate_Rewrite_DB: + Channel_Name: master-1 + Master_TLS_Version: +*************************** 2. row *************************** + Slave_IO_State: Waiting for master to send event + Master_Host: master-2 + Master_User: slave + Master_Port: 3306 + Connect_Retry: 60 + Master_Log_File: my2log.000001 + Read_Master_Log_Pos: 154 + Relay_Log_File: slave-2-relay-bin-master@002d2.000002 + Relay_Log_Pos: 361 + Relay_Master_Log_File: my2log.000001 + 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: 154 + Relay_Log_Space: 583 + 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: 2 + Master_UUID: 1b453d94-452e-11ed-b744-000c2936b606 + Master_Info_File: mysql.slave_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: 146f4cae-452e-11ed-b87f-000c29311164:1-2 + Auto_Position: 1 + Replicate_Rewrite_DB: + Channel_Name: master-2 + Master_TLS_Version: +2 rows in set (0.00 sec) +``` + +#### 7.验证 + +主服务器创建数据: + +```shell +[root@master-1 ~]# mysql -u root -pQianFeng@123 +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: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> create database qfcloud; +Query OK, 1 row affected (0.00 sec) + +mysql> exit +Bye +``` + +其他服务器验证: + +```shell +[root@master-2 ~]# mysql -u root -pQianFeng@123 +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: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| mysql | +| performance_schema | +| qfcloud | +| sys | ++--------------------+ +5 rows in set (0.00 sec) + +[root@slave-1 ~]# mysql -u root -pQianFeng@123 +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: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| mysql | +| performance_schema | +| qfcloud | +| sys | ++--------------------+ +5 rows in set (0.00 sec) + +[root@slave-2 ~]# mysql -u root -pQianFeng@123 +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 7 +Server version: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, 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> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| mysql | +| performance_schema | +| qfcloud | +| sys | ++--------------------+ +5 rows in set (0.00 sec) +```