mysql主从

文章目录

mysql主从
1.主从介绍
1.1 主从的作用
1.2主从的形式
2.主从复制原理
3. 主从复制配置

mysql主从

1.主从介绍

MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的

1.1 主从的作用

实时灾备,用于故障切换
读写分离,提供查询服务
备份,避免影响业务

1.2主从的形式

1.一主一从
2. 主主复制
3. 一主多从—扩展系统读取的性能,因为读是在从库读取的
4. 多主一从—5.7开始支持
5. 联级复制

2.主从复制原理
主从复制过程:

主上面把所有的写操作记录到binlog日志生成logdump线程,从上面有两个线程,I/O线程和SQL线程,I/O线程去请求主上面的binlog日志,而主上面通过logdump线程发送binlog日志到到从服务器上,从服务器收到了请求的binlog日志之后,写到本地的relaylog日志(中继日志)上面,sql线程去读取relaylog日志,重放relaylog日志的内容,并解析成具体操作重新执行一遍,实现从上面的数据和主上面的数据完全一样。

  1. 主从复制配置

主从复制配置步骤:

确保从数据库与主数据库里的数据一样
在主数据库里创建一个同步账号授权给从数据库使用
配置主数据库(修改配置文件)
配置从数据库(修改配置文件)

需求:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作

环境说明:

数据库角色IP应用与系统版本有无数据
主数据库172.16.195.119centos7/redhat7mysql-5.7有数据
从数据库172.16.195.120centos7/redhat7mysql-5.7无数据

mysql安装

关闭防火墙和selinux

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# getenforce
Disabled

创建用户和组

[root@localhost src]# groupadd -r -g 306 mysql
[root@localhost src]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

下载二进制格式的mysql软件包

