赞
踩
- #关闭防火墙
- [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
- #关闭防火墙
- [root@slave ~]# systemctl stop firewalld
-
- #关闭selinux
- [root@slave ~]# setenforce 0
-
- #安装lrzsz
- [root@slave ~]# yum -y install lrzsz
-
- #安装rsync
- [root@slave ~]# yum -y install rsync
- # 将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
- [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
[root@master ~]# cp -r mysql-8.0.33-linux-glibc2.12-x86_64 /usr/local/mysql
[root@slave ~]# cp -r mysql-8.0.33-linux-glibc2.12-x86_64 /usr/local/mysql
- [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
- [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)
- [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
- [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
[root@master mysql]# rm -rf /etc/my.cnf
[root@slave ~]# rm -rf /etc/my.cnf
- # 初始化 从数据库不能初始化
- [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
从数据库不能初始化
[root@master mysql]# cp support-files/mysql.server /etc/init.d/mysql8
[root@slave ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql8
- #修改配置文件
- [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
- #查看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
- #停止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
- #在从库中查看有没有将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
- [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
- [root@master ~]# service mysql8 start
- Starting MySQL.Logging to '/usr/local/mysql/data/db01-master.err'.
- . SUCCESS!
- [root@slave ~]# service mysql8 start
- Starting MySQL.Logging to '/usr/local/mysql/data/err.log'.
- . SUCCESS!
- #修改配置文件
- [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>
- #修改配置文件
- [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
- #创建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)
- #获得远程主机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就表示主从数据库配置成功了
- #在主数据库上创建表,插入数据
- #创建不了,是因为锁表
- 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)
- #在从数据库上查看有没有同步主的数据库
- 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语句
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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。