2024年8月7日(mysql主从 )

news2024/9/30 15:22:00
回顾
主服务器

[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 -p

mysql> 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 -A

Database 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)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1991467.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

QT找不到编辑框

问题展示: 解决办法:ALT0 然后我的变成了这种: 解决办法:文件系统改变成项目:

DNTR——F

文章目录 AbstractIntroductionContribution Related WorkAdvancements in Feature Pyramid Networks (FPNs)Coarse-to-Fine Image Partitioning in Drone Imagery DetectionDevelopments in Loss Function Approaches for Tiny Object DetectionR-CNN for Small Object Detect…

大炼模型进入尾声,“失眠”的欧洲和日本能否扳回一局?

大数据产业创新服务媒体 ——聚焦数据 改变商业 2022年末,ChatGPT-3.5的惊艳亮相,瞬间引爆了全球范围内的生成式AI(GenAI)热潮。 这场现代版的"淘金热"迅速在科技领域蔓延,尤其是在全球两大科技强国——中国…

简单分享下python打包手机app的apk

Python 把python程序打包成apk的完整步骤 1. 引言 在移动应用市场蓬勃发展的今天,开发人员常常需要将自己的Python程序打包成APK文件,以便在Android设备上运行。本文将详细介绍将Python程序打包成APK的完整步骤。 2. 准备工作 在开始打包前&#xff0c…

全网最详解LVS(Linux virual server)

目录 一、LVS(Linux virual server)是什么? 二、集群和分布式简介 2.1、集群Cluster 2.2、分布式 2.3、集群和分布式 三、LVS运行原理 3.1、LVS基本概念 3.2、LVS集群的类型 3.2.1 nat模式 3.2.2 DR模式 3.2.3、LVS工作模式总结 …

RSYSLOG收到华为防火墙日志差8小时的解决方法

RSYSLOG收到华为防火墙日志差8小时 这个问题其实不关Rsyslog配置的事,只要修改华为墙的配置就好 处理方法: info-center loghost 172.18.6.91 language Chinese local-time 在华为web界面添加ip是不会添加local-time这个参数的, 需要在命令…

sqli-labs第二关详解

首先让id1,正常显示,接着尝试and 11和and 12 and 11正常,and 12不正常 所以可以判断是数字型注入,使用order by 判断列数,发现有三个字段 使用union语句,找出能显示信息的地方 接下来就是找出数据库名称和版…

Leetcode75-7 除自身以外数组的乘积

没做出来 本来的思路是遍历一遍得到所有乘积和然后除就行 但是题目不能用除法 答案的思路 for(int i0;i<n;i) //最终每个元素其左右乘积进行相乘得出结果{res[i]*left; //乘以其左边的乘积left*nums[i];res[n-1-i]*right; //乘以其右边的乘积right*nums[n-1-i]…

搭建 Web 群集Haproxy

案例概述 Haproxy 是目前比较流行的一种群集调度工具&#xff0c;同类群集调度工具有很多&#xff0c;如 LVS 和Nginx。相比较而言&#xff0c;LVS 性能最好&#xff0c;但是搭建相对复杂;Nginx 的upstream模块支持群集功能&#xff0c;但是对群集节点健康检查功能不强&#xf…

海量数据处理商用短链接生成器平台 - 10

第二十一章 短链服务冗余双写-链路测试和异常消息处理实战 第1集 冗余双写MQ架构-消费者配置自动创建队列和集群测试 简介&#xff1a; 冗余双写MQ架构-MQ消费者配置自动创建队列 controller-service层开发配置文件配置MQ ##----------rabbit配置-------------- spring.rab…

古彝文——唯一存活的世界六大古文字

关注我们 - 数字罗塞塔计划 - 早在五千年前&#xff0c;彝族的先祖就发明了十月太阳历&#xff0c;成为中华文明的重要创造者之一&#xff1b;同时&#xff0c;彝族的先祖也创制了古彝文&#xff0c;开创了独具特色的彝族文化。古彝文也被称为古夷文、传统彝文&#xff0c;是相…

Unity补完计划 之 动态控制TileMap

本文仅作笔记学习和分享&#xff0c;不用做任何商业用途 本文包括但不限于unity官方手册&#xff0c;unity唐老狮等教程知识&#xff0c;如有不足还请斧正 1.TileMap &TileBase Unity - Scripting API: Tilemap &#xff0c;看手册内容太多了故介绍几个常用的公共方法 首…

一个IT能拖垮整个公司?你若不信,看完此文再来评论

看到文章的标题&#xff0c;你也许会心生疑惑&#xff1a;一个IT真的能拖垮整个公司吗&#xff1f;也可能会觉得我根本就是在哗众取宠、博人眼球。 而我要说&#xff0c;在特定的条件下&#xff0c;这真的不夸张&#xff0c;你若不信&#xff0c;且听我娓娓道来&#xff0c;看完…

java maven项目如何分析循环依赖并使用maven helper插件排查排除依赖

文章目录 java maven项目如何分析循环依赖并使用maven helper插件排查排除依赖分析项目依赖插件Maven Helper找到并排除循环的依赖 java maven项目如何分析循环依赖并使用maven helper插件排查排除依赖 分析项目依赖 选择检测整个项目 查看红色的模块&#xff0c;都是有循环依…

问卷星无法选择复制

删除<link rel"stylesheet" href"//image.wjx.cn/joinnew/css/jqmobo.css?v5187">

【信创】Linux如何增加与删除交换分区 _ 统信 _ 麒麟 _ 中科方德

原文链接&#xff1a;信创】Linux如何增加与删除交换分区 | 统信 | 麒麟 | 中科方德 Hello&#xff0c;大家好啊&#xff01;今天给大家带来一篇关于在信创终端操作系统上如何增加与删除交换分区的文章。交换分区&#xff08;Swap&#xff09;在Linux系统中扮演着重要角色&…

思特威正式发布子品牌飞凌微,首发产品定位智驾视觉处理

2024年8月8日&#xff0c;中国上海 — 思特威&#xff08;上海&#xff09;电子科技股份有限公司&#xff08;股票简称&#xff1a;思特威&#xff0c;股票代码&#xff1a;688213&#xff09;正式发布全资子公司品牌——飞凌微电子&#xff08;Flyingchip™&#xff0c;以下简…

工具学习_CAPA a ATTCK

随着网络威胁的日益复杂和多样化&#xff0c;恶意软件的分析与识别成为安全领域的重要挑战。在这个背景下&#xff0c;静态分析技术成为了对抗恶意软件的有效手段。CAPA 作为一款强大的静态分析工具&#xff0c;能够帮助分析师快速提取恶意软件的静态特征&#xff0c;而 ATT&am…

无线领夹麦克风和有线哪个好?求知无线领夹麦哪个牌子好?

如今&#xff0c;自媒体行业蓬勃发展&#xff0c;音频设备作为创作过程中不可或缺的一部分&#xff0c;越来越受到重视。无线领夹麦克风因其出色的性能和便携性&#xff0c;逐渐成为市场上的热门产品。购买无线领夹麦克风已经成为许多创作者的共识。然而&#xff0c;面对市面上…

自动驾驶系列—图像到IPM:深入解析IMP投影变换技术

&#x1f31f;&#x1f31f; 欢迎来到我的技术小筑&#xff0c;一个专为技术探索者打造的交流空间。在这里&#xff0c;我们不仅分享代码的智慧&#xff0c;还探讨技术的深度与广度。无论您是资深开发者还是技术新手&#xff0c;这里都有一片属于您的天空。让我们在知识的海洋中…