[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

解压软件至/usr/local/

[root@localhost src]# ls
debug kernels mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# ls /usr/local/
bin games lib libexec sbin src
etc include lib64 mysql-5.7.22-linux-glibc2.12-x86_64 share
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’

修改目录/usr/local/mysql的属主属组

[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql

添加环境变量

[root@localhost ~]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
[root@localhost ~]# echo ‘export PATH=/usr/local/mysql/bin:$PATH’ > /etc/profile.d/mysql.sh
[root@localhost ~]# . /etc/profile.d/mysql.sh
[root@localhost ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

建立数据存放目录

[root@localhost mysql]# mkdir /opt/data
[root@localhost mysql]# chown -R mysql.mysql /opt/data/
[root@localhost mysql]# ll /opt/
total 0
drwxr-xr-x 2 mysql mysql 6 Aug 14 16:54 data

初始化数据库

[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-08-15T07:57:46.168380Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-15T07:57:50.542516Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-08-15T07:57:50.927286Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-08-15T07:57:51.071260Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e8600890-a060-11e8-b1a2-000c294c50b4.
2018-08-15T07:57:51.074566Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
2018-08-15T07:57:51.078089Z 1 [Note] A temporary password is generatedfor root@localhost: hfGdwnViq6,
#请注意,这个命令的最后会生成一个临时密码,此处密码是hfGdwnViq6,

配置mysql

[root@localhost ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
‘/usr/local/include/mysql’ -> ‘/usr/local/mysql/include/’
[root@localhost ~]# echo ‘/usr/local/mysql/lib’ > /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# ldconfig -v

生成配置文件

[root@localhost ~]# cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
EOF

配置服务启动脚本

[root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# sed -ri ‘s#^(basedir=).#\1/usr/local/mysql#g’ /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(datadir=).
#\1/opt/data#g’ /etc/init.d/mysqld

启动mysql

[root@localhost ~]# service mysqld start
Starting MySQL… SUCCESS!
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :22 :
LISTEN 0 100 127.0.0.1:25 :
LISTEN 0 128 :::22 :::

LISTEN 0 100 ::1:25 :::
LISTEN 0 80 :::3306 :::

修改密码

使用临时密码登录

[root@localhost ~]# mysql -uroot -p‘hfGdwnViq6,’
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22

Copyright © 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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

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

mysql>

设置新密码

mysql> set password = password(‘shicailun123!’);
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye

保从数据库与主数据库里的数据一样

查看主库有哪些库

mysql> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| scl |
| sys |
±-------------------+

查看从库中有哪些库

mysql> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
±-------------------+

主库比从库多了一个scl库,查看主库中scl库有哪些表

mysql> use scl
Database changed
mysql> show tables;
±--------------+
| Tables_in_scl |
±--------------+
| student |
±--------------+

查看scl表中的内容

mysql> select * from student;
±—±----------±-----+
| id | name | age |
±—±----------±-----+
| 1 | zhangshan | 20 |
| 2 | lisi | 21 |
| 3 | wangwu | 22 |
| 4 | zhaoliu | 23 |
±—±----------±-----+

#全备主库
#全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致,加锁命令如下
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)

mysql>
#此锁住的终端必须在另一终端备份完成后才能退出

#备份主库并将备份文件传送到从库
[root@master ~]# mysqldump -uroot -pscl666 --all-databases > all-201908202014
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master ~]# ls
all-201908202014

[root@master ~]# scp all-201908202014 root@172.16.195.120:/root/

#解除主库的锁表状态,直接退出交互式界面即可
mysql> quit
Bye

#在从库上恢复主库的备份并查看内容,确保跟主库一致
[root@slave ~]# mysql -uroot -pscl666 < all-201908202014
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@slave ~]# mysql -uroot -pscl666 -e ‘show databases;’
mysql: [Warning] Using a password on the command line interface can be insecure.
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| scl |
| sys |
±-------------------+

[root@slave ~]# mysql -uroot -pscl666 -e ‘select *from scl.student;’
mysql: [Warning] Using a password on the command line interface can be insecure.
±—±----------±-----+
| id | name | age |
±—±----------±-----+
| 1 | zhangshan | 20 |

在主数据库里创建一个同步账号授权给从数据库使用

mysql> create user ‘repl’@‘172.16.195.120’ identified by ‘repl666’;
mysql> grant replication slave on . to ‘repl’@‘172.16.195.120’;
mysql> flush privileges;

配置主数据库

编辑配置文件/etc/my.cnf 在[mysqld]这段后面加上如下内容
[root@master ~]# vim /etc/my.cnf

log-bin = mysql_bin # 启用binlog日志
server-id = 1 # 数据库服务器唯一标识符,主库的server-id值必须比从库的小

重启mysql服务

[root@master ~]# service mysqld restart
Shutting down MySQL… SUCCESS!

查看主库的状态

mysql> show master status;
±-----------------±---------±-------------±-----------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±-----------------±---------±-------------±-----------------±------------------+

配置从数据库

编辑配置文件/etc/my.cnf 在[mysqld]这段后面加上如下内容
[root@slave ~]# vim /etc/my.cnf

relay-log = mysql_relay_bin # 启用relay(中继日志)日志
server-id = 2 # 数据库服务器唯一标识符,从库的server-id值必须比主库的大

重启从库的mysql服务

[root@slave ~]# service mysqld restart
Shutting down MySQL… SUCCESS!

配置并启动主从复制

[root@slave ~]# mysql -uroot -pscl666
mysql> change master to
-> master_host=‘172.16.195.119’,
-> master_user=‘repl’,
-> master_password=‘repl666’,
-> master_log_file=‘mysql_bin.000004’,
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.08 sec)

mysql>

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

查看从服务器的状态

mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 172.16.195.119
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000004
Read_Master_Log_Pos: 154
Relay_Log_File: mysql_relay_bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql_bin.000004
Slave_IO_Running: yes # 此处必须为yes
Slave_SQL_Running: yes # 此处必须为yes
#若配置没问题依然显示no请重启服务后重试

验证:
在主服务器的scl库的student表中插入数据:
mysql> select *from student;
±—±----------±-----+
| id | name | age |
±—±----------±-----+
| 1 | zhangshan | 20 |
| 2 | lisi | 21 |
| 3 | wangwu | 22 |
| 4 | zhaoliu | 23 |
±—±----------±-----+

mysql> insert into student(name,age) values(‘chenxi’,30),(‘chengsong’,35);

mysql> select *from student;
±—±----------±-----+
| id | name | age |
±—±----------±-----+
| 1 | zhangshan | 20 |
| 2 | lisi | 21 |
| 3 | wangwu | 22 |
| 4 | zhaoliu | 23 |
| 5 | chenxi | 30 |
| 6 | chengsong | 35 |

在从数据库中查看数据是否同步:
mysql> select *from scl.student;
±—±----------±-----+
| id | name | age |
±—±----------±-----+
| 1 | zhangshan | 20 |
| 2 | lisi | 21 |
| 3 | wangwu | 22 |
| 4 | zhaoliu | 23 |
| 5 | chenxi | 30 |
| 6 | chengsong | 35 |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于微信小程序的家政服务预约系统采用PHP语言和微信小程序技术,数据库采用Mysql,运行软件为微信开发者工具。本系统实现了管理员和客户、员工三个角色的功能。管理员的功能为客户管理、员工管理、家政服务管理、服务预约管理、员工风采管理、客户需求管理、接单管理等。客户的功能为查看家政服务进行预约和发布自己的需求以及管理预约信息和接单信息等。员工可以查看预约信息和进行接单。本系统实现了网上预约家政服务的流程化管理,可以帮助工作人员的管理工作和帮助客户查询家政服务的相关信息,改变了客户找家政服务的方式,提高了预约家政服务的效率。 本系统是针对网上预约家政服务开发的工作管理系统,包括到所有的工作内容。可以使网上预约家政服务的工作合理化和流程化。本系统包括手机端设计和电脑端设计,有界面和数据库。本系统的使用角色分为管理员和客户、员工三个身份。管理员可以管理系统里的所有信息。员工可以发布服务信息和查询客户的需求进行接单。客户可以发布需求和预约家政服务以及管理预约信息、接单信息。 本功能可以实现家政服务信息的查询和删除,管理员添加家政服务信息功能填写正确的信息就可以实现家政服务信息的添加,点击家政服务信息管理功能可以看到基于微信小程序的家政服务预约系统里所有家政服务的信息,在添加家政服务信息的界面里需要填写标题信息,当信息填写不正确就会造成家政服务信息添加失败。员工风采信息可以使客户更好的了解员工。员工风采信息管理的流程为,管理员点击员工风采信息管理功能,查看员工风采信息,点击员工风采信息添加功能,输入员工风采信息然后点击提交按钮就可以完成员工风采信息的添加。客户需求信息关系着客户的家政服务预约,管理员可以查询和修改客户需求信息,还可以查看客户需求的添加时间。接单信息属于本系统里的核心数据,管理员可以对接单的信息进行查询。本功能设计的目的可以使家政服务进行及时的安排。管理员可以查询员工信息,可以进行修改删除。 客户可以查看自己的预约和修改自己的资料并发布需求以及管理接单信息等。 在首页里可以看到管理员添加和管理的信息,客户可以在首页里进行家政服务的预约和公司介绍信息的了解。 员工可以查询客户需求进行接单以及管理家政服务信息和留言信息、收藏信息等。
数字社区解决方案是一套综合性的系统,旨在通过新基建实现社区的数字化转型,打通智慧城市建设的"最后一公里"。该方案以国家政策为背景,响应了国务院、公安部和中央政法会议的号召,强调了社会治安防控体系的建设以及社区治理创新的重要性。 该方案的建设标准由中央综治办牵头,采用"9+X"模式,通过信息采集、案(事)件流转等手段,实现五级信息中心的互联互通,提升综治工作的可预见性、精确性和高效性。然而,当前社区面临信息化管理手段不足、安全隐患、人员动向难以掌握和数据资源融合难等问题。 为了解决这些问题,数字社区建设目标提出了"通-治-服"的治理理念,通过街道社区、区政府、公安部门和居民的共同努力,实现社区的平安、幸福和便捷。建设思路围绕"3+N"模式,即人工智能、物联网和数据资源,结合态势感知、业务分析和指挥调度,构建起一个全面的数据支持系统。 数字社区的治理体系通过"一张图"实现社区内各维度的综合态势可视化,"一套表"进行业务分析,"一张网"完成指挥调度。这些工具共同提升了社区治理的智能化和效率。同时,数字社区还提供了包括智慧通行、智慧环保、居家养老和便民服务等在内的多样化数字服务,旨在提升居民的生活质量。 在硬件方面,数字社区拥有IOT物联网边缘网关盒子和AI边缘分析盒子,这些设备能够快速集成老旧小区的物联设备,实现传统摄像设备的智能化改造。平台优势体现在数字化能力中台和多样化的应用,支持云、边、端的协同工作,实现模块化集成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值