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

  • 29
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值