[root@slave-mysql ~]# yum -y install rsync
[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 ~]# ls
[root@master-mysql ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz
[root@master-mysql ~]# ls
[root@master-mysql ~]# vim mysql-8.0.33-linux-glibc2.12-x86_64/support-files/mysql.server
[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]# ls
[root@master-mysql mysql]# mkdir mysql-files
[root@master-mysql mysql]# ll
[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]# ll[root@master-mysql mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql
[root@master-mysql mysql]# ls[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
[root@master-mysql mysql]# service mysql8 start[root@master-mysql mysql]# ./bin/mysql -P 3306 -p
[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/err.log
log-bin=/usr/local/mysql/data/binlog
charactor_set_server=utf8mb4server_id=10
[root@master-mysql mysql]# service mysql8 restart
[root@master-mysql mysql]# ls data
[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 ~]# ll /usr/local/mysql/
[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 mysql]# rm -rf /usr/local/mysql/data/auto.cnf
[root@master-mysql mysql]# ls /usr/local/mysql/data/
[root@master-mysql mysql]# rsync -av /usr/local/mysql/data root@192.168.8.128:/usr/local/mysql/
[root@slave-mysql ~]# vim /usr/local/mysql/data/my.cnf
[root@slave-mysql ~]# service mysql8 start[root@slave-mysql ~]# /usr/local/mysql/bin/mysql -pN6s9znjbL*/s
[root@master-mysql mysql]# vim /etc/profile
[root@master-mysql mysql]# source /etc/profile[root@master-mysql mysql]# mysql -p
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.01 sec) mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000004 | 1198 | | | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
[root@slave-mysql ~]# vim /etc/profile
[root@slave-mysql ~]# source /etc/profile[root@slave-mysql ~]# mysql -h192.168.8.139 -uslave -pslave_123 --get-server-public-key
[root@slave-mysql ~]# mysql -p123456 -P3306
mysql> stop slave; Query OK, 0 rows affected, 2 warnings (0.00 sec) mysql> reset slave; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> change master to -> master_host='192.168.8.139', -> master_user='slave', -> master_password='slave_123', -> master_log_file='binlog.000004', -> master_log_pos=3296; Query OK, 0 rows affected, 8 warnings (0.02 sec) mysql> start slave; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> show slave status\G;
- mysql> unlock tables;
- Query OK, 0 rows affected (0.01 sec)
- mysql> create database if not exists test charset utf8mb4;
- Query OK, 1 row affected (0.01 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.02 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.00 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 | 王五 | 男 |
- +----+--------+--------+
- 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)
-
- 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)
- mysql> select * from student;
- +----+--------+--------+
- | id | name | gender |
- +----+--------+--------+
- | 1 | 张三 | 男 |
- | 2 | 李四 | 男 |
- | 3 | 王五 | 男 |
- | 4 | 李网 | 女 |
- | 6 | 章节 | 男 |
- +----+--------+--------+
- 5 rows in set (0.00 sec)
- mysql> select * from student;
- +----+--------+--------+
- | id | name | gender |
- +----+--------+--------+
- | 1 | 张三 | 男 |
- | 2 | 李四 | 男 |
- | 3 | 王五 | 男 |
- | 4 | 李网 | 女 |
- | 6 | 章节 | 男 |
- +----+--------+--------+
- 5 rows in set (0.00 sec)
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 (),()
1. delete from 表名
2. deletefrom表名称 where id=3
3. delete from 表名称 where age>8
4. delete from 表 where name on ("a","b","c")
1. update mysql.user set host='%' where name='root'
2. update user set password='abc' where username="zhangsan"
1.1 select 字段名列表 from表名称,索引
- mysql> select count(*) from student;
- +----------+
- | count(*) |
- +----------+
- | 3 |
- +----------+
- 1 row in set (0.05 sec)
-
- mysql> select count(1) from student;
- +----------+
- | count(1) |
- +----------+
- | 3 |
- +----------+
- 1 row in set (0.06 sec)
-
- mysql> select count(id) from student;
- +-----------+
- | count(id) |
- +-----------+
- | 3 |
- +-----------+
- 1 row in set (0.06 sec)
-
- mysql> select a.*,b.* from student as a,student as b;
- +----+--------+--------+----+--------+--------+
- | id | name | gender | id | name | gender |
- +----+--------+--------+----+--------+--------+
- | 3 | 王五 | 男 | 1 | 张三 | 男 |
- | 2 | 李四 | 男 | 1 | 张三 | 男 |
- | 1 | 张三 | 男 | 1 | 张三 | 男 |
- | 3 | 王五 | 男 | 2 | 李四 | 男 |
- | 2 | 李四 | 男 | 2 | 李四 | 男 |
- | 1 | 张三 | 男 | 2 | 李四 | 男 |
- | 3 | 王五 | 男 | 3 | 王五 | 男 |
- | 2 | 李四 | 男 | 3 | 王五 | 男 |
- | 1 | 张三 | 男 | 3 | 王五 | 男 |
- +----+--------+--------+----+--------+--------+
- 9 rows in set (0.00 sec)
- mysql> create user 'abc'@'%' identified by '123';
- Query OK, 0 rows affected (0.01 sec)
-
- mysql> grant all on *.* to 'abc'@'%';
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> flush privileges;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select id,name,gender from student;
- +----+--------+--------+
- | id | name | gender |
- +----+--------+--------+
- | 1 | 张三 | 男 |
- | 2 | 李四 | 男 |
- | 3 | 王五 | 男 |
- +----+--------+--------+
- 3 rows in set (0.00 sec)
-
- mysql> select id as 编号,name,gender from student;
- +--------+--------+--------+
- | 编号 | name | gender |
- +--------+--------+--------+
- | 1 | 张三 | 男 |
- | 2 | 李四 | 男 |
- | 3 | 王五 | 男 |
- +--------+--------+--------+
- 3 rows in set (0.00 sec)
数据分析的基础
- mysql> select max(price) from product group by name;
- +------------+
- | max(price) |
- +------------+
- | 8.5 |
- | 12.5 |
- | 12.4 |
- | 18.3 |
- +------------+
- 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.00 sec)
-
- mysql> select avg(price) from product;
- +--------------------+
- | avg(price) |
- +--------------------+
- | 12.924999713897705 |
- +--------------------+
- 1 row in set (0.00 sec)
-
- mysql> select *,price*qty as tt from product;
- +----+-----------+-------+-----+-------------------+
- | id | name | price | qty | tt |
- +----+-----------+-------+-----+-------------------+
- | 1 | 香蕉 | 8.5 | 200 | 1700 |
- | 2 | 苹果 | 12.5 | 400 | 5000 |
- | 3 | 菠萝 | 12.4 | 70 | 867.9999732971191 |
- | 4 | 哈密瓜 | 18.3 | 400 | 7319.999694824219 |
- +----+-----------+-------+-----+-------------------+
- 4 rows in set (0.00 sec)
-
- mysql> select sum(tt) from (select *,price*qty as tt from productt) as a;
- +--------------------+
- | sum(tt) |
- +--------------------+
- | 14887.999668121338 |
- +--------------------+
- 1 row in set (0.00 sec)
1. 二进制
2. 八进制
3. 十进制
4. 十六进制
5. AA 27
- mysql> select * from student order by gender desc;
- +----+--------+--------+
- | id | name | gender |
- +----+--------+--------+
- | 1 | 张三 | 男 |
- | 2 | 李四 | 男 |
- | 3 | 王五 | 男 |
- | 4 | 花花 | 女 |
- | 5 | 花 | 女 |
- +----+--------+--------+
- 5 rows in set (0.00 sec)
-
- mysql> select * from student order by gender asc;
- +----+--------+--------+
- | id | name | gender |
- +----+--------+--------+
- | 4 | 花花 | 女 |
- | 5 | 花 | 女 |
- | 1 | 张三 | 男 |
- | 2 | 李四 | 男 |
- | 3 | 王五 | 男 |
- +----+--------+--------+
- 5 rows in set (0.00 sec)
只有select子句和having子句、order by子句中能使用聚合函数,where子句不能使用聚合函
数。当使用聚合函数查询以后,不能使用where条件,如果要添加条件,
- mysql> select gender,count(gender) from student group by gender;
-
- +--------+---------------+
- | gender | count(gender) |
- +--------+---------------+
- | 男 | 3 |
- | 女 | 2 |
- +--------+---------------+
- 2 rows in set (0.01 sec)
-
- mysql> select gender as 性别,count(gender) as 人数 from studentgroup by gender;
- +--------+--------+
- | 性别 | 人数 |
- +--------+--------+
- | 男 | 3 |
- | 女 | 2 |
- +--------+--------+
- 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.01 sec)
-
- mysql> insert into product (name,price,qty) values("苹果",12.5,4000);
- 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.00 sec)
-
- mysql> insert into product (name,price,qty) values("哈密瓜",18.3,,400);
- Query OK, 1 row affected (0.00 sec)
-
- mysql> select * from product;
- +----+-----------+-------+-----+
- | id | name | price | qty |
- +----+-----------+-------+-----+
- | 1 | 香蕉 | 8.5 | 200 |
- | 2 | 苹果 | 12.5 | 400 |
- | 3 | 菠萝 | 12.4 | 70 |
- | 4 | 哈密瓜 | 18.3 | 400 |
- +----+-----------+-------+-----+
- 4 rows in set (0.00 sec)
-
- mysql> select * from product order by qty;
- +----+-----------+-------+-----+
- | id | name | price | qty |
- +----+-----------+-------+-----+
- | 3 | 菠萝 | 12.4 | 70 |
- | 1 | 香蕉 | 8.5 | 200 |
- | 2 | 苹果 | 12.5 | 400 |
- | 4 | 哈密瓜 | 18.3 | 400 |
- +----+-----------+-------+-----+
- 4 rows in set (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 |
- +----+-----------+-------+-----+
- 4 rows in set (0.00 sec)
- mysql> select * from (select * from product order by qty) as a oerder 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 now();
- +---------------------+
- | now() |
- +---------------------+
- | 2024-08-07 15:30:50 |
- +---------------------+
- 1 row in set (0.00 sec)
-
- mysql> select year(now());
- +-------------+
- | year(now()) |
- +-------------+
- | 2024 |
- +-------------+
- 1 row in set (0.00 sec)
-
- mysql> select second(now());
- +---------------+
- | second(now()) |
- +---------------+
- | 13 |
- +---------------+
- 1 row in set (0.00 sec)
-
- mysql> insert into product (name,price,qty) values (now(),7.8,90)
- );
- Query OK, 1 row affected (0.00 sec)
-
- mysql> select * from product;
- +----+---------------------+-------+-----+
- | id | name | price | qty |
- +----+---------------------+-------+-----+
- | 1 | 香蕉 | 8.5 | 200 |
- | 2 | 苹果 | 12.5 | 400 |
- | 3 | 菠萝 | 12.4 | 70 |
- | 4 | 哈密瓜 | 18.3 | 400 |
- | 5 | 2024-08-07 15:33:08 | 7.8 | 90 |
- +----+---------------------+-------+-----+
- 5 rows in set (0.00 sec)