当前位置:   article > 正文

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

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

一、mysql环境的配置

1.环境准备

(1)主数据库

  1. #关闭防火墙
  2. [root@master ~]# systemctl stop firewalld
  3. #关闭selinux
  4. [root@master ~]# setenforce 0
  5. #下载lrzsz工具
  6. [root@master ~]# yum -y install lrzsz
  7. #安装rsync
  8. [root@master ~]# yum -y install rsync
  9. #查看是否安装了libaio
  10. [root@master ~]# rpm -qa|grep libaio
  11. libaio-0.3.109-13.el7.x86_64
  12. #查看是否安装了rsync
  13. [root@master ~]# rpm -qa|grep rsync
  14. rsync-3.1.2-12.el7_9.x86_64

(2)从数据库

  1. #关闭防火墙
  2. [root@slave ~]# systemctl stop firewalld
  3. #关闭selinux
  4. [root@slave ~]# setenforce 0
  5. #安装lrzsz
  6. [root@slave ~]# yum -y install lrzsz
  7. #安装rsync
  8. [root@slave ~]# yum -y install rsync

2.解压

(1)主数据库

  1. # 将mysqlmysql-8.0.33-linux-glibc2.12-x86_64.tar包拖到xshell中
  2. [root@master ~]# rz -E
  3. rz waiting to receive.
  4. [root@master ~]# ls
  5. anaconda-ks.cfg mysql-8.0.33-linux-glibc2.12-x86_64.tar
  6. # 解压
  7. [root@master ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar
  8. [root@master ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz
  9. # 查看是否解压成功
  10. [root@master ~]# ls
  11. anaconda-ks.cfg
  12. mysql-8.0.33-linux-glibc2.12-x86_64
  13. mysql-8.0.33-linux-glibc2.12-x86_64.tar
  14. mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz
  15. mysql-router-8.0.33-linux-glibc2.12-x86_64.tar.xz
  16. mysql-test-8.0.33-linux-glibc2.12-x86_64.tar.xz

(2)从数据库

  1. [root@slave ~]# ls
  2. anaconda-ks.cfg mysql-8.0.33-linux-glibc2.12-x86_64.tar
  3. [root@slave ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar
  4. [root@slave ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz
  5. [root@slave ~]# ls
  6. anaconda-ks.cfg
  7. mysql-8.0.33-linux-glibc2.12-x86_64
  8. mysql-8.0.33-linux-glibc2.12-x86_64.tar
  9. mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz
  10. mysql-router-8.0.33-linux-glibc2.12-x86_64.tar.xz
  11. 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)主数据库

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

(2)从数据库

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

5.修改权限

(1)主数据库

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

(2)从数据库

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

6.删除配置文件

(1)主数据库

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

(2)从数据库

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

7.初始化

(1)主数据库

  1. # 初始化 从数据库不能初始化
  2. [root@master mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql
  3. 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
  4. 2024-08-07T01:28:33.420601Z 1 [System][MY-013576] [InnoDB] InnoDB initialization has started.
  5. 2024-08-07T01:28:34.159124Z 1 [System][MY-013577] [InnoDB] InnoDB initialization has ended.
  6. 2024-08-07T01:28:35.485851Z 6 [Note][MY-010454] [Server] A temporary password is generated for root@localhost: CpjsNb1yO-fd
  7. [root@master mysql]# ls /usr/local/mysql
  8. bin docs lib man README support-files
  9. data include LICENSE mysql-files share
  10. [root@master mysql]# ll
  11. 总用量 296
  12. drwxr-xr-x. 2 root root 4096 87 09:21 bin
  13. drwxr-x---. 7 mysql mysql 4096 87 09:28 data
  14. drwxr-xr-x. 2 root root 38 87 09:21 docs
  15. drwxr-xr-x. 3 root root 282 87 09:21 include
  16. drwxr-xr-x. 6 root root 201 87 09:21 lib
  17. -rw-r--r--. 1 root root 284945 87 09:21 LICENSE
  18. drwxr-xr-x. 4 root root 30 87 09:21 man
  19. drwxr-xr-x. 2 mysql mysql 6 87 09:22 mysql-files
  20. -rw-r--r--. 1 root root 666 87 09:21 README
  21. drwxr-xr-x. 28 root root 4096 87 09:21 share
  22. drwxr-xr-x. 2 root root 77 87 09:21 support-files
  23. [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配置文件

  1. #修改配置文件
  2. [root@master mysql]# vim my.cnf
  3. [mysqld]
  4. basedir=/usr/local/mysql
  5. datadir=/usr/local/mysql/data
  6. socket=/tmp/mysql.sock
  7. port=3306
  8. #启动mysql服务
  9. [root@master mysql]# service mysql8 start
  10. Starting MySQL.Logging to '/usr/local/mysql/data/master.err'.
  11. . SUCCESS!
  12. #登录mysql
  13. [root@master mysql]# ./bin/mysql -P3306 -p
  14. Enter password:
  15. Welcome to the MySQL monitor. Commands end with ; or \g.
  16. Your MySQL connection id is 8
  17. Server version: 8.0.33
  18. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  19. Oracle is a registered trademark of Oracle Corporation and/or its
  20. affiliates. Other names may be trademarks of their respective
  21. owners.
  22. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  23. mysql> alter user 'root'@'localhost' identified by 'Hui@2003';
  24. Query OK, 0 rows affected (0.00 sec)
  25. #退出
  26. mysql> quit
  27. Bye
  28. #在my.cnf中继续添加内容
  29. [root@master mysql]# vim my.cnf
  30. [mysqld]
  31. basedir=/usr/local/mysql
  32. datadir=/usr/local/mysql/data
  33. socket=/tmp/mysql.sock
  34. port=3306
  35. log-error=/usr/local/mysql/data/db01-master.err
  36. log-bin=/usr/local/mysql/data/binlog
  37. server-id=10
  38. character_set_server=utf8mb4
  39. #启动服务
  40. [root@master mysql]# service mysql8 start
  41. Starting MySQL SUCCESS!
  42. #登录mysql
  43. [root@master mysql]# ./bin/mysql -P3306 -pHui@2003
  44. mysql: [Warning] Using a password on the command line interface can be insecure.
  45. Welcome to the MySQL monitor. Commands end with ; or \g.
  46. Your MySQL connection id is 9
  47. Server version: 8.0.33 MySQL Community Server - GPL
  48. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  49. Oracle is a registered trademark of Oracle Corporation and/or its
  50. affiliates. Other names may be trademarks of their respective
  51. owners.
  52. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  53. #退出
  54. mysql> quit
  55. Bye

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

  1. #查看data目录
  2. [root@master mysql]# ls -l data/
  3. 总用量 90572
  4. -rw-r-----. 1 mysql mysql 56 87 09:28 auto.cnf
  5. -rw-r-----. 1 mysql mysql 477 87 09:40 binlog.000001
  6. -rw-r-----. 1 mysql mysql 16 87 09:37 binlog.index
  7. -rw-------. 1 mysql mysql 1680 87 09:28 ca-key.pem
  8. -rw-r--r--. 1 mysql mysql 1112 87 09:28 ca.pem
  9. -rw-r--r--. 1 mysql mysql 1112 87 09:28 client-cert.pem
  10. -rw-------. 1 mysql mysql 1680 87 09:28 client-key.pem
  11. -rw-r-----. 1 mysql mysql 196608 87 09:40 #ib_16384_0.dblwr
  12. -rw-r-----. 1 mysql mysql 8585216 87 09:28 #ib_16384_1.dblwr
  13. -rw-r-----. 1 mysql mysql 5985 87 09:28 ib_buffer_pool
  14. -rw-r-----. 1 mysql mysql 12582912 87 09:40 ibdata1
  15. -rw-r-----. 1 mysql mysql 12582912 87 09:37 ibtmp1
  16. drwxr-x---. 2 mysql mysql 4096 87 09:37 #innodb_redo
  17. drwxr-x---. 2 mysql mysql 187 87 09:37 #innodb_temp
  18. -rw-r-----. 1 mysql mysql 929 87 09:37 master.err
  19. -rw-r-----. 1 mysql mysql 5 87 09:37 master.pid
  20. drwxr-x---. 2 mysql mysql 143 87 09:28 mysql
  21. -rw-r-----. 1 mysql mysql 25165824 87 09:40 mysql.ibd
  22. drwxr-x---. 2 mysql mysql 8192 87 09:28 performance_schema
  23. -rw-------. 1 mysql mysql 1676 87 09:28 private_key.pem
  24. -rw-r--r--. 1 mysql mysql 452 87 09:28 public_key.pem
  25. -rw-r--r--. 1 mysql mysql 1112 87 09:28 server-cert.pem
  26. -rw-------. 1 mysql mysql 1680 87 09:28 server-key.pem
  27. drwxr-x---. 2 mysql mysql 28 87 09:28 sys
  28. -rw-r-----. 1 mysql mysql 16777216 87 09:39 undo_001
  29. -rw-r-----. 1 mysql mysql 16777216 87 09:40 undo_002
  30. #查看错误日志
  31. [root@master mysql]# cat data/master.err
  32. 2024-08-07T01:37:35.068359Z 0 [System][MY-010116] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.33) starting as process 1925
  33. 2024-08-07T01:37:35.081787Z 1 [System][MY-013576] [InnoDB] InnoDB initialization has started.
  34. 2024-08-07T01:37:35.735592Z 1 [System][MY-013577] [InnoDB] InnoDB initialization has ended.
  35. 2024-08-07T01:37:36.273703Z 0 [Warning][MY-010068] [Server] CA certificate ca.pem is self signed.
  36. 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.
  37. 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.
  38. 2024-08-07T01:37:36.306366Z 0 [System][MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /tmp/mysqlx.sock
  39. #查看/tmp中的套接字的信息
  40. [root@master mysql]# ll /tmp
  41. 总用量 12
  42. -rwx------. 1 root root 836 87 00:25 ks-script-pjA4To
  43. srwxrwxrwx. 1 mysql mysql 0 87 09:37 mysql.sock
  44. -rw-------. 1 mysql mysql 5 87 09:37 mysql.sock.lock
  45. srwxrwxrwx. 1 mysql mysql 0 87 09:37 mysqlx.sock
  46. -rw-------. 1 mysql mysql 5 87 09:37 mysqlx.sock.lock
  47. drwx------. 3 root root 17 87 09:06 systemd-private-4192ed53ab514a048ba1c51132c3e28f-chronyd.service-skAuWy
  48. drwx------. 3 root root 17 86 17:35 systemd-private-f93e9a7cc83a4e6ba1ea5a4ff1abcdc2-chronyd.service-2U7zsa
  49. drwx------. 2 root root 6 87 00:25 vmware-root
  50. -rw-------. 1 root root 0 87 00:21 yum.log

11.进行数据同步

(1)主数据库

  1. #停止mysql服务
  2. [root@master ~]# service mysql8 stop
  3. Shutting down MySQL. SUCCESS!
  4. #删除/data中的auto.cnf
  5. [root@master ~]# rm -rf /usr/local/mysql/data/auto.cnf
  6. #查看是否删除
  7. [root@master ~]# ls /usr/local/mysql/data/
  8. binlog.000001 client-key.pem #innodb_redo performance_schema sys
  9. binlog.index #ib_16384_0.dblwr #innodb_temp private_key.pem undo_001
  10. ca-key.pem #ib_16384_1.dblwr master.err public_key.pem undo_002
  11. ca.pem ib_buffer_pool mysql server-cert.pem
  12. client-cert.pem ibdata1 mysql.ibd server-key.pem
  13. #将主数据库中的data同步到从数据库中
  14. [root@master ~]# rsync -av /usr/local/mysql/data root@192.168.2.39:/usr/local/mysql

(2)从数据库

  1. #在从库中查看有没有将data同步过来
  2. [root@slave ~]# ls /usr/local/mysql
  3. bin docs lib man README support-files
  4. data include LICENSE mysql-files share
  5. #查看是否将主库data中的auto.cnf同步过来
  6. [root@slave ~]# ls /usr/local/mysql/data
  7. binlog.000001 client-key.pem #innodb_redo performance_schema sys
  8. binlog.index #ib_16384_0.dblwr #innodb_temp private_key.pem undo_001
  9. ca-key.pem #ib_16384_1.dblwr master.err public_key.pem undo_002
  10. ca.pem ib_buffer_pool mysql server-cert.pem
  11. client-cert.pem ibdata1 mysql.ibd server-key.pem

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

  1. [root@slave ~]# vim /usr/local/mysql/my.cnf
  2. [mysqld]
  3. basedir=/usr/local/mysql
  4. datadir=/usr/local/mysql/data
  5. socket=/tmp/mysql.sock
  6. log-error=/usr/local/mysql/data/err.log
  7. relay-log=/usr/local/mysql/data/relaylog
  8. character_set_server=utf8mb4
  9. server-id=11

13.启动服务

(1)主数据库

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

(2)从数据库

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

14.将mysql添加到bin

(1)主数据库

  1. #修改配置文件
  2. [root@master ~]# vim /etc/profile
  3. #在最后一行添加内容
  4. export PATH=/usr/local/mysql/bin:$PATH
  5. [root@master ~]# source /etc/profile
  6. #尝试用mysql命令是否能进入
  7. [root@master ~]# mysql -pHui@2003
  8. mysql: [Warning] Using a password on the command line interface can be insecure.
  9. Welcome to the MySQL monitor. Commands end with ; or \g.
  10. Your MySQL connection id is 8
  11. Server version: 8.0.33 MySQL Community Server - GPL
  12. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  13. Oracle is a registered trademark of Oracle Corporation and/or its
  14. affiliates. Other names may be trademarks of their respective
  15. owners.
  16. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  17. mysql>

(2)从数据库

  1. #修改配置文件
  2. [root@slave ~]# vim /etc/profile
  3. #在最后一行添加内容
  4. export PATH=/usr/local/mysql/bin:$PATH
  5. [root@slave ~]# source /etc/profile
  6. #尝试用mysql命令是否能进入
  7. [root@slave ~]# mysql -h192.168.2.38 -uslave --get-server-public-key -p
  8. Enter password: slave_123;
  9. Welcome to the MySQL monitor. Commands end with ; or \g.
  10. Your MySQL connection id is 15
  11. Server version: 8.0.33 MySQL Community Server - GPL
  12. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  13. Oracle is a registered trademark of Oracle Corporation and/or its
  14. affiliates. Other names may be trademarks of their respective
  15. owners.
  16. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  17. mysql> quit
  18. Bye

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

  1. #创建slave账户
  2. mysql> create user 'slave'@'192.168.2.%' identified by 'slave_123;'
  3. Query OK, 0 rows affected (0.02 sec)
  4. #给slave账户所有权限
  5. mysql> grant replication slave on *.* to 'slave'@'192.168.2.%';
  6. Query OK, 0 rows affected (0.02 sec)
  7. #刷新权限
  8. mysql> flush privileges;
  9. Query OK, 0 rows affected (0.00 sec)
  10. #防止数据被篡改,进行锁表
  11. mysql> flush tables with read lock;
  12. Query OK, 0 rows affected (0.00 sec)
  13. #查看主数据库的状态信息
  14. mysql> show master status;
  15. +---------------+----------+--------------+------------------+-------------------+
  16. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  17. +---------------+----------+--------------+------------------+-------------------+
  18. | binlog.000002 | 878 | | | |
  19. +---------------+----------+--------------+------------------+-------------------+
  20. 1 row in set (0.00 sec)

16.从数据库的配置

  1. #获得远程主机master主机的公钥
  2. [root@slave ~]# mysql -h192.168.2.38 -uslave --get-server-public-key -p
  3. Enter password:
  4. Welcome to the MySQL monitor. Commands end with ; or \g.
  5. Your MySQL connection id is 15
  6. Server version: 8.0.33 MySQL Community Server - GPL
  7. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. #退出登录
  13. mysql> quit
  14. Bye
  15. #启动mysql服务
  16. [root@slave ~]# service mysql8 start
  17. Starting MySQL SUCCESS!
  18. #登录本地的slave服务器数据库
  19. [root@slave ~]# mysql -pHui@2003
  20. mysql: [Warning] Using a password on the command line interface can be insecure.
  21. Welcome to the MySQL monitor. Commands end with ; or \g.
  22. Your MySQL connection id is 9
  23. Server version: 8.0.33 MySQL Community Server - GPL
  24. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  25. Oracle is a registered trademark of Oracle Corporation and/or its
  26. affiliates. Other names may be trademarks of their respective
  27. owners.
  28. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  29. mysql> change master to
  30. -> master_host='192.168.2.38',
  31. -> master_user='slave',
  32. -> master_password='slave_123;',
  33. -> master_port=3306,
  34. -> master_log_file='binlog.000002',
  35. -> master_log_pos=878;
  36. Query OK, 0 rows affected, 9 warnings (0.03 sec)
  37. #启动slave服务
  38. mysql> start slave;
  39. Query OK, 0 rows affected, 1 warning (0.02 sec)
  40. #查看从服务器状态信息
  41. mysql> show slave status\G

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

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

(1)主数据库

  1. #在主数据库上创建表,插入数据
  2. #创建不了,是因为锁表
  3. mysql> create database test;
  4. ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock
  5. #解锁
  6. mysql> unlock tables;
  7. Query OK, 0 rows affected (0.00 sec)
  8. #创建库
  9. mysql> create database test;
  10. Query OK, 1 row affected (0.01 sec)
  11. #使用数据库
  12. mysql> use test;
  13. Database changed
  14. #创建student表
  15. mysql> create table student(id int primary key,name varchar(45) not null,gender varchar(4) not null);
  16. Query OK, 0 rows affected (0.02 sec)
  17. #插入数据
  18. mysql> insert into student values(1,'张三','男');
  19. 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
  20. mysql> insert into student values(1,'张三','男');
  21. Query OK, 1 row affected (0.03 sec)
  22. mysql> insert into student values(2,'李四','男');
  23. Query OK, 1 row affected (0.00 sec)
  24. mysql> insert into student values(3,'王五','男');
  25. Query OK, 1 row affected (0.00 sec)
  26. mysql> insert into student values(4,'小凤仙','女');
  27. Query OK, 1 row affected (0.00 sec)
  28. #查看表中的数据
  29. mysql> select * from student;
  30. +----+-----------+--------+
  31. | id | name | gender |
  32. +----+-----------+--------+
  33. | 1 | 张三 | 男 |
  34. | 2 | 李四 | 男 |
  35. | 3 | 王五 | 男 |
  36. | 4 | 小凤仙 | 女 |
  37. +----+-----------+--------+
  38. 4 rows in set (0.00 sec)

(2)从数据库

  1. #在从数据库上查看有没有同步主的数据库
  2. mysql> show databases;
  3. +--------------------+
  4. | Database |
  5. +--------------------+
  6. | information_schema |
  7. | mysql |
  8. | performance_schema |
  9. | sys |
  10. | test |
  11. +--------------------+
  12. 5 rows in set (0.01 sec)
  13. #使用test数据库
  14. mysql> use test;
  15. Reading table information for completion of table and column names
  16. You can turn off this feature to get a quicker startup with -A
  17. Database changed
  18. #可以看到数据已经同步过来了
  19. mysql> select * from student;
  20. +----+-----------+--------+
  21. | id | name | gender |
  22. +----+-----------+--------+
  23. | 1 | 张三 | 男 |
  24. | 2 | 李四 | 男 |
  25. | 3 | 王五 | 男 |
  26. | 4 | 小凤仙 | 女 |
  27. +----+-----------+--------+
  28. 4 rows in set (0.01 sec)
  29. 可以看到数据都同步过来了

四、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

练习

查询

  1. mysql> use test;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. mysql> select name from student;
  6. +-----------+
  7. | name |
  8. +-----------+
  9. | 张三 |
  10. | 李四 |
  11. | 王五 |
  12. | 小凤仙 |
  13. +-----------+
  14. 4 rows in set (0.00 sec)
  15. mysql> select id,gender from student;
  16. +----+--------+
  17. | id | gender |
  18. +----+--------+
  19. | 1 | 男 |
  20. | 2 | 男 |
  21. | 3 | 男 |
  22. | 4 | 女 |
  23. +----+--------+
  24. 4 rows in set (0.00 sec)
  25. mysql> select a.*,b.* from student as a,student as b;
  26. +----+-----------+--------+----+-----------+--------+
  27. | id | name | gender | id | name | gender |
  28. +----+-----------+--------+----+-----------+--------+
  29. | 4 | 小凤仙 | 女 | 1 | 张三 | 男 |
  30. | 3 | 王五 | 男 | 1 | 张三 | 男 |
  31. | 2 | 李四 | 男 | 1 | 张三 | 男 |
  32. | 1 | 张三 | 男 | 1 | 张三 | 男 |
  33. | 4 | 小凤仙 | 女 | 2 | 李四 | 男 |
  34. | 3 | 王五 | 男 | 2 | 李四 | 男 |
  35. | 2 | 李四 | 男 | 2 | 李四 | 男 |
  36. | 1 | 张三 | 男 | 2 | 李四 | 男 |
  37. | 4 | 小凤仙 | 女 | 3 | 王五 | 男 |
  38. | 3 | 王五 | 男 | 3 | 王五 | 男 |
  39. | 2 | 李四 | 男 | 3 | 王五 | 男 |
  40. | 1 | 张三 | 男 | 3 | 王五 | 男 |
  41. | 4 | 小凤仙 | 女 | 4 | 小凤仙 | 女 |
  42. | 3 | 王五 | 男 | 4 | 小凤仙 | 女 |
  43. | 2 | 李四 | 男 | 4 | 小凤仙 | 女 |
  44. | 1 | 张三 | 男 | 4 | 小凤仙 | 女 |
  45. +----+-----------+--------+----+-----------+--------+
  46. 16 rows in set (0.00 sec)

创建可以远程登录的用户

  1. mysql> create user 'haha'@'%' identified by 'Hui22003;'
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> grant all privileges on *.* to 'haha'@'%' ;
  4. Query OK, 0 rows affected (0.01 sec)

远程登录:

统计  

  1. # 用来统计的,这三个没区别
  2. mysql> select count(*) from student;
  3. +----------+
  4. | count(*) |
  5. +----------+
  6. | 4 |
  7. +----------+
  8. 1 row in set (0.07 sec)
  9. mysql> select count(1) from student;
  10. +----------+
  11. | count(1) |
  12. +----------+
  13. | 4 |
  14. +----------+
  15. 1 row in set (0.06 sec)
  16. mysql> select count(gender) from student;
  17. +---------------+
  18. | count(gender) |
  19. +---------------+
  20. | 4 |
  21. +---------------+
  22. 1 row in set (0.05 sec)

别名

  1. mysql> select id,name,gender from student;
  2. +----+-----------+--------+
  3. | id | name | gender |
  4. +----+-----------+--------+
  5. | 1 | 张三 | 男 |
  6. | 2 | 李四 | 男 |
  7. | 3 | 王五 | 男 |
  8. | 4 | 小凤仙 | 女 |
  9. +----+-----------+--------+
  10. 4 rows in set (0.00 sec)
  11. mysql> select id as 编号,name,gender from student;
  12. +--------+-----------+--------+
  13. | 编号 | name | gender |
  14. +--------+-----------+--------+
  15. | 1 | 张三 | 男 |
  16. | 2 | 李四 | 男 |
  17. | 3 | 王五 | 男 |
  18. | 4 | 小凤仙 | 女 |
  19. +--------+-----------+--------+
  20. 4 rows in set (0.00 sec)
  21. mysql> select id as 编号,name as 姓名,gender as 性别 from student;
  22. +--------+-----------+--------+
  23. | 编号 | 姓名 | 性别 |
  24. +--------+-----------+--------+
  25. | 1 | 张三 | 男 |
  26. | 2 | 李四 | 男 |
  27. | 3 | 王五 | 男 |
  28. | 4 | 小凤仙 | 女 |
  29. +--------+-----------+--------+
  30. 4 rows in set (0.00 sec)

数据分析的基础

1.排序

1.max

2.min

2.汇总

1.count

2.sum

3.avg

练习

排序

  1. mysql> select * from student;
  2. +----+-----------+--------+
  3. | id | name | gender |
  4. +----+-----------+--------+
  5. | 1 | 张三 | 男 |
  6. | 2 | 李四 | 男 |
  7. | 3 | 王五 | 男 |
  8. | 4 | 小凤仙 | 女 |
  9. +----+-----------+--------+
  10. 4 rows in set (0.00 sec)
  11. 按照性别排序
  12. mysql> select * from student order by gender;
  13. +----+-----------+--------+
  14. | id | name | gender |
  15. +----+-----------+--------+
  16. | 4 | 小凤仙 | 女 |
  17. | 1 | 张三 | 男 |
  18. | 2 | 李四 | 男 |
  19. | 3 | 王五 | 男 |
  20. +----+-----------+--------+
  21. 4 rows in set (0.00 sec)
  22. 按照性别进行降序/升序排序
  23. mysql> select * from student order by gender desc;
  24. +----+-----------+--------+
  25. | id | name | gender |
  26. +----+-----------+--------+
  27. | 1 | 张三 | 男 |
  28. | 2 | 李四 | 男 |
  29. | 3 | 王五 | 男 |
  30. | 4 | 小凤仙 | 女 |
  31. +----+-----------+--------+
  32. 4 rows in set (0.00 sec)
  33. mysql> select * from student order by gender asc;
  34. +----+-----------+--------+
  35. | id | name | gender |
  36. +----+-----------+--------+
  37. | 4 | 小凤仙 | 女 |
  38. | 1 | 张三 | 男 |
  39. | 2 | 李四 | 男 |
  40. | 3 | 王五 | 男 |
  41. +----+-----------+--------+
  42. 4 rows in set (0.00 sec)

新建一个表

  1. mysql> create table product(
  2. -> id int primary key auto_increment,
  3. -> name varchar(45) not null,
  4. -> price float not null,
  5. -> qty int not null);
  6. Query OK, 0 rows affected (0.03 sec)

查看表的内容

  1. mysql> desc product;
  2. +-------+-------------+------+-----+---------+----------------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +-------+-------------+------+-----+---------+----------------+
  5. | id | int | NO | PRI | NULL | auto_increment |
  6. | name | varchar(45) | NO | | NULL | |
  7. | price | float | NO | | NULL | |
  8. | qty | int | NO | | NULL | |
  9. +-------+-------------+------+-----+---------+----------------+
  10. 4 rows in set (0.00 sec)

插入数据

  1. mysql> insert into product (name,price,qty) values("香蕉",8.5,200) ;
  2. Query OK, 1 row affected (0.01 sec)
  3. mysql> insert into product (name,price,qty) values("苹果",12.5,400) ;
  4. Query OK, 1 row affected (0.00 sec)
  5. mysql> insert into product (name,price,qty) values("菠萝",12.4,70) ;
  6. Query OK, 1 row affected (0.00 sec)
  7. mysql> insert into product (name,price,qty) values("哈密瓜",18.3,400) ;
  8. Query OK, 1 row affected (0.00 sec)

查看表

  1. mysql> select * from product;
  2. +----+-----------+-------+-----+
  3. | id | name | price | qty |
  4. +----+-----------+-------+-----+
  5. | 1 | 香蕉 | 8.5 | 200 |
  6. | 2 | 苹果 | 12.5 | 400 |
  7. | 3 | 菠萝 | 12.4 | 70 |
  8. | 4 | 哈密瓜 | 18.3 | 400 |
  9. +----+-----------+-------+-----+
  10. 4 rows in set (0.00 sec)

查看升序排列的qty

  1. mysql> select * from product order by qty;
  2. +----+-----------+-------+-----+
  3. | id | name | price | qty |
  4. +----+-----------+-------+-----+
  5. | 3 | 菠萝 | 12.4 | 70 |
  6. | 1 | 香蕉 | 8.5 | 200 |
  7. | 2 | 苹果 | 12.5 | 400 |
  8. | 4 | 哈密瓜 | 18.3 | 400 |
  9. +----+-----------+-------+-----+
  10. 4 rows in set (0.00 sec)

查看升序排列的价格

  1. mysql> select * from product order by price;
  2. +----+-----------+-------+-----+
  3. | id | name | price | qty |
  4. +----+-----------+-------+-----+
  5. | 1 | 香蕉 | 8.5 | 200 |
  6. | 3 | 菠萝 | 12.4 | 70 |
  7. | 2 | 苹果 | 12.5 | 400 |
  8. | 4 | 哈密瓜 | 18.3 | 400 |
  9. +----+-----------+-------+-----+
  10. 4 rows in set (0.00 sec)

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

  1. mysql> select * from (select * from product order by qty ) as a order by a.price;
  2. +----+-----------+-------+-----+
  3. | id | name | price | qty |
  4. +----+-----------+-------+-----+
  5. | 1 | 香蕉 | 8.5 | 200 |
  6. | 3 | 菠萝 | 12.4 | 70 |
  7. | 2 | 苹果 | 12.5 | 400 |
  8. | 4 | 哈密瓜 | 18.3 | 400 |
  9. +----+-----------+-------+-----+
  10. 4 rows in set (0.00 sec)

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

聚合查询

练习

  1. 统计有多少男生和多少女生
  2. mysql> select gender,count(gender) from student group by gender;
  3. +--------+---------------+
  4. | gender | count(gender) |
  5. +--------+---------------+
  6. | 男 | 3 |
  7. | 女 | 1 |
  8. +--------+---------------+
  9. 2 rows in set (0.00 sec)

写别名  

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

 查看最大值、最小值、求和、求平均数

  1. mysql> select max(price) from product;
  2. +------------+
  3. | max(price) |
  4. +------------+
  5. | 18.3 |
  6. +------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select min(price) from product;
  9. +------------+
  10. | min(price) |
  11. +------------+
  12. | 8.5 |
  13. +------------+
  14. 1 row in set (0.00 sec)
  15. mysql> select sum(price) from product;
  16. +-------------------+
  17. | sum(price) |
  18. +-------------------+
  19. | 51.69999885559082 |
  20. +-------------------+
  21. 1 row in set (0.00 sec)
  22. mysql> select avg(price) from product;
  23. +--------------------+
  24. | avg(price) |
  25. +--------------------+
  26. | 12.924999713897705 |
  27. +--------------------+
  28. 1 row in set (0.00 sec)

日期 

  1. mysql> select year('1985-7-6') as birth;
  2. +-------+
  3. | birth |
  4. +-------+
  5. | 1985 |
  6. +-------+
  7. 1 row in set (0.00 sec)
  8. mysql> select month('1985-7-6') as birth;
  9. +-------+
  10. | birth |
  11. +-------+
  12. | 7 |
  13. +-------+
  14. 1 row in set (0.00 sec)
  15. mysql> select month('1985-7-6') as mon;
  16. +------+
  17. | mon |
  18. +------+
  19. | 7 |
  20. +------+
  21. 1 row in set (0.00 sec)

now-显示现在的时间

  1. mysql> select now();
  2. +---------------------+
  3. | now() |
  4. +---------------------+
  5. | 2024-08-07 15:30:36 |
  6. +---------------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select year(now());
  9. +-------------+
  10. | year(now()) |
  11. +-------------+
  12. | 2024 |
  13. +-------------+
  14. 1 row in set (0.00 sec)
  15. mysql> select second(now());
  16. +---------------+
  17. | second(now()) |
  18. +---------------+
  19. | 59 |
  20. +---------------+
  21. 1 row in set (0.00 sec)
  22. mysql> insert into product (name,price,qty) values(now(),7.8,90);
  23. Query OK, 1 row affected (0.02 sec)
  24. mysql> select * from product;
  25. +----+---------------------+-------+-----+
  26. | id | name | price | qty |
  27. +----+---------------------+-------+-----+
  28. | 1 | 香蕉 | 8.5 | 200 |
  29. | 2 | 苹果 | 12.5 | 400 |
  30. | 3 | 菠萝 | 12.4 | 70 |
  31. | 4 | 哈密瓜 | 18.3 | 400 |
  32. | 5 | 2024-08-07 15:32:48 | 7.8 | 90 |
  33. +----+---------------------+-------+-----+
  34. 5 rows in set (0.00 sec)

分组查询

  1. mysql> select gender,count(gender) from student group by gender;
  2. +--------+---------------+
  3. | gender | count(gender) |
  4. +--------+---------------+
  5. | 男 | 3 |
  6. | 女 | 1 |
  7. +--------+---------------+
  8. 2 rows in set (0.00 sec)
  9. mysql> select gender as 性别,count(gender) as 人数 from student group by gender;
  10. +--------+--------+
  11. | 性别 | 人数 |
  12. +--------+--------+
  13. | 男 | 3 |
  14. | 女 | 1 |
  15. +--------+--------+
  16. 2 rows in set (0.00 sec)

子句查询

  1. mysql> select sum(tt) from (select *,price*qty as tt from product) as a;
  2. +--------------------+
  3. | sum(tt) |
  4. +--------------------+
  5. | 14887.999668121338 |
  6. +--------------------+
  7. 1 row in set (0.00 sec)

向下取整

  1. mysql> select trim("a b");
  2. +-------------+
  3. | trim("a b") |
  4. +-------------+
  5. | a b |
  6. +-------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select floor(3.14);
  9. +-------------+
  10. | floor(3.14) |
  11. +-------------+
  12. | 3 |
  13. +-------------+
  14. 1 row in set (0.00 sec)
  15. mysql> select floor(-3.14);
  16. +--------------+
  17. | floor(-3.14) |
  18. +--------------+
  19. | -4 |
  20. +--------------+
  21. 1 row in set (0.00 sec)

向上取整

  1. mysql> select ceiling(9.8);
  2. +--------------+
  3. | ceiling(9.8) |
  4. +--------------+
  5. | 10 |
  6. +--------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select ceiling(9.3);
  9. +--------------+
  10. | ceiling(9.3) |
  11. +--------------+
  12. | 10 |
  13. +--------------+
  14. 1 row in set (0.00 sec)

四舍五入

  1. mysql> select round(9.3);
  2. +------------+
  3. | round(9.3) |
  4. +------------+
  5. | 9 |
  6. +------------+
  7. 1 row in set (0.00 sec)
  8. mysql> select round(9.5);
  9. +------------+
  10. | round(9.5) |
  11. +------------+
  12. | 10 |
  13. +------------+
  14. 1 row in set (0.00 sec)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/947130
推荐阅读
相关标签
  

闽ICP备14008679号