8.7-主从数据库的配置+mysql的增删改查

news2024/9/25 11:17:42

一、mysql环境的配置

1.环境准备

(1)主数据库

#关闭防火墙
[root@master ~]# systemctl stop firewalld

#关闭selinux
[root@master ~]# setenforce 0

#下载lrzsz工具
[root@master ~]# yum -y install lrzsz

#安装rsync
[root@master ~]# yum -y install rsync

#查看是否安装了libaio
[root@master ~]# rpm -qa|grep libaio
libaio-0.3.109-13.el7.x86_64

#查看是否安装了rsync
[root@master ~]# rpm -qa|grep rsync
rsync-3.1.2-12.el7_9.x86_64

(2)从数据库

#关闭防火墙
[root@slave ~]# systemctl stop firewalld

#关闭selinux
[root@slave ~]# setenforce 0

#安装lrzsz
[root@slave ~]# yum -y install lrzsz

#安装rsync
[root@slave ~]# yum -y install rsync

2.解压

(1)主数据库

# 将mysqlmysql-8.0.33-linux-glibc2.12-x86_64.tar包拖到xshell中
[root@master ~]# rz -E
rz waiting to receive.
[root@master ~]# ls
anaconda-ks.cfg  mysql-8.0.33-linux-glibc2.12-x86_64.tar

