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跨时钟域 慢时钟域数据-> 快时钟域 方法 : 使用两个锁存器 (打两拍) 数据跨时钟域同步过程中,脉冲宽度会改变,不影响同步结…...
想做市场品牌策划?这3大秘诀让你的品牌脱颖而出!
行业痛点分析当前品牌策划领域面临诸多技术挑战。许多企业有产品无品牌,产品品质过硬、技术领先,但缺乏清晰的品牌定位与价值表达,陷入 “酒香也怕巷子深” 的困境,只能靠低价竞争。数据表明,约 60%的企业因品牌定位不…...
OpenClaw+Phi-3-vision-128k-instruct内容创作流:从图文素材到Markdown自动排版
OpenClawPhi-3-vision-128k-instruct内容创作流:从图文素材到Markdown自动排版 1. 为什么需要自动化内容创作流 作为一个长期与图文内容打交道的创作者,我每天都要处理大量零散的素材——截图、手写笔记、PPT片段、网页摘录。最痛苦的不是创作本身&…...
STM32F407实战指南:基于74HC595的4位数码管驱动与动态扫描详解
1. 从零认识数码管:你的第一个嵌入式显示方案 第一次接触数码管时,我完全被它简单粗暴的显示方式吸引了。这种由7个LED灯组成的显示器件,通过不同段的组合就能展示0-9的数字,成本不到2块钱却能在各种家电上看到它的身影。我们这次…...
我需要开发出一个检测手机移动的算法来
1 用GPS2 运动传感器3 其他传感器就是真正能100%不会误报,而且精度达到:只要移动距离超过1米就要报警的那种。挂在树上面即使手机不停在转动也不会误报后来发现:只需要一个位移检测算法就可以了。...
API调用成本优化实战:Token中转站的原理与选型建议
前言作为AI应用开发者,过去几个月我一直被一个问题困扰——API账单太贵了。特别是 Claude 3.5 Sonnet、GPT-4o 这类顶级模型,性能确实强,但价格也着实肉疼。随便跑几个测试,几十美元就没了;如果上线正式应用࿰…...
《高效能人士的七个习惯》:从内圣到外王的完整方法论
这本书在全世界卖了千万册,斯蒂芬柯维用七个习惯构建了一套从自我管理到影响他人的完整体系。一、前言:比七个习惯更重要的两件事 很多人读这本书只关注七个习惯本身,却忽略了前言中两个至关重要的前提: 1. 积极乐观是一切的起点 …...
网络信息安全技术术语对照表
类别术语中文术语英文术语说明基础技术类加密encryption将明文数据通过特定算法和密钥转换为密文数据的过程,目的是确保数据在存储、传输过程中不被未授权方获取和理解。基础技术类解密decryption将加密后的密文数据,通过对应的算法和密钥还原为原始明文…...
【工业级Python内存治理白皮书】:覆盖CPython 3.8–3.12的7层内存管控架构,含可落地的监控-预警-自愈SOP手册
第一章:Python 智能体内存管理策略 性能调优指南Python 的内存管理并非完全由开发者显式控制,而是依托于引用计数、循环垃圾回收器(GC)与内存池(pymalloc)三层协同机制。理解其内在逻辑是实现高性能智能体&…...
为什么又来学习C语言?
我是一名来自民办二本院校的大三学生,早在大一上时学校就安排了C语言的课程,但是当时我很是浮躁,心不在学习,甚至想着回去复读,所以并没有吸纳多少C语言的知识。现在大三,有了考研想法,想重拾C语…...
三自由度机械臂自适应神经网络控制(径向基函数)Matlab代码
✅作者简介:热爱科研的Matlab仿真开发者,擅长毕业设计辅导、数学建模、数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。👇 关注我领取海量matlab电子书和数学建模资料🍊个人信条:格物致知,完整Matl…...
