Day49——Linux入门学习3(Mysql服务)

文章目录

      • 磁盘管理
      • Mysql服务
      • 主从复制
        • 设置复制配置
        • 设置复制从站
        • 测试
        • 使用GTID进行主从复制
        • 半同步复制
          • 机制
        • 延迟复制
        • 慢查询

磁盘管理

Day49——Linux入门学习3(Mysql服务)_第1张图片

添加一块硬盘,如下

Day49——Linux入门学习3(Mysql服务)_第2张图片

下一步

Day49——Linux入门学习3(Mysql服务)_第3张图片

下一步

Day49——Linux入门学习3(Mysql服务)_第4张图片

继续下一步,不用改变最大磁盘大小

Day49——Linux入门学习3(Mysql服务)_第5张图片

完成,确定

进行查看

[root@server1 ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000bf993

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/rhel-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/rhel-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

上面的Disk /dev/sdb: 21.5 GB, 21474836480 bytes…就自动添加上来了

拿到一块硬盘如何操作?

  1. 分区
  2. 格式化
  3. 挂载(相当是访问入口,多个挂载点就有多个入口)

tips:超过1TB的硬盘,分区时使用parted ,GPT分区(这种分区方式对磁盘的分区数量没有限制)

fdisk : 传统分区,四个主分区,划扩展分区,在扩展分区里面划逻辑分区

服务器的磁盘必须支持热插拔~

第一,分区:

[root@server1 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xa18757be.

Command (m for help): m   m获取帮助
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

新建:

p 主分区,e 扩展分区

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 直接回车,默认主分区
Partition number (1-4, default 1): 默认从1开始,回车
First sector (2048-41943039, default 2048): 选择扇区,回车
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): (这里要是直接回车,那就是整个盘分了一个区)
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G 分了10个G
Partition 1 of type Linux and of size 10 GiB is set

打印一下刚分区的信息

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x4e6113aa

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux

只要在Command (m for help): 这里不输入w,这一切都不会保存的

Command (m for help): w 保存
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

在proc(Linux内核真正识别的设备)中查看

[root@server1 ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0   20971520 sda
   8        1    1048576 sda1
   8        2   19921920 sda2
   8       16   20971520 sdb
   8       17   10485760 sdb1  这个就是刚保存成功的磁盘
  11        0    4391936 sr0
 253        0   17821696 dm-0
 253        1    2097152 dm-1

第二,格式化!

在rhel7 中默认推荐使用xfs文件系统,它可支持多达8EB的容量

[root@server1 ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2621440, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

第三,挂载!

先定义个目录

[root@server1 ~]# mkdir /data1

再挂

[root@server1 ~]# mount /dev/sdb1 /data1/

后续操作:

进入data1中,存点数据搁里面

[root@server1 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  redis-5.0.5.tar.gz
[root@server1 ~]# cp redis-5.0.5.tar.gz /data1/
[root@server1 ~]# cd /data1/
[root@server1 data1]# ls
redis-5.0.5.tar.gz

查看分区挂载

[root@server1 data1]# df
Filesystem            1K-blocks     Used Available Use% Mounted on
/dev/mapper/rhel-root  17811456 10180676   7630780  58% /
devtmpfs                 996972        0    996972   0% /dev
tmpfs                   1014056        0   1014056   0% /dev/shm
tmpfs                   1014056    10692   1003364   2% /run
tmpfs                   1014056        0   1014056   0% /sys/fs/cgroup
/dev/sr0                4391278  4391278         0 100% /media/cdrom
/dev/sda1               1038336   168000    870336  17% /boot
tmpfs                    202812        4    202808   1% /run/user/42
tmpfs                    202812       28    202784   1% /run/user/1000
/dev/sdb1              10475520    34924  10440596   1% /data1

但是,机器重启之后,这个还需重新再次mount动作

如何自动挂载?

vim /etc/fstab,系统会自动读取这里面的挂载项,在开机时自动挂载

/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=7bb94be1-1908-46f3-9bcd-926672d9c0df /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
/dev/sr0                /media/cdrom            iso9660 defaults        0 0
/dev/sdb1(设备)               /data1(挂载点)                  xfs(文件系统类型)     defaults(默认的挂载选项)        0(不检测) 0(不记录)

验证一下:

先卸载刚才的挂载目录

[root@server1 ~]# umount /data1/
[root@server1 ~]# umount /data1/
umount: /data1/: not mounted
[root@server1 ~]# df
刚才的挂载发现已经不在

再次挂载

[root@server1 ~]# mount -a  挂载所有,会从fstab从读取的
[root@server1 ~]# df
刚才的挂载发现就在了

再来查看下分区状况

[root@server1 ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x4e6113aa

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux

因为还有10个G,再次尝试分区

[root@server1 ~]# fdisk /dev/sdb
...
Command (m for help): n 
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): 
Using default response p
Partition number (2-4, default 2): 现在就从2开始了

First sector (20973568-41943039, default 20973568): 
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): 
Using default value 41943039
Partition 2 of type Linux and of size 10 GiB is set
将剩下的10G都给它

Command (m for help): p
....
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux
/dev/sdb2        20973568    41943039    10484736   83  Linux

Command (m for help): w
The partition table has been altered!

但是,保存之后有警告!

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.

它想让用户重启,但是线上情况不允许重启

[root@server1 ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0   20971520 sda
   8        1    1048576 sda1
   8        2   19921920 sda2
   8       16   20971520 sdb
   8       17   10485760 sdb1
  11        0    4391936 sr0
 253        0   17821696 dm-0
 253        1    2097152 dm-1

说明Linux内核没有刷新,它读取不到刚才分的磁盘。

怎么解决?

使用partprobe指令 ,相当于刷新了内核分区表

[root@server1 ~]# partprobe 
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.

再次查看

[root@server1 ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0   20971520 sda
   8        1    1048576 sda1
   8        2   19921920 sda2
   8       16   20971520 sdb
   8       17   10485760 sdb1  第一次分的
   8       18   10484736 sdb2  第二次分的
  11        0    4391936 sr0
 253        0   17821696 dm-0
 253        1    2097152 dm-1

接着格式化!

用另一个文件系统

[root@server1 ~]# mkfs.ext4 /dev/sdb2
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621184 blocks
131059 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

再创建个目录,进行挂载

[root@server1 ~]# mkdir /data2
[root@server1 ~]# mount  /dev/sdb2 /data2

拷贝文件到新建目录

[root@server1 data2]# cp ~/redis-5.0.5.tar.gz .
[root@server1 data2]# ls
lost+found  redis-5.0.5.tar.gz

若是希望开机挂载,就是在fstab中进行添加

删除分区?

先要在系统层面,把挂载去掉,不要在这个需卸载的目录里,执行umount

[root@server1 ~]# umount /data2/

接着

[root@server1 ~]# fdisk /dev/sdb
进行操作

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x4e6113aa

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux
/dev/sdb2        20973568    41943039    10484736   83  Linux

Command (m for help): d
Partition number (1,2, default 2): 回车,默认删2
Partition 2 is deleted

Command (m for help): p
....
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux
删掉了2分区

保存退出即可

还是需要用partprobe进行刷新一下

[root@server1 ~]# partprobe 
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
[root@server1 ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0   20971520 sda
   8        1    1048576 sda1
   8        2   19921920 sda2
   8       16   20971520 sdb
   8       17   10485760 sdb1  只有1分区了
  11        0    4391936 sr0
 253        0   17821696 dm-0
 253        1    2097152 dm-1

假设磁盘空间不够,怎么办?

[root@server1 ~]# df -h /data1
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1        10G   35M   10G   1% /data1
这里假设满了,准备将这个拉伸一下

拉伸到20个G

如何操作?像这样的标准分区是没法拉伸的

but,比如像根文件系统万一不够了,如何热拉伸?

[root@server1 ~]# df -h 
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   17G  9.8G  7.3G  58% /

将空盘加到根文件系统。

  1. 先将fstab中自动挂载的盘去掉
[root@server1 ~]# vim /etc/fstab 
[root@server1 ~]# df
Filesystem            1K-blocks     Used Available Use% Mounted on
/dev/mapper/rhel-root  17811456 10180716   7630740  58% /
devtmpfs                 996972        0    996972   0% /dev
tmpfs                   1014056        0   1014056   0% /dev/shm
tmpfs                   1014056    10692   1003364   2% /run
tmpfs                   1014056        0   1014056   0% /sys/fs/cgroup
/dev/sda1               1038336   168000    870336  17% /boot
tmpfs                    202812        4    202808   1% /run/user/42
tmpfs                    202812       28    202784   1% /run/user/1000
/dev/sdb1              10475520    34924  10440596   1% /data1
/dev/sr0                4391278  4391278         0 100% /media
[root@server1 ~]# umount /data1/
[root@server1 ~]# df
..没有/data1即可
  1. 删掉sdb1
[root@server1 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x4e6113aa

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux

Command (m for help): d
Selected partition 1
Partition 1 is deleted

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x4e6113aa

   Device Boot      Start         End      Blocks   Id  System

记得保存

proc中查看

[root@server1 ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0   20971520 sda
   8        1    1048576 sda1
   8        2   19921920 sda2
   8       16   20971520 sdb
  11        0    4391936 sr0
 253        0   17821696 dm-0
 253        1    2097152 dm-1

显示当前的逻辑卷:

[root@server1 ~]# lvs
  LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root rhel -wi-ao---- <17.00g                                                    
  swap rhel -wi-ao----   2.00g  

如何扩展到根上,才是关键

必须是基于LVM分区的(红帽子默认就是LVM分区),才能拉伸

[root@server1 ~]# pvcreate /dev/sdb
WARNING: dos signature detected on /dev/sdb at offset 510. Wipe it? [y/n]: y
  Wiping dos signature on /dev/sdb.
  Physical volume "/dev/sdb" successfully created.

用pvs命令扫描

[root@server1 ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree 
  /dev/sda2  rhel lvm2 a--  <19.00g     0 
  /dev/sdb        lvm2 ---   20.00g 20.00g

将底层的物理磁盘打成pv(物理卷)

查看卷组

[root@server1 ~]# vgs
\  VG   #PV #LV #SN Attr   VSize   VFree
  rhel   1   2   0 wz--n- <19.00g    0 

扩展:

  1. rhel通过/dev/sdb来扩展
[root@server1 ~]# vgextend rhel /dev/sdb
  Volume group "rhel" successfully extended

[root@server1 ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree  
  rhel   2   2   0 wz--n- 38.99g <20.00g
  1. lvextend
[root@server1 ~]# lvextend -L +20G /dev/rhel/root 
  Insufficient free space: 5120 extents needed, but only 5119 available

vgs里面的free是小于20G的,所以这里不够,于是

[root@server1 ~]# lvextend -l +5119 /dev/rhel/root 
  Size of logical volume rhel/root changed from <17.00 GiB (4351 extents) to 36.99 GiB (9470 extents).
  Logical volume rhel/root successfully resized.

lvs查看一下

[root@server1 ~]# lvs
\  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root rhel -wi-ao---- 36.99g                                                    
  swap rhel -wi-ao----  2.00g     

逻辑卷扩展了,但是根还未扩展!

做拉伸,将这个root拉伸到与逻辑卷相同大小

[root@server1 ~]# xfs_growfs /dev/rhel/root 
meta-data=/dev/mapper/rhel-root  isize=512    agcount=4, agsize=1113856 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=4455424, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 4455424 to 9697280

再次查看

[root@server1 ~]# df -h /
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   37G  9.8G   28G  27% /

Mysql服务

需要的文件:mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar

进行解压~

[root@server1 ~]# tar xf mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar

[root@server1 mysql]# ls
mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-8.0.19-1.el7.x86_64.rpm
mysql-community-common-8.0.19-1.el7.x86_64.rpm
mysql-community-devel-8.0.19-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.19-1.el7.x86_64.rpm
mysql-community-libs-8.0.19-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.19-1.el7.x86_64.rpm
mysql-community-server-8.0.19-1.el7.x86_64.rpm
mysql-community-test-8.0.19-1.el7.x86_64.rpm

卸载系统自带的mariadb-lib

  • 查看mariadb版本
    rpm -qa | grep mariadb
  • 卸载mariadb
    rpm -e mariadb-libs-xxxxx.el7.x86_64 --nodeps

重启reboot后

进行顺序安装:

rpm -ivh mysql-community-common-8.0.13-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.13-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.13-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.13-1.el7.x86_64.rpm

或者安装使用如下命令:

[root@server1 ~]# yum install mysql-community-client-8.0.19-1.el7.x86_64.rpm mysql-community-common-8.0.19-1.el7.x86_64.rpm mysql-community-libs-8.0.19-1.el7.x86_64.rpm mysql-community-libs-compat-8.0.19-1.el7.x86_64.rpm mysql-community-server-8.0.19-1.el7.x86_64.rpm 

mysql-community-libs-compat-8.0.19-1.el7.x86_64.rpm 这个会替换rhel自带的mariadb

安装好了之后,启动数据库

[root@server1 ~]# systemctl start mysqld
[root@server1 ~]# 

临时密码

[root@server1 ~]# cat /var/log/mysqld.log 
2020-03-26T10:03:21.443084Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.19) initializing of server in progress as process 8238
2020-03-26T10:03:22.951112Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: _D:!UwNe;8Pm

数据库初始化脚本的执行:

[root@server1 ~]# mysql_secure_installation 

Enter password for user root:  此处粘贴上面的那个密码

然后输入新密码:(大小写数字特殊字符)

Change the password for root ?  No

后面一路y 就可以了

使用mysql

[root@server1 ~]# mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

显示数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

主从复制

进行两台虚拟机的配置

Day49——Linux入门学习3(Mysql服务)_第6张图片

配置好两台机子的ip地址,mysql

需要将selinux进行配置

[root@server1 ~]# cat /etc/sysconfig/selinux

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled   改为disabled


reboot之后才生效

关闭防火墙

[root@server1 ~]# systemctl disable --now firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

一主多从:应用于读远远高于写的场景

参考Mysql官网学习

https://dev.mysql.com/doc/refman/8.0/en/binlog-replication-configuration-overview.html

设置复制配置

一定要注意,因为我是在虚拟机中克隆出来的,所以要:

两台机子的ip地址;

修改UUid,用下面命令查看:保证主机和从机的不一致~这样就不会导致后面的IO进程是No了

cat /var/lib/mysql/auto.cnf

数据库就需要设置不一样的serverid

主的

[root@server1 ~]# vim /etc/my.cnf
.....
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

server-id=1
必须是正整数
[root@server2 ~]# systemctl restart mysqld
需要重启一下

从的

[root@server2 ~]# vim /etc/my.cnf
....
....
server-id=2
[root@server2 ~]# systemctl restart mysqld
需要重启一下

给不同的节点,给不同的Serverid

主:

查看master当前状态

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 |      155 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

对于slaver来说,复制要从binlog.000007文件开始

从机连接主机会存在一个用户名密码

查看插件:

mysql> show plugins;
.... 
mysql_native_password  有更好的兼容性密码插件
.....
默认使用下面:
 caching_sha2_password           | ACTIVE   | AUTHENTICATION     | NULL    | GP

tips:

mysql> CREATE USER 'repl'@'%.example.com' IDENTIFIED BY 'password'; 这里直接回车,会默认使用上面的身份验证插件

这里使用兼容性更好的插件,创建用户:

mysql> CREATE USER 'repl'@'192.168.43.%' IDENTIFIED with mysql_native_password  BY 'Westos+007';
Query OK, 0 rows affected (0.01 sec)

授权:

mysql> grant replication slave on *.* to 'repl'@'192.168.43.%';
Query OK, 0 rows affected (0.00 sec)

上面配置好了以后,server2就可以链接过来哦

server2中:

[root@server2 ~]# mysql -h 192.168.43.100 -u repl -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

从机只有一个复制权限:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
验证一下主从好不好用

主机:

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 |      680 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

主从复制的依据是二进制日志

主从复制前提是主和从中数据应该是一致的

设置复制从站

mysql -p登录slave

要设置从服务器与主服务器进行通信以进行复制,请为从服务器配置必要的连接信息。

mysql> CHANGE MASTER TO MASTER_HOST='192.168.43.100', MASTER_USER='repl',MASTER_PASSWORD='Westos+007', MASTER_LOG_FILE='binlog.000007', MASTER_LOG_POS=680;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

启动:

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

查看slave的状态:

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.43.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000002
          Read_Master_Log_Pos: 847
               Relay_Log_File: server2-relay-bin.000002
                Relay_Log_Pos: 486
        Relay_Master_Log_File: binlog.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
.....

Day49——Linux入门学习3(Mysql服务)_第7张图片

测试

在主机上创建:

mysql> create database westos;
Query OK, 1 row affected (0.01 sec)

mysql> use westos
Database changed
mysql> create table tb1 (
    -> id int not null,
    -> name varchar(20) not null);
Query OK, 0 rows affected (0.06 sec)

mysql> desc tb1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | NO   |     | NULL    |       |
| name  | varchar(20) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

注意:主从复制为单向的!

主机中创建好之后,在从机中查看:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| westos             |
+--------------------+
5 rows in set (0.01 sec)

多了一个westos
mysql> use westos
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> select * from tb1;
+----+-------+
| id | name  |
+----+-------+
|  1 | user1 |
|  2 | user2 |
+----+-------+
2 rows in set (0.00 sec)

当主机再插入一个数据后,从机:

mysql> select * from tb1;
+----+-------+
| id | name  |
+----+-------+
|  1 | user1 |
|  2 | user2 |
|  3 | user3 |
+----+-------+
3 rows in set (0.00 sec)

使用GTID进行主从复制

slave上设置时,不再配置主机的那个二进制文件和位置,自动定位~

CHANGE MASTER TO MASTER_HOST='192.168.43.100', MASTER_USER='repl',MASTER_PASSWORD='Westos+007',MASTER_AUTO_POSITION = 1;

对正在运行的slave停止

对主机和从机上配置参数

在mysql的主配置文件中

[root@server1 ~]# vim /etc/my.cnf

.....
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

server-id=1

gtid_mode=ON
enforce-gtid-consistency=ON

主机从机配置完,重启mysql服务,使之生效

在从机:重启之后,自动启动slave了

[root@server2 ~]# mysql -p

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.43.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000003
          Read_Master_Log_Pos: 155
               Relay_Log_File: server2-relay-bin.000005
                Relay_Log_Pos: 363
        Relay_Master_Log_File: binlog.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
.....

首先要停止slave

mysql> stop slave;

执行指令:

mysql> CHANGE MASTER TO MASTER_HOST='192.168.43.100', MASTER_USER='repl',MASTER_PASSWORD='Westos+007',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> use westos
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> insert into tb1 values (4,'user4');

从机处:

mysql> select * from westos.tb1;
+----+-------+
| id | name  |
+----+-------+
|  1 | user1 |
|  2 | user2 |
|  3 | user3 |
|  4 | user4 |
+----+-------+
4 rows in set (0.00 sec)

成功~

IO线程:通过IO线程复制主库的二进制日志文件

主机处的进程:

mysql> show processlist;
+----+-----------------+---------------+--------+------------------+------+---------------------------------------------------------------+------------------+
| Id | User            | Host          | db     | Command          | Time | State                                                         | Info             |
+----+-----------------+---------------+--------+------------------+------+---------------------------------------------------------------+------------------+
|  4 | event_scheduler | localhost     | NULL   | Daemon           |  729 | Waiting on empty queue                                        | NULL             |
|  9 | repl            | server2:59716 | NULL   | Binlog Dump GTID |  408 | Master has sent all binlog to slave; waiting for more updates | NULL             |
| 10 | root            | localhost     | westos | Query            |    0 | starting                                                      | show processlist |
+----+-----------------+---------------+--------+------------------+------+---------------------------------------------------------------+------------------+
3 rows in set (0.00 sec)

从机处的进程:

mysql> show processlist;
+----+-----------------+-----------------+------+---------+-------+--------------------------------------------------------+------------------+
| Id | User            | Host            | db   | Command | Time  | State                                                  | Info             |
+----+-----------------+-----------------+------+---------+-------+--------------------------------------------------------+------------------+
|  5 | event_scheduler | localhost       | NULL | Daemon  |   568 | Waiting on empty queue                                 | NULL             |
| 10 | root            | localhost       | NULL | Query   |     0 | starting                                               | show processlist |
| 11 | system user     | connecting host | NULL | Connect |   287 | Waiting for master to send event                       | NULL             |
| 12 | system user     |                 | NULL | Query   | 12176 | Slave has read all relay log; waiting for more updates | NULL             |
+----+-----------------+-----------------+------+---------+-------+--------------------------------------------------------+------------------+
4 rows in set (0.00 sec)

半同步复制

生产环境中,半同步复制会设置为无穷大

默认数据库复制用的是异步~一旦主机有了数据写入,就会把数据经过网络发送到从机上

在主机上查看半同步插件

mysql> show plugins;

没有,手动安装

mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.02 sec)

在从机上也进行安装插件

mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';

主机中:

mysql> show variables like 'rpl%';
+-------------------------------------------+------------+
| Variable_name                             | Value      |
+-------------------------------------------+------------+
| rpl_read_size                             | 8192       |
| rpl_semi_sync_master_enabled              | OFF        | 半同步复制没开
| rpl_semi_sync_master_timeout              | 10000      | 在生产环境中这个值设置为无穷大
| rpl_semi_sync_master_trace_level          | 32         |
| rpl_semi_sync_master_wait_for_slave_count | 1          |
| rpl_semi_sync_master_wait_no_slave        | ON         |
| rpl_semi_sync_master_wait_point           | AFTER_SYNC |
| rpl_stop_slave_timeout                    | 31536000   |
+-------------------------------------------+------------+
8 rows in set (0.00 sec)

rpl_semi_sync_master_timeout 为啥要设置无穷大,

因为主机将数据发给slave之后,主机会一直等从机一个回应,主机要确定这个数据已经存到slave之后,主机才会继续提交,如果slave不接受,主机会一直等。但是按照上面的值等10s ,又会变成异步复制。然后又不靠谱了

主机中开启:

mysql> SET GLOBAL rpl_semi_sync_master_enabled =1;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'rpl%';
+-------------------------------------------+------------+
| Variable_name                             | Value      |
+-------------------------------------------+------------+
| rpl_read_size                             | 8192       |
| rpl_semi_sync_master_enabled              | ON         | 开启
| rpl_semi_sync_master_timeout              | 10000      |

从机也要开启:

mysql> SET GLOBAL rpl_semi_sync_slave_enabled =1;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'rpl%';
+---------------------------------+----------+
| Variable_name                   | Value    |
+---------------------------------+----------+
| rpl_read_size                   | 8192     |
| rpl_semi_sync_slave_enabled     | ON       | 
| rpl_semi_sync_slave_trace_level | 32       |
| rpl_stop_slave_timeout          | 31536000 |
+---------------------------------+----------+
4 rows in set (0.01 sec)

已开启,但是未生效

要对slave的IO进程操作:

mysql> stop slave io_thread;
Query OK, 0 rows affected (0.00 sec)

这样IO就停掉了
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.43.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000003
          Read_Master_Log_Pos: 447
               Relay_Log_File: server2-relay-bin.000002
                Relay_Log_Pos: 655
        Relay_Master_Log_File: binlog.000003
             Slave_IO_Running: No
            Slave_SQL_Running: Yes

再开启
mysql> start slave io_thread;
Query OK, 0 rows affected (0.00 sec)

这样刚才的配置才能真正生效!


在主机:

mysql> use westos
Database changed

mysql> insert into tb1 values (5,'user5');
Query OK, 1 row affected (0.00 sec)

查看状态:

mysql> show status like 'rpl%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |
| Rpl_semi_sync_master_net_avg_wait_time     | 0     |
| Rpl_semi_sync_master_net_wait_time         | 0     |
| Rpl_semi_sync_master_net_waits             | 1     |
| Rpl_semi_sync_master_no_times              | 0     |
| Rpl_semi_sync_master_no_tx                 | 0     |
| Rpl_semi_sync_master_status                | ON    |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 665   |
| Rpl_semi_sync_master_tx_wait_time          | 665   |
| Rpl_semi_sync_master_tx_waits              | 1     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 1     | 这里是通过半同步复制成功的记录
+--------------------------------------------+-------+
14 rows in set (0.01 sec)

当再插入一条数据时:
Rpl_semi_sync_master_yes_tx 对于的值就会增加1!

当失败时:

Rpl_semi_sync_master_no_tx 这个值会记录!

从机处查看数据库

mysql> select * from westos.tb1;
+----+-------+
| id | name  |
+----+-------+
|  1 | user1 |
|  2 | user2 |
|  3 | user3 |
|  4 | user4 |
|  5 | user5 |
|  5 | user5 |
|  6 | user6 |
+----+-------+
7 rows in set (0.00 sec)

做个小测试:

将slave的IO进程停掉

mysql> stop slave io_thread;
Query OK, 0 rows affected (0.00 sec)

这样从机就不接收了,会等10s

主机继续插入数据:

mysql> insert into tb1 values (7,'user7');
这里会等10s
Query OK, 1 row affected (10.00 sec)

以后再写数据,就不在等待,因为变成了异步复制!

从机现在的状态:

mysql> show status like 'rpl%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | OFF   |
+----------------------------+-------+
1 row in set (0.00 sec)
不支持半同步了

主机中的 Rpl_semi_sync_master_no_tx 这个值也变化了

mysql> show status like 'rpl%';

| Rpl_semi_sync_master_net_waits             | 3     |

如果在从机中将IO打开,会快速的支持半同步

mysql> start slave io_thread;
Query OK, 0 rows affected (0.00 sec)

mysql> show status like 'rpl%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON    |
+----------------------------+-------+
1 row in set (0.00 sec)

主机处的半同步状态也为ON 了

机制

在mysql 5.7 之前 所用的机制为After_Commit,它做不到无损;

像8.0:使用的是AFTER_SYNC(5.7加入的)

mysql> show variables like 'rpl%';

| rpl_semi_sync_master_wait_point           | AFTER_SYNC |

延迟复制

对一些错误行为有挽救的机会

配置一下

先在slave端,停掉Slave

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

使用该 CHANGE MASTER TO MASTER_DELAY=N语句将延迟设置为*N*秒

mysql> CHANGE MASTER TO MASTER_DELAY=30;
Query OK, 0 rows affected (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

查看slave状态

mysql> show slave status\G;

....
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 30

SQL存在延迟!

主机中插入数据:

mysql> insert into tb1 values (9,'user9');
Query OK, 1 row affected (0.01 sec)

但是在slave中查询,会没有,30秒之后,读取日志中的值

慢查询

主机中:

查看慢查询

mysql> show variables like 'slow%';
+---------------------+---------------------------------+
| Variable_name       | Value                           |
+---------------------+---------------------------------+
| slow_launch_time    | 2                               |
| slow_query_log      | OFF                             | 默认关闭
| slow_query_log_file | /var/lib/mysql/server1-slow.log | 
+---------------------+---------------------------------+
3 rows in set (0.00 sec)

开启:

mysql> set global slow_query_log=1;
Query OK, 0 rows affected (0.00 sec)

查看默认的长查询时间:

mysql> show variables like 'long%';
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.01 sec)

默认是10秒

当有查询语句执行时间超过10s之后,会被记录在/var/lib/mysql/server1-slow.log的日志文件中,方便后面的分析

你可能感兴趣的:(Day49——Linux入门学习3(Mysql服务))