# 解压
[root@master ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar 
[root@master ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz

# 查看是否解压成功
[root@master ~]# ls
anaconda-ks.cfg
mysql-8.0.33-linux-glibc2.12-x86_64
mysql-8.0.33-linux-glibc2.12-x86_64.tar
mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz
mysql-router-8.0.33-linux-glibc2.12-x86_64.tar.xz
mysql-test-8.0.33-linux-glibc2.12-x86_64.tar.xz

(2)从数据库

[root@slave ~]# ls
anaconda-ks.cfg  mysql-8.0.33-linux-glibc2.12-x86_64.tar
[root@slave ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar 
[root@slave ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz 
[root@slave ~]# ls
anaconda-ks.cfg
mysql-8.0.33-linux-glibc2.12-x86_64
mysql-8.0.33-linux-glibc2.12-x86_64.tar
mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz
mysql-router-8.0.33-linux-glibc2.12-x86_64.tar.xz
mysql-test-8.0.33-linux-glibc2.12-x86_64.tar.xz

3.备份文件

(1)主数据库

[root@master ~]# cp -r mysql-8.0.33-linux-glibc2.12-x86_64 /usr/local/mysql

(2)从数据库

[root@slave ~]# cp -r mysql-8.0.33-linux-glibc2.12-x86_64 /usr/local/mysql

4.创建目录和用户

(1)主数据库

[root@master ~]# cd /usr/local/mysql/
[root@master mysql]# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@master mysql]# mkdir mysql-files
[root@master mysql]# ll
总用量 292
drwxr-xr-x.  2 root root   4096 8月   7 09:21 bin
drwxr-xr-x.  2 root root     38 8月   7 09:21 docs
drwxr-xr-x.  3 root root    282 8月   7 09:21 include
drwxr-xr-x.  6 root root    201 8月   7 09:21 lib
-rw-r--r--.  1 root root 284945 8月   7 09:21 LICENSE
drwxr-xr-x.  4 root root     30 8月   7 09:21 man
drwxr-xr-x.  2 root root      6 8月   7 09:22 mysql-files
-rw-r--r--.  1 root root    666 8月   7 09:21 README
drwxr-xr-x. 28 root root   4096 8月   7 09:21 share
drwxr-xr-x.  2 root root     77 8月   7 09:21 support-files
[root@master mysql]# id mysql
id: mysql: no such user
[root@master mysql]# useradd -r -s /sbin/nologin mysql
[root@master mysql]# id mysql
uid=997(mysql) gid=995(mysql) 组=995(mysql)
[root@master mysql]# echo $?
0

(2)从数据库

[root@slave ~]# mkdir /usr/local/mysql/mysql-files
[root@slave ~]# useradd -r -s /sbin/nologin mysql
[root@slave ~]# id mysql
uid=997(mysql) gid=995(mysql) 组=995(mysql)

5.修改权限

(1)主数据库

[root@master mysql]# chown mysql:mysql ./mysql-files/
[root@master mysql]# ll
总用量 292
drwxr-xr-x.  2 root  root    4096 8月   7 09:21 bin
drwxr-xr-x.  2 root  root      38 8月   7 09:21 docs
drwxr-xr-x.  3 root  root     282 8月   7 09:21 include
drwxr-xr-x.  6 root  root     201 8月   7 09:21 lib
-rw-r--r--.  1 root  root  284945 8月   7 09:21 LICENSE
drwxr-xr-x.  4 root  root      30 8月   7 09:21 man
drwxr-xr-x.  2 mysql mysql      6 8月   7 09:22 mysql-files
-rw-r--r--.  1 root  root     666 8月   7 09:21 README
drwxr-xr-x. 28 root  root    4096 8月   7 09:21 share
drwxr-xr-x.  2 root  root      77 8月   7 09:21 support-files

(2)从数据库

[root@slave ~]# chown mysql:mysql /usr/local/mysql/mysql-files
[root@slave ~]# ll /usr/local/mysql/
总用量 292
drwxr-xr-x.  2 root  root    4096 8月   7 10:07 bin
drwxr-xr-x.  2 root  root      38 8月   7 10:07 docs
drwxr-xr-x.  3 root  root     282 8月   7 10:07 include
drwxr-xr-x.  6 root  root     201 8月   7 10:07 lib
-rw-r--r--.  1 root  root  284945 8月   7 10:07 LICENSE
drwxr-xr-x.  4 root  root      30 8月   7 10:07 man
drwxr-xr-x.  2 mysql mysql      6 8月   7 10:11 mysql-files
-rw-r--r--.  1 root  root     666 8月   7 10:07 README
drwxr-xr-x. 28 root  root    4096 8月   7 10:07 share
drwxr-xr-x.  2 root  root      77 8月   7 10:07 support-files

6.删除配置文件

(1)主数据库

[root@master mysql]# rm -rf /etc/my.cnf

(2)从数据库

[root@slave ~]# rm -rf /etc/my.cnf

7.初始化

(1)主数据库

# 初始化  从数据库不能初始化
[root@master mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql
2024-08-07T01:28:33.413323Z 0 [System][MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.33) initializing of server in progress as process 1701
2024-08-07T01:28:33.420601Z 1 [System][MY-013576] [InnoDB] InnoDB initialization has started.
2024-08-07T01:28:34.159124Z 1 [System][MY-013577] [InnoDB] InnoDB initialization has ended.
2024-08-07T01:28:35.485851Z 6 [Note][MY-010454] [Server] A temporary password is generated for root@localhost: CpjsNb1yO-fd
[root@master mysql]# ls /usr/local/mysql
bin   docs     lib      man          README  support-files
data  include  LICENSE  mysql-files  share
[root@master mysql]# ll
总用量 296
drwxr-xr-x.  2 root  root    4096 8月   7 09:21 bin
drwxr-x---.  7 mysql mysql   4096 8月   7 09:28 data
drwxr-xr-x.  2 root  root      38 8月   7 09:21 docs
drwxr-xr-x.  3 root  root     282 8月   7 09:21 include
drwxr-xr-x.  6 root  root     201 8月   7 09:21 lib
-rw-r--r--.  1 root  root  284945 8月   7 09:21 LICENSE
drwxr-xr-x.  4 root  root      30 8月   7 09:21 man
drwxr-xr-x.  2 mysql mysql      6 8月   7 09:22 mysql-files
-rw-r--r--.  1 root  root     666 8月   7 09:21 README
drwxr-xr-x. 28 root  root    4096 8月   7 09:21 share
drwxr-xr-x.  2 root  root      77 8月   7 09:21 support-files

[root@master mysql]# ./bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data

(2)从数据库

从数据库不能初始化

8.其他配置

(1)主数据库

[root@master mysql]# cp support-files/mysql.server /etc/init.d/mysql8

(2)从数据库

[root@slave ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql8

9.修改主数据库的my.cnf配置文件

#修改配置文件
[root@master mysql]# vim my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
port=3306

#启动mysql服务
[root@master mysql]# service mysql8 start
Starting MySQL.Logging to '/usr/local/mysql/data/master.err'.
. SUCCESS! 

#登录mysql
[root@master mysql]# ./bin/mysql -P3306 -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.33

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> alter user 'root'@'localhost' identified by 'Hui@2003';
 
Query OK, 0 rows affected (0.00 sec)

#退出
mysql> quit
Bye

#在my.cnf中继续添加内容
[root@master 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]# service mysql8 start
Starting MySQL SUCCESS! 

#登录mysql
[root@master mysql]# ./bin/mysql -P3306 -pHui@2003
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.33 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

#退出
mysql> quit
Bye

10.查看主数据库中目录的信息

#查看data目录
[root@master mysql]# ls -l data/
总用量 90572
-rw-r-----. 1 mysql mysql       56 8月   7 09:28 auto.cnf
-rw-r-----. 1 mysql mysql      477 8月   7 09:40 binlog.000001
-rw-r-----. 1 mysql mysql       16 8月   7 09:37 binlog.index
-rw-------. 1 mysql mysql     1680 8月   7 09:28 ca-key.pem
-rw-r--r--. 1 mysql mysql     1112 8月   7 09:28 ca.pem
-rw-r--r--. 1 mysql mysql     1112 8月   7 09:28 client-cert.pem
-rw-------. 1 mysql mysql     1680 8月   7 09:28 client-key.pem
-rw-r-----. 1 mysql mysql   196608 8月   7 09:40 #ib_16384_0.dblwr
-rw-r-----. 1 mysql mysql  8585216 8月   7 09:28 #ib_16384_1.dblwr
-rw-r-----. 1 mysql mysql     5985 8月   7 09:28 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 8月   7 09:40 ibdata1
-rw-r-----. 1 mysql mysql 12582912 8月   7 09:37 ibtmp1
drwxr-x---. 2 mysql mysql     4096 8月   7 09:37 #innodb_redo
drwxr-x---. 2 mysql mysql      187 8月   7 09:37 #innodb_temp
-rw-r-----. 1 mysql mysql      929 8月   7 09:37 master.err
-rw-r-----. 1 mysql mysql        5 8月   7 09:37 master.pid
drwxr-x---. 2 mysql mysql      143 8月   7 09:28 mysql
-rw-r-----. 1 mysql mysql 25165824 8月   7 09:40 mysql.ibd
drwxr-x---. 2 mysql mysql     8192 8月   7 09:28 performance_schema
-rw-------. 1 mysql mysql     1676 8月   7 09:28 private_key.pem
-rw-r--r--. 1 mysql mysql      452 8月   7 09:28 public_key.pem
-rw-r--r--. 1 mysql mysql     1112 8月   7 09:28 server-cert.pem
-rw-------. 1 mysql mysql     1680 8月   7 09:28 server-key.pem
drwxr-x---. 2 mysql mysql       28 8月   7 09:28 sys
-rw-r-----. 1 mysql mysql 16777216 8月   7 09:39 undo_001
-rw-r-----. 1 mysql mysql 16777216 8月   7 09:40 undo_002

#查看错误日志
[root@master mysql]# cat data/master.err
2024-08-07T01:37:35.068359Z 0 [System][MY-010116] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.33) starting as process 1925
2024-08-07T01:37:35.081787Z 1 [System][MY-013576] [InnoDB] InnoDB initialization has started.
2024-08-07T01:37:35.735592Z 1 [System][MY-013577] [InnoDB] InnoDB initialization has ended.
2024-08-07T01:37:36.273703Z 0 [Warning][MY-010068] [Server] CA certificate ca.pem is self signed.
2024-08-07T01:37:36.273749Z 0 [System][MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2024-08-07T01:37:36.306295Z 0 [System][MY-010931] [Server] /usr/local/mysql/bin/mysqld: ready for connections. Version: '8.0.33'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server - GPL.
2024-08-07T01:37:36.306366Z 0 [System][MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /tmp/mysqlx.sock

#查看/tmp中的套接字的信息
[root@master mysql]# ll /tmp
总用量 12
-rwx------. 1 root  root  836 8月   7 00:25 ks-script-pjA4To
srwxrwxrwx. 1 mysql mysql   0 8月   7 09:37 mysql.sock
-rw-------. 1 mysql mysql   5 8月   7 09:37 mysql.sock.lock
srwxrwxrwx. 1 mysql mysql   0 8月   7 09:37 mysqlx.sock
-rw-------. 1 mysql mysql   5 8月   7 09:37 mysqlx.sock.lock
drwx------. 3 root  root   17 8月   7 09:06 systemd-private-4192ed53ab514a048ba1c51132c3e28f-chronyd.service-skAuWy
drwx------. 3 root  root   17 8月   6 17:35 systemd-private-f93e9a7cc83a4e6ba1ea5a4ff1abcdc2-chronyd.service-2U7zsa
drwx------. 2 root  root    6 8月   7 00:25 vmware-root
-rw-------. 1 root  root    0 8月   7 00:21 yum.log

11.进行数据同步

(1)主数据库

#停止mysql服务
[root@master ~]# service mysql8 stop
Shutting down MySQL. SUCCESS! 

#删除/data中的auto.cnf
[root@master ~]# rm -rf /usr/local/mysql/data/auto.cnf 

#查看是否删除
[root@master ~]# ls /usr/local/mysql/data/
binlog.000001    client-key.pem     #innodb_redo  performance_schema  sys
binlog.index     #ib_16384_0.dblwr  #innodb_temp  private_key.pem     undo_001
ca-key.pem       #ib_16384_1.dblwr  master.err    public_key.pem      undo_002
ca.pem           ib_buffer_pool     mysql         server-cert.pem
client-cert.pem  ibdata1            mysql.ibd     server-key.pem

#将主数据库中的data同步到从数据库中
[root@master ~]# rsync -av /usr/local/mysql/data root@192.168.2.39:/usr/local/mysql

(2)从数据库

#在从库中查看有没有将data同步过来
[root@slave ~]# ls /usr/local/mysql
bin   docs     lib      man          README  support-files
data  include  LICENSE  mysql-files  share

#查看是否将主库data中的auto.cnf同步过来
[root@slave ~]# ls /usr/local/mysql/data
binlog.000001    client-key.pem     #innodb_redo  performance_schema  sys
binlog.index     #ib_16384_0.dblwr  #innodb_temp  private_key.pem     undo_001
ca-key.pem       #ib_16384_1.dblwr  master.err    public_key.pem      undo_002
ca.pem           ib_buffer_pool     mysql         server-cert.pem
client-cert.pem  ibdata1            mysql.ibd     server-key.pem

12.修改从数据库的my.cnf文件

[root@slave ~]# vim /usr/local/mysql/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock

log-error=/usr/local/mysql/data/err.log
relay-log=/usr/local/mysql/data/relaylog
character_set_server=utf8mb4
server-id=11

13.启动服务

(1)主数据库

[root@master ~]# service mysql8 start 
Starting MySQL.Logging to '/usr/local/mysql/data/db01-master.err'.
. SUCCESS! 

(2)从数据库

[root@slave ~]# service mysql8 start 
Starting MySQL.Logging to '/usr/local/mysql/data/err.log'.
. SUCCESS! 

14.将mysql添加到bin

(1)主数据库

#修改配置文件
[root@master ~]# vim /etc/profile

#在最后一行添加内容
export PATH=/usr/local/mysql/bin:$PATH
[root@master ~]# source /etc/profile

#尝试用mysql命令是否能进入
[root@master ~]# mysql -pHui@2003
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.33 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

(2)从数据库

#修改配置文件
[root@slave ~]# vim /etc/profile

#在最后一行添加内容
export PATH=/usr/local/mysql/bin:$PATH

[root@slave ~]# source /etc/profile

#尝试用mysql命令是否能进入
[root@slave ~]# mysql -h192.168.2.38 -uslave  --get-server-public-key -p
Enter password: slave_123;

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.33 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye

15.在主数据库中创建账号

#创建slave账户
mysql> create user 'slave'@'192.168.2.%' identified by 'slave_123;';
Query OK, 0 rows affected (0.02 sec)

#给slave账户所有权限
mysql> grant replication slave on *.* to 'slave'@'192.168.2.%';
Query OK, 0 rows affected (0.02 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;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 |      878 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

16.从数据库的配置

#获得远程主机master主机的公钥
[root@slave ~]# mysql -h192.168.2.38 -uslave  --get-server-public-key -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.33 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

#退出登录
mysql> quit
Bye

#启动mysql服务
[root@slave ~]# service mysql8 start
Starting MySQL SUCCESS! 

#登录本地的slave服务器数据库
[root@slave ~]# mysql -pHui@2003
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.33 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> change master to
    -> master_host='192.168.2.38',
    -> master_user='slave',
    -> master_password='slave_123;',
    -> master_port=3306,
    -> master_log_file='binlog.000002',
    -> master_log_pos=878;
Query OK, 0 rows affected, 9 warnings (0.03 sec)

#启动slave服务
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.02 sec)

#查看从服务器状态信息
mysql> show slave status\G

Slave_IO_Running和Slave_SQL_Running都是yes就表示主从数据库配置成功了

二、进行主从数据同步测试

(1)主数据库

#在主数据库上创建表,插入数据
#创建不了,是因为锁表
mysql> create database test;
ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock

#解锁
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

#创建库
mysql> create database test;
Query OK, 1 row affected (0.01 sec)

#使用数据库
mysql> use test;
Database changed

#创建student表
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,'张三','男');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ','男')' at line 1
mysql> insert into student values(1,'张三','男');
Query OK, 1 row affected (0.03 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> 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)

(2)从数据库

#在从数据库上查看有没有同步主的数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.01 sec)

#使用test数据库
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 | 王五      | 男     |
|  4 | 小凤仙    | 女     |
+----+-----------+--------+
4 rows in set (0.01 sec)

可以看到数据都同步过来了

四、mysql语句的增删改查

MySQL语句

1.新增

insert into库名称.表名

(id,username,password) values(1,"abc","123")

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

insert into 表名称 select * from 其他表

insert into 表 value () ,()

2.删除

delete from 表名

delete from tablename where id=3

delete from tablename where age>8

delete from tablename where name on ("a","b","c");

3.修改

update mysql.user set host='%' where name='root'

update user set password='abc' where username="hui"

4.查询

select

练习

查询

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 name from student;
+-----------+
| name      |
+-----------+
| 张三      |
| 李四      |
| 王五      |
| 小凤仙    |
+-----------+
4 rows in set (0.00 sec)

mysql> select id,gender from student;
+----+--------+
| id | gender |
+----+--------+
|  1 | 男     |
|  2 | 男     |
|  3 | 男     |
|  4 | 女     |
+----+--------+
4 rows in set (0.00 sec)



mysql> select a.*,b.* from student as a,student as b;
+----+-----------+--------+----+-----------+--------+
| id | name      | gender | id | name      | gender |
+----+-----------+--------+----+-----------+--------+
|  4 | 小凤仙    | 女     |  1 | 张三      | 男     |
|  3 | 王五      | 男     |  1 | 张三      | 男     |
|  2 | 李四      | 男     |  1 | 张三      | 男     |
|  1 | 张三      | 男     |  1 | 张三      | 男     |
|  4 | 小凤仙    | 女     |  2 | 李四      | 男     |
|  3 | 王五      | 男     |  2 | 李四      | 男     |
|  2 | 李四      | 男     |  2 | 李四      | 男     |
|  1 | 张三      | 男     |  2 | 李四      | 男     |
|  4 | 小凤仙    | 女     |  3 | 王五      | 男     |
|  3 | 王五      | 男     |  3 | 王五      | 男     |
|  2 | 李四      | 男     |  3 | 王五      | 男     |
|  1 | 张三      | 男     |  3 | 王五      | 男     |
|  4 | 小凤仙    | 女     |  4 | 小凤仙    | 女     |
|  3 | 王五      | 男     |  4 | 小凤仙    | 女     |
|  2 | 李四      | 男     |  4 | 小凤仙    | 女     |
|  1 | 张三      | 男     |  4 | 小凤仙    | 女     |
+----+-----------+--------+----+-----------+--------+
16 rows in set (0.00 sec)

创建可以远程登录的用户

mysql> create user 'haha'@'%' identified by 'Hui22003;';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on *.* to 'haha'@'%' ;
Query OK, 0 rows affected (0.01 sec)

远程登录:

统计  

# 用来统计的,这三个没区别

mysql> select count(*) from student;
+----------+
| count(*) |
+----------+
|        4 |
+----------+
1 row in set (0.07 sec)

mysql> select count(1) from student;
+----------+
| count(1) |
+----------+
|        4 |
+----------+
1 row in set (0.06 sec)

mysql> select count(gender) from student;
+---------------+
| count(gender) |
+---------------+
|             4 |
+---------------+
1 row in set (0.05 sec)

别名

mysql> select id,name,gender from student;
+----+-----------+--------+
| id | name      | gender |
+----+-----------+--------+
|  1 | 张三      | 男     |
|  2 | 李四      | 男     |
|  3 | 王五      | 男     |
|  4 | 小凤仙    | 女     |
+----+-----------+--------+
4 rows in set (0.00 sec)

mysql> select id as 编号,name,gender from student;
+--------+-----------+--------+
| 编号   | name      | gender |
+--------+-----------+--------+
|      1 | 张三      | 男     |
|      2 | 李四      | 男     |
|      3 | 王五      | 男     |
|      4 | 小凤仙    | 女     |
+--------+-----------+--------+
4 rows in set (0.00 sec)

mysql> select id as 编号,name as 姓名,gender as 性别 from student;
+--------+-----------+--------+
| 编号   | 姓名      | 性别   |
+--------+-----------+--------+
|      1 | 张三      | 男     |
|      2 | 李四      | 男     |
|      3 | 王五      | 男     |
|      4 | 小凤仙    | 女     |
+--------+-----------+--------+
4 rows in set (0.00 sec)

数据分析的基础

1.排序

1.max

2.min

2.汇总

1.count

2.sum

3.avg

练习

排序

mysql> select * from student;
+----+-----------+--------+
| id | name      | gender |
+----+-----------+--------+
|  1 | 张三      | 男     |
|  2 | 李四      | 男     |
|  3 | 王五      | 男     |
|  4 | 小凤仙    | 女     |
+----+-----------+--------+
4 rows in set (0.00 sec)



按照性别排序

mysql> select * from student order by gender;
+----+-----------+--------+
| id | name      | gender |
+----+-----------+--------+
|  4 | 小凤仙    | 女     |
|  1 | 张三      | 男     |
|  2 | 李四      | 男     |
|  3 | 王五      | 男     |
+----+-----------+--------+
4 rows in set (0.00 sec)

按照性别进行降序/升序排序

mysql> select * from student order by gender desc;
+----+-----------+--------+
| id | name      | gender |
+----+-----------+--------+
|  1 | 张三      | 男     |
|  2 | 李四      | 男     |
|  3 | 王五      | 男     |
|  4 | 小凤仙    | 女     |
+----+-----------+--------+
4 rows in set (0.00 sec)

mysql> select * from student order by gender asc;
+----+-----------+--------+
| id | name      | gender |
+----+-----------+--------+
|  4 | 小凤仙    | 女     |
|  1 | 张三      | 男     |
|  2 | 李四      | 男     |
|  3 | 王五      | 男     |
+----+-----------+--------+
4 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.03 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.00 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,400) ;
Query OK, 1 row affected (0.00 sec)

mysql> insert into product (name,price,qty) values("菠萝",12.4,70) ;
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)

查看升序排列的qty

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)

先查看按照升序排列的qty,然后在这基础上在按照price的升序排列进行查看

mysql> select * from (select * from product order by qty ) as a order 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)

当使用聚合查询以后,不能使用where,如果要使用,添加having

聚合查询

练习

统计有多少男生和多少女生

mysql> select gender,count(gender) from student group by gender;
+--------+---------------+
| gender | count(gender) |
+--------+---------------+
| 男     |             3 |
| 女     |             1 |
+--------+---------------+
2 rows in set (0.00 sec)

写别名  

mysql> select gender as 性别,count(gender) as 人数 from student group by gender;
+--------+--------+
| 性别   | 人数   |
+--------+--------+
| 男     |      3 |
| 女     |      1 |
+--------+--------+
2 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 year('1985-7-6') as birth;
+-------+
| birth |
+-------+
|  1985 |
+-------+
1 row in set (0.00 sec)

mysql> select month('1985-7-6') as birth;
+-------+
| birth |
+-------+
|     7 |
+-------+
1 row in set (0.00 sec)

mysql> select month('1985-7-6') as mon;
+------+
| mon  |
+------+
|    7 |
+------+
1 row in set (0.00 sec)

now-显示现在的时间

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-08-07 15:30:36 |
+---------------------+
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()) |
+---------------+
|            59 |
+---------------+
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.02 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:32:48 |   7.8 |  90 |
+----+---------------------+-------+-----+
5 rows in set (0.00 sec)

分组查询

mysql> select gender,count(gender) from student group by gender;
+--------+---------------+
| gender | count(gender) |
+--------+---------------+
| 男     |             3 |
| 女     |             1 |
+--------+---------------+
2 rows in set (0.00 sec)

mysql> select gender as 性别,count(gender) as 人数 from student group by gender;
+--------+--------+
| 性别   | 人数   |
+--------+--------+
| 男     |      3 |
| 女     |      1 |
+--------+--------+
2 rows in set (0.00 sec)

子句查询

mysql> select sum(tt) from (select *,price*qty as tt  from product) as a;
+--------------------+
| sum(tt)            |
+--------------------+
| 14887.999668121338 |
+--------------------+
1 row in set (0.00 sec)

向下取整

mysql> select trim("a b");
+-------------+
| trim("a b") |
+-------------+
| a b         |
+-------------+
1 row in set (0.00 sec)

mysql> select floor(3.14);
+-------------+
| floor(3.14) |
+-------------+
|           3 |
+-------------+
1 row in set (0.00 sec)

mysql> select floor(-3.14);
+--------------+
| floor(-3.14) |
+--------------+
|           -4 |
+--------------+
1 row in set (0.00 sec)

向上取整

mysql> select ceiling(9.8);
+--------------+
| ceiling(9.8) |
+--------------+
|           10 |
+--------------+
1 row in set (0.00 sec)

mysql> select ceiling(9.3);
+--------------+
| ceiling(9.3) |
+--------------+
|           10 |
+--------------+
1 row in set (0.00 sec)

四舍五入

mysql> select round(9.3);
+------------+
| round(9.3) |
+------------+
|          9 |
+------------+
1 row in set (0.00 sec)

mysql> select round(9.5);
+------------+
| round(9.5) |
+------------+
|         10 |
+------------+
1 row in set (0.00 sec)

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

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

相关文章

如何高效利用阿里云Docker镜像仓库管理您的容器镜像

文章目录 前言一、Docker镜像仓库1.公共仓库2.私有仓库 二、开通阿里云Docker镜像仓库ACR1.创建阿里云账号并开通容器镜像服务2.创建命名空间与镜像仓库 三、如何使用镜像仓库ACR1.登录阿里云Docker Registry2.推送镜像到阿里云私有镜像仓库3.从阿里云私有镜像仓库拉取镜像 总结…

CAN直接网络管理(20240805)

长安CAN网络管理规范 个人理解:管理CAN网络中各NM节点的工作模式(状态); 1.术语定义 👉节点地址:用于唯一标识网络中每个节点的单字节数字,取值范围是 0x00~0xFF。👉状态迁移&#x…

Vue3从零开始——掌握setup、ref和reactive函数的奥秘

文章目录 一、Vue 3 组合式 API 概述二、setup​ 函数的基本使用2.1 setup​ 函数的特点2.2 setup​ 函数的基本结构2.3 实现一个简单的小demo 三、ref​ 函数的功能和应用3.1 ref​函数介绍3.2 基本使用3.2.1 定义ref​数据3.2.2 修改响应式变量 3.3 使用ref​函数实现计数器 …

CTFHUB-web-RCE-过滤空格

开启题目 在 URL 后面拼接使用管道符执行注入,发现了 flag 的可疑文件 127.0.0.1|ls 因为这题过滤了空格,如果在 URL 中输入空格,则不会执行 ping 命令。那么这里将空格替换为 %09 ,也就是 ASCII 的 Tab 键。右键检查网页源代码发…

【OpenCV-Python实战项目】01-OCR文本检测

OpenCVTesseract文本检测 0.介绍1.基础功能实现1.1 字符检测1.2 单词检测1.3 只检测数字 2.工程应用2.1 封装类2.2 屏幕截图识别2.3 视频文本检测(待优化) 3.参考 0.介绍 (1)Tesseract是一个开源文本识别 (OCR)引擎,是…

TypeScript数组

数组 数组 数据存储的集合 声明数组的两种方式 1.只声明不赋值 &#xff1a;let arr1:string[] <>泛型 数组在使用前先赋值 2.边声明边赋值 let arr3:string[][];//空数组 // [数据1,数据2,.....数据n]数据之间用,隔开 let arr4:string[][张三,李四,王五]; let ar…

A. Array

https://codeforces.com/gym/102875/problem/A p<30 可以建立30颗线段树维护 然后看操作 一.add 懒标记 二.mul 三.mi 好像不能找到 于是懒标记貌似不能做&#xff0c; 但是考虑结果都要mod p,所以数都在0-p之间 其实就是i-> (操作)%p的映射 那就不用维护别的标…

MySQL —— 约束

MySQL —— 约束 引言not nulluniqueprimary key —— 主键auto_increment复合主键 foreign key —— 外键插入数据删除主表的数据 default 引言 在设计表的时候&#xff0c;有些列是必填项&#xff08;如果用户不填&#xff0c;那这个数据就没有必要存进数据库&#xff09;&a…

GPT-5:未来已来,你准备好了吗?

GPT-5 一年半后发布&#xff1f;对此你有何期待&#xff1f; IT之家6月22日消息&#xff0c;在美国达特茅斯工程学院周四公布的采访中&#xff0c;OpenAI首席技术官米拉穆拉蒂被问及GPT-5是否会在明年发布&#xff0c;给出了肯定答案并表示将在一年半后发布。此外&#xff0c;穆…

基于STM32的智能宠物喂食器

目录 引言环境准备工作 硬件准备软件安装与配置系统设计 系统架构硬件连接代码实现 初始化代码控制代码应用场景 宠物定时喂食远程控制喂食常见问题及解决方案 常见问题解决方案结论 1. 引言 智能宠物喂食器可以通过定时和远程控制&#xff0c;实现对宠物的科学喂养。本文将…

对抗生成:基于CycleGAN的风格迁移

对抗生成&#xff1a;基于CycleGAN的风格迁移 前言相关介绍CycleGAN 的工作原理核心思想主要组件训练目标 优点缺点应用实例总结 实验环境项目地址LinuxWindows 项目结构具体用法准备数据&#xff08;以vangogh2photo为例&#xff09;进行训练&#xff08;以vangogh2photo为例&…

【vulhub靶场之spring】——

简介&#xff1a; Spring是Java EE编程领域的一个轻量级开源框架&#xff0c;该框架由一个叫Rod Johnson的程序员在2002年最早提出并随后创建&#xff0c;是为了解决企业级编程开发中的复杂性&#xff0c;业务逻辑层和其他各层的松耦合问题&#xff0c;因此它将面向接口的编程思…

Shiro框架漏洞复现(附修复方法)

Apache Shiro是一个强大易用的Java安全框架&#xff0c;提供了认证、授权、加密和会话管理等功能。Shiro框架直观、易用&#xff0c;同时也能提供健壮的安全性。 在Shiro框架下&#xff0c;用户登陆成功后会生成一个经过加密的Cookie。其Cookie的Key的值为RememberMe&#xff0…

用Python实现超精准识别图片中的文字,零基础小白也能轻松学会!

将图片中的文字转换成可编辑的文本&#xff08;通常称为光学字符识别&#xff0c;Optical Character Recognition, OCR&#xff09;可以通过Python的一些库来实现。一个流行的OCR库是Tesseract-OCR&#xff0c;它可以通过Python的pytesseract库来调用。首先&#xff0c;你需要在…

【全国大学生电子设计竞赛】2021年B题

&#x1f970;&#x1f970;全国大学生电子设计大赛学习资料专栏已开启&#xff0c;限时免费&#xff0c;速速收藏~

3D,从无知到无畏

欢迎来到 PaQiuQiu 的空间 本文为【3D&#xff0c;从无知到无畏专栏目录】&#xff0c;方便大家更好的阅读! &#x1f680;~写在前面~ 近年来&#xff0c;3D视觉技术在众多领域飞速发展&#xff0c;除了智能机器人、自动驾驶、无人机等无人系统&#xff0c;在我们身边的AR、VR…

Nuxt2:强制删除window.__NUXT__中的数据

一、问题描述 在以前的一篇文章《Nuxt3: 强制删除__NUXT_DATA__的一种方式》中曾介绍了在Nuxt3中如何删除存在于页面id为__NUXT_DATA__的script节点中的数据。 此次&#xff0c;Nuxt2与Nuxt3不同在于它的数据是存在于window.__NUXT__&#xff0c;那么该如何处理呢&#xff1f;…

Python 动态进度条实现,多个实例来展示实现方式

目录 1. 使用 print 函数 2. 使用 tqdm 库 3. 自定义样式 4. 多进度条 5. 嵌套进度条 6. 更新频率控制 7. 动态描述 8. 自定义回调 9. 使用 click 库 10. 使用 rich 库 文末福利 文末赠免费精品编程资料~~ 在编写Python脚本时&#xff0c;特别是在处理长时间运行的…

PDF Guru Anki:一款以PDF为中心的多功能办公学习工具箱

PDF Guru Anki 是一个假想的工具箱&#xff0c;因为目前我没有关于这个具体产品的详细信息。但是&#xff0c;根据这个名字和一般的PDF工具箱功能&#xff0c;我可以想象一些可能的独特功能&#xff0c;这些功能可以包括&#xff1a; PDF 阅读器&#xff1a;提供高效的PDF阅读…

linux下的C++程序

1.安装g编译环境&#xff08;c&#xff09;、gcc编译环境&#xff08;c语言&#xff09; sudo yum install gcc或者gcc-c //安装gcc/g编译(用管理员权限弄&#xff09; 验证是否安装成功 gcc或者g --version //如果显示版本号&#xff0c;则表示安装成功 sudo yum remove g…