2024年8月7日(mysql主从 )
回顾
主服务器
[root@master_mysql ~]# yum -y install rsync
[root@master_mysql ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar
[root@master_mysql ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz
[root@master_mysql ~]# cp -r mysql-8.0.33-linux-glibc2.12-x86_64 /usr/local/mysql
[root@master_mysql ~]# cd /usr/local/mysql/
[root@master_mysql mysql]# mkdir mysql-files
[root@master_mysql mysql]# useradd -r -s /sbin/nologin mysql[root@master_mysql mysql]# id mysql
uid=997(mysql) gid=995(mysql) 组=995(mysql)
[root@master_mysql mysql]# chown mysql:mysql ./mysql-files/[root@master_mysql mysql]# rm -rf /etc/my.cnf
[root@master_mysql mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/[root@master_mysql mysql]# ./bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
[root@master_mysql mysql]# cp support-files/mysql.server /etc/init.d/mysql8
[root@master_mysql mysql]# vim my.cnf
[mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data socket=/tmp/mysql.sock port=3306 log-error=/usr/local/mysql/data/db01-master.err log-bin=/usr/local/mysql/data/binlog server-id=10 character_set_server=utf8mb4
[root@master_mysql mysql]# service mysql8 start
. SUCCESS!
[root@master_mysql mysql]# ./bin/mysql -P 3306 -pmysql> alter user 'root'@'localhost' identified by '123456';
mysql> exit[root@master_mysql mysql]# service mysql8 restart
Shutting down MySQL. SUCCESS!
Starting MySQL.. SUCCESS!
从服务器(不用初始化也不用启动)
[root@slave_mysql ~]# yum -y install rsync
[root@slave_mysql ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar
[root@slave_mysql ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz[root@slave_mysql ~]# cp -r mysql-8.0.33-linux-glibc2.12-x86_64 /usr/local/mysql
[root@slave_mysql ~]# useradd -r -s /sbin/nologin mysql
[root@slave_mysql ~]# mkdir /usr/local/mysql/mysql-files
[root@slave_mysql ~]# chown mysql:mysql /usr/local/mysql/mysql-files/[root@slave_mysql ~]# rm -rf /etc/my.cnf
[root@slave_mysql ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql8
主服务器(同步)
[root@master_mysql ~]# rm -rf /usr/local/mysql/data/auto.cnf
[root@master_mysql ~]# service mysql8 stop
Shutting down MySQL. SUCCESS!
[root@master_mysql ~]# rm -rf /usr/local/mysql/data/auto.cnf
[root@master_mysql ~]# ls /usr/local/mysql/data/
[root@master_mysql ~]# rsync -av /usr/local/mysql/data root@192.168.8.165:/usr/local/mysql/ #从服务器IP地址
从服务器
[root@slave_mysql ~]# vim /usr/local/mysql/data/my.cnf
[mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data socket=/tmp/mysql.sock log-error=/usr/local/mysql/data/err.log log-relay=/usr/local/mysql/data/relaylog character_set_server=utf8mb4 server-id=11
[root@slave_mysql ~]# service mysql8 start
[root@slave_mysql ~]# /usr/local/mysql/bin/mysql -p123456
1、 主从复制的实现
[root@master_mysql ~]# vim /etc/profile
export PATH=/usr/local/mysql/bin:$PATH
[root@master_mysql ~]# sorce /etc/profile
mysql> create user 'slave'@'192.168.8.%' identified by 'slave_123' '; Query OK, 0 rows affected (0.01 sec)mysql> grant replication slave on *.* to 'slave'@'192.168.8.%'; Query OK, 0 rows affected (0.01 sec)mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec) mysql> show master status;
[root@slave_mysql ~]# vim /etc/profile
export PATH=/usr/local/mysql/bin:$PATH
[root@slave_mysql ~]# source /etc/profile
[root@slave_mysql ~]# mysql -h192.168.8.164 -uslave -pslave_123 --get-server-public-key
[root@slave_mysql ~]# mysql -p123456 -P3306
mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> reset slave; Query OK, 0 rows affected, 1 warning (0.01 sec)mysql> change master to-> master_host='192.168.8.164',-> master_user='slave',-> master_password='slave_123',-> master_log_file='binlog.000009',-> master_log_pos=447; Query OK, 0 rows affected, 8 warnings (0.00 sec)mysql> start slave; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> show slave status\G;
2、主服务器创建表并向表里添加数据
mysql> unlock tables; Query OK, 0 rows affected (0.00 sec)mysql> create database if not exists test charset utf8mb4; Query OK, 1 row affected (0.00 sec)mysql> use test; Database changed mysql> create table student(id int primary key,name varchar(45) not null,gender varchar(4) not null); Query OK, 0 rows affected (0.01 sec)mysql> insert into student values(1,'张三','男'); Query OK, 1 row affected (0.02 sec)mysql> insert into student values(2,'凤凰','女'); Query OK, 1 row affected (0.01 sec)mysql> insert into student values(3,'张三','男'); Query OK, 1 row affected (0.00 sec) mysql> select * from student; +----+--------+--------+ | id | name | gender | +----+--------+--------+ | 1 | 张三 | 男 | | 2 | 凤凰 | 女 | | 3 | 张三 | 男 | +----+--------+--------+ 3 rows in set (0.00 sec)
3、从服务器查看同步并写入东西(从服务器不容许写入东西)
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.00 sec)mysql> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -ADatabase changed mysql> select * from student; +----+--------+--------+ | id | name | gender | +----+--------+--------+ | 1 | 张三 | 男 | | 2 | 凤凰 | 女 | | 3 | 张三 | 男 | +----+--------+--------+ 3 rows in set (0.00 sec)mysql> insert into student values(4,'李网','女'); Query OK, 1 row affected (0.00 sec)mysql> select * from student; +----+--------+--------+ | id | name | gender | +----+--------+--------+ | 1 | 张三 | 男 | | 2 | 凤凰 | 女 | | 3 | 张三 | 男 | | 4 | 李网 | 女 | +----+--------+--------+ 4 rows in set (0.00 sec)
4、主服务器插入数据
mysql> insert into student values(6,'章节','男'); Query OK, 1 row affected (0.01 sec)mysql> select * from student; +----+--------+--------+ | id | name | gender | +----+--------+--------+ | 1 | 张三 | 男 | | 2 | 凤凰 | 女 | | 3 | 张三 | 男 | | 6 | 章节 | 男 | +----+--------+--------+ 4 rows in set (0.00 sec)
5、从服务器查看同步
mysql> select * from student; +----+--------+--------+ | id | name | gender | +----+--------+--------+ | 1 | 张三 | 男 | | 2 | 凤凰 | 女 | | 3 | 张三 | 男 | | 4 | 李网 | 女 | | 6 | 章节 | 男 | +----+--------+--------+ 5 rows in set (0.00 sec)
二、SQL语句
1、新增
1. insert into库名称.表名
(id,username,password)values(1,"abc","123")
2. insert into 表名称 values(1, "name","word")
3. insert into 表名称 select* from 其他表
4. insert into 表 values (),()
2、删除
1. delete from 表名
2. deletefrom表名称 where id=3
3. delete from 表名称 where age>8
4. delete from 表 where name on ("a","b","c")
3、修改
1. update mysql.user set host='%' where name='root'
2. update user set password='abc' where username="zhangsan"
4、查询
1. 单表查询
1.1 select 字段名列表 from表名称,索引
1.2
5、远程连接数据库的
username
password
url (mysql IP或者域名 数据库名称 端口号 )
mysql> create user 'li'@'%' identified by '123';
Query OK, 0 rows affected (0.02 sec)mysql> grant all on *.* to 'li'@'%';
Query OK, 0 rows affected (0.01 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)mysql> select count(*) from student;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
1 row in set (0.06 sec)mysql> select count(1) from student;
+----------+
| count(1) |
+----------+
| 4 |
+----------+
1 row in set (0.06 sec)mysql> select a.*,b.* from student as a,student as b;
+----+--------+--------+----+--------+--------+
| id | name | gender | id | name | gender |
+----+--------+--------+----+--------+--------+
| 6 | 章节 | 男 | 1 | 张三 | 男 |
| 3 | 张三 | 男 | 1 | 张三 | 男 |
| 2 | 凤凰 | 女 | 1 | 张三 | 男 |
| 1 | 张三 | 男 | 1 | 张三 | 男 |
| 6 | 章节 | 男 | 2 | 凤凰 | 女 |
| 3 | 张三 | 男 | 2 | 凤凰 | 女 |
| 2 | 凤凰 | 女 | 2 | 凤凰 | 女 |
| 1 | 张三 | 男 | 2 | 凤凰 | 女 |
| 6 | 章节 | 男 | 3 | 张三 | 男 |
| 3 | 张三 | 男 | 3 | 张三 | 男 |
| 2 | 凤凰 | 女 | 3 | 张三 | 男 |
| 1 | 张三 | 男 | 3 | 张三 | 男 |
| 6 | 章节 | 男 | 6 | 章节 | 男 |
| 3 | 张三 | 男 | 6 | 章节 | 男 |
| 2 | 凤凰 | 女 | 6 | 章节 | 男 |
| 1 | 张三 | 男 | 6 | 章节 | 男 |
+----+--------+--------+----+--------+--------+#别名
mysql> select id as 编号,name,gender from student;
+--------+--------+--------+
| 编号 | name | gender |
+--------+--------+--------+
| 1 | 张三 | 男 |
| 2 | 凤凰 | 女 |
| 3 | 张三 | 男 |
| 6 | 章节 | 男 |
+--------+--------+--------+
6、mysql函数
排序:max min
汇总:count sum avg
数制:二进制 八进制 十进制 十六进制
只有select子句和having子句,order by 子句中能使用聚合函数1,where子句红中不能使用聚合函数,当使用聚合函数查询以后,不能使用where条件,如果添加条件 ,就使用having
mysql> select * from student order by gender desc; +----+--------+--------+ | id | name | gender | +----+--------+--------+ | 1 | 张三 | 男 | | 3 | 张三 | 男 | | 6 | 章节 | 男 | | 2 | 凤凰 | 女 | +----+--------+--------+ 4 rows in set (0.00 sec)mysql> select * from student order by gender asc; +----+--------+--------+ | id | name | gender | +----+--------+--------+ | 2 | 凤凰 | 女 | | 1 | 张三 | 男 | | 3 | 张三 | 男 | | 6 | 章节 | 男 | +----+--------+--------+ 4 rows in set (0.00 sec)mysql> select gender as 性别,count(gender) as 人数 from student grroup by gender; +--------+--------+ | 性别 | 人数 | +--------+--------+ | 男 | 3 | | 女 | 1 | +--------+--------+ 2 rows in set (0.00 sec)mysql> create table product(-> id int primary key auto_increment,-> name varchar(45) not null,-> price float not null,-> qty int not null); Query OK, 0 rows affected (0.01 sec)mysql> desc product; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | name | varchar(45) | NO | | NULL | | | price | float | NO | | NULL | | | qty | int | NO | | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.01 sec)mysql> insert into product (name,price,qty) values("香蕉",8.5,200); Query OK, 1 row affected (0.00 sec)mysql> insert into product (name,price,qty) values("苹果",12.5,40)0); Query OK, 1 row affected (0.00 sec)mysql> insert into product (name,price,qty) values("菠萝",12.4,700); Query OK, 1 row affected (0.01 sec)mysql> insert into product (name,price,qty) values("哈密瓜",18.3,7400); Query OK, 1 row affected (0.00 sec)mysql> select * from product order by price; +----+-----------+-------+-----+ | id | name | price | qty | +----+-----------+-------+-----+ | 1 | 香蕉 | 8.5 | 200 | | 3 | 菠萝 | 12.4 | 70 | | 2 | 苹果 | 12.5 | 400 | | 4 | 哈密瓜 | 18.3 | 400 | +----+-----------+-------+-----+mysql> select * from (select * from product order by qty) as a ordder by a.price; +----+-----------+-------+-----+ | id | name | price | qty | +----+-----------+-------+-----+ | 1 | 香蕉 | 8.5 | 200 | | 3 | 菠萝 | 12.4 | 70 | | 2 | 苹果 | 12.5 | 400 | | 4 | 哈密瓜 | 18.3 | 400 | +----+-----------+-------+-----+ 4 rows in set (0.00 sec)mysql> select max(price) from product; +------------+ | max(price) | +------------+ | 18.3 | +------------+ 1 row in set (0.00 sec)mysql> select min(price) from product; +------------+ | min(price) | +------------+ | 8.5 | +------------+ 1 row in set (0.00 sec)mysql> select sum(price) from product; +-------------------+ | sum(price) | +-------------------+ | 51.69999885559082 | +-------------------+ 1 row in set (0.01 sec)
相关文章:

2024年8月7日(mysql主从 )
回顾 主服务器 [rootmaster_mysql ~]# yum -y install rsync [rootmaster_mysql ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar [rootmaster_mysql ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz [rootmaster_mysql ~]# cp -r mysql-8.0.33-linux-glibc2.…...

接口/自动化测试 面试集合
1. apache和nginx的区别? Nginx相对Apache的优点: 轻量级,同样起web服务,比apache占用更少的内存及资源; 抗并发,nginx处理请求是异步非阻塞的,支持更多的并发连接,而apache则是阻塞型的,在高 并发下ngi…...

菜鸡勇闯第136场双周赛
菜鸡鼓足了勇气报名了力扣双周赛(后来复盘才知道双周赛更难一点,我真是头铁。。) 没想到还拿了个竞赛名次哈哈哈哈哈还在前50%,小力它真的,我哭死 为什么我本科被高数老师忽悠,去打了两年数模o(≧口≦)o 每…...

趋动科技陈飞:从小模型到大模型,AI时代下的数据中心建设
自AI大模型横空出世,不断推动着AI从学术界到产业界向大众破圈,新的时代正在来临。11月15-16日,由CDCC主办的“2023第11届数据中心标准大会”在北京国家会议中心盛大开幕。 本届大会的主题围绕“AI时代 重塑未来”,聚焦数据中心领…...

yolo v8 + flask部署到云服务器,以及问题记录
环境安装 1、运行项目报错:no python application found, check your startup logs for errors 在云服务器pytorch版本安装错了,安装了GPU版本,需要安装CPU版本 # CPU only 使用下面这段代码避免出现第二个错误 pip install torch2.3.1 to…...

【科研必备插件】easyscholar如何使文章显示期刊影响因子与分区等级
简要介绍 EasyScholar 是一个微软 Edge 浏览器的扩展程序,可以显示会议、期刊等级。可支持在各大论文搜索网站,显示各种期刊、会议等级排名 要想你的知网页面如下图所示,快来获取安装,快速科研有方法 插件安装教程 ①打开浏览器…...

UE5 UrlEncode转换
调用接口时用到了 UFUNCTION(BlueprintPure, Category "FuncLib", meta (Keywords "URL1"))static FString StringToURLEncode(const FString& str1);FString UBasicFuncLib::StringToURLEncode(const FString& str1){return FGenericPlatformH…...

【QML】Qt.rgba()的正确使用方法
1. 问题 设置颜色 color: Qt.rgba(65,105,225,255) ,应该是蓝色,却显示白色。 2. 正确方法 //正确代码 color: Qt.rgba(65/255, 105/255, 225/255, 255/255)...

centos7.9 docker安装
1、不要通过yum直接安装 具体原因: CentOS 6 因内核太旧,即使支持安装 docker,但会有各种问题,不建议安装CentOS 7 的 extras 源虽然可以安装 docker,但包比较旧,建议从官方源或镜像源站点下载安装 docke…...

spring操作数据库
xml版 程序结构 配置文件 dbUtil-阿帕奇提供操作数据库的插件 核心类:QueryRunner .query() 查询 .update() 增删改 <dependencies><!-- spring --><dependency><groupId>org.springframework</groupId><artifactId>spri…...

Apache Flink中TaskManager,SubTask,TaskSlot,并行度之间的关系
Apache Flink 中Application 与 Job 一个完整的Flink Application 一般组成如下: Source 数据来源Transformation 数据转换处理等Sink 数据传输 Flink 中一个或者多个Operator(算子)组合对数据进行转换形成一个 Transformation,一…...

马斯克xAI新计划:人工智能模型Grok 2测试版即将发布
特斯拉CEO马斯克在X平台上表示,人工智能模型Grok 2测试版即将发布。Grok,作为xAI公司的明星大语言模型,其首代产品Grok 1已凭借神经演化计算与深度学习技术的深度融合,展现了超乎想象的学习速度与智能深度,赢得了业界的…...

【机器人学】6-4.六自由度机器人运动学参数辨识-机器人精度验证【附MATLAB代码】
前言 前两个章节以及完成了机器人参数辨识。 【机器人学】6-1.六自由度机器人运动学参数辨识-辨识数学模型的建立 【机器人学】6-2.六自由度机器人运动学参数辨识-优化方法求解辨识参数 这里我们认为激光测量仪测量到的数据为机器人实际到达的位置,而机器人理论到…...

分销商城小程序系统渠道拓展
线上卖货渠道很多,想要不断提高营收和新客获取,除了自己和工具本身努力外,还需要其他人的帮助来提高商城店铺的整体销量。 搭建saas商城系统网站/小程序,后台上货,设置支付、配送、营销、精美模板商城装修等内容&…...

WPF篇(14)-ProgressBar进度条+Calendar日历控件+DatePicker日期控件
ProgressBar进度条 ProgressBar进度条通常在我们执行某个任务需要花费大量时间时使用,这时可以采用进度条显示任务或线程的执行进度,以便给用户良好的使用体验。 ProgressBar类定义 public class ProgressBar : RangeBase {public static readonly De…...

链表高频题目和必备技巧
链表高频题目和必备技巧 1. 链表类题目注意点 1,如果笔试中空间要求不严格,直接使用容器来解决链表问题 2,如果笔试中空间要求严格、或者在面试中面试官强调空间的优化,需要使用额外空间复杂度**O(1)**的方法 3,最…...

Vue3详细介绍,正则采集器所用前端框架
Vue3 引入了一个全新的响应式系统,它是基于ES6的Proxy特性构建的。这个系统使得 Vue 能够更加高效地追踪数据的变化,并在数据发生变化时自动更新DOM。响应式系统的核心是"可观察",当数据变化时,视图会响应这些变化并重新…...

数据集--COCO2017(快速下载)
1、数据集介绍 数据集官网:https://cocodataset.org/#home COCO(Common Objects in Context)数据集是计算机视觉领域中最广泛使用的数据集之一,主要用于目标检测、分割和图像标注任务。COCO 数据集由 Microsoft 发布,…...

【管理咨询宝藏159】顶级咨询公司人力三支柱建设方案思路
阅读完整版报告内容,请搜索VV号“管理咨询宝藏”。 【管理咨询宝藏159】顶级咨询公司人力三支柱建设方案思路 【格式】PDF版本 【关键词】人力咨询、三支柱、人力体系 【核心观点】 - 集团总部制定全集团共享中心总体规划路径,组织并负责实施与推广。各…...

跨时钟域总结
跨时钟域总结 秋招学习跨时钟域 总结一下吧 异步电路 设计中有两个频率不同的时钟(也可能多个),而有数据在两组时钟之间传输 单bit跨时钟域 慢时钟域数据-> 快时钟域 方法 : 使用两个锁存器 (打两拍) 数据跨时钟域同步过程中,脉冲宽度会改变,不影响同步结…...

富婆和富公子都在看的负载均衡和Haproxy大全
一.负载均衡 1.1:什么是负载均衡 负载均衡: Load Balance ,简称 LB ,是一种服务或基于硬件设备等实现的高可用反向代理技术,负载均 衡将特定的业务(web 服务、网络流量等 ) 分担给指定的一个或多个后端特定的服务器或…...

VScode找python环境 (conda)
第一步 CtrlshiftP 第二步 框框里输入:Python:Select Interpreter...

C# Winform序列化和反序列化
在NET Framework 4.7.2中不能用Newtonsoft.Json进行序列化和反序列化,为解决此问题,采用System.Text.Json进行序列化,注意要添加System.Memory的引用。 1、创建测试类 using System; using System.Collections.Generic; using System.Linq; …...

crc原理概述
CRC(循环冗余校验)是一种错误检测技术,用于确保数据在传输或存储过程中没有发生变化。它通过将数据视为一个多项式,利用二进制除法得到一个校验码(CRC值)。接收方使用相同的算法验证数据和CRC值是否匹配&am…...

C++要求或禁止在堆中产生对象
有时你想这样管理某些对象,要让某种类型的对象能够自我销毁,也就是能够“delete this”。很明显这种管理方式需要此类型对象被分配在堆中。而其它一些时候你想获得一种保障:“不在堆中分配对象,从而保证某种类型的类不会发生内存泄…...

为什么阿里开发手册推荐用静态工厂方法代替构造器?
🍅 作者简介:哪吒,CSDN2021博客之星亚军🏆、新星计划导师✌、博客专家💪 🍅 哪吒多年工作总结:Java学习路线总结,搬砖工逆袭Java架构师 🍅 技术交流:定期更新…...

前端写法建议【让项目更加易于维护】
背景 标题前提条件: 没有字典接口、或其他原因,需要前端手动维护的情况 示例环境:vue2,其他项目同理 示例 如果项目有某种类别,前端和后端约定好了,某些情况下,需要前端写死时。 比如有字段…...

EasyExcel 自定义转换器、自定义导出字典映射替换、满足条件内容增加样式,完整代码+详细注释说明
虽然最之前是在其他地方看到的,但最终因缘巧合下找到了原文,还是尊重一下原作者。 参考引用了这位佬的博客,确实方便使用。 https://blog.csdn.net/qq_45914616/article/details/137200688?spm1001.2014.3001.5502 这是一个基于Easyexcel通过…...

C语言学习笔记 Day10(指针--中)
Day10 内容梳理: 目录 Chapter 7 指针 7.4 指针 & 数组 (1)指针操作数组元素 (2)指针加减运算 1)加法 2)减法 (3)指针数组 7.5 多级指针 Chapter 7 指针 …...

网页显示打印 pdf
文件服务使用 minio,使用 nginx 反向代理。 将文件存放在 minio 上,如果是公开的文件,则统一放到一个桶,设置为公开只读。 如果是私有文件,则使用临时链接,给有权限的用户查看和打印。 要实现在 html 页…...