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/ ![](https://i-blog.csdnimg.cn/direct/6e84e6652ca1481fb07b436cee134672.png) \[root@master_mysql mysql\]# rm -rf /etc/my.cnf \[root@master_mysql mysql\]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ ![](https://i-blog.csdnimg.cn/direct/8db79f6c2c5d4ad990289a8895606291.png) \[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 ```bash [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/ ![](https://i-blog.csdnimg.cn/direct/b55b122f4b3b49d5b7770701907c2b57.png) \[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 [email protected]:/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 ```sql 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 ```sql 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、主服务器创建表并向表里添加数据
sql 复制代码
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、从服务器查看同步并写入东西(从服务器不容许写入东西)
sql 复制代码
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、主服务器插入数据
sql 复制代码
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、从服务器查看同步
sql 复制代码
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")

  1. insert into 表名称 values(1, "name","word")

  2. insert into 表名称 select* from 其他表

  3. 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或者域名 数据库名称 端口号 )

sql 复制代码
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

sql 复制代码
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)
相关推荐
Ultipa3 分钟前
云计算与大数据进阶 | 26、解锁云架构核心:深度解析可扩展数据库的5大策略与挑战(上)
大数据·数据库·云计算
中国lanwp27 分钟前
在Maven中替换文件内容的插件和方法
服务器·数据库·maven
菜菜小蒙27 分钟前
【MySQL】内置函数
数据库·mysql
2401_8368365927 分钟前
mysql故障排查与环境优化
数据库·mysql·adb
惊起白鸽45030 分钟前
mysql故障排查与生产环境优化
数据库·mysql
帅的被人砍xxx43 分钟前
mariadb 升级 (通过yum)
数据库·mariadb
通义灵码1 小时前
MySQL 开发的智能助手:通义灵码在 IntelliJ IDEA 中的应用
数据库·mysql·阿里云·intellij-idea·通义灵码
charlie1145141911 小时前
Linux内核深入学习(4)——内核常见的数据结构2——红黑树
linux·数据结构·学习·红黑树
XMYX-01 小时前
在 CentOS 7.9 上部署 node_exporter 并接入 Prometheus + Grafana 实现主机监控
centos·grafana·prometheus
chen.@-@1 小时前
Linux 常用命令
linux