mysql进阶
目录
1. 二进制格式mysql安装
- 1.下载二进制格式的mysql软件包
[root@mr ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@mr ~]# cd /usr/src/
[root@mr src]# ls
debug kernels mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@mr src]#
- 2.创建用户和组,解压软件至/usr/local/,修改目录/usr/local/mysql的属主属组
[root@mr src]# id mysql
id: ‘mysql’: no such user
[root@mr src]# useradd -M -r -s /sbin/nologin mysql
[root@mr src]# id mysql
uid=993(mysql) gid=990(mysql) groups=990(mysql)
[root@mr src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@mr src]# cd /usr/local/
[root@mr local]# ll
total 0
drwxr-xr-x. 2 root root 18 Jul 7 10:28 bin
drwxr-xr-x. 2 root root 6 May 19 2020 etc
drwxr-xr-x. 2 root root 6 May 19 2020 games
drwxr-xr-x. 2 root root 6 May 19 2020 include
drwxr-xr-x. 2 root root 6 May 19 2020 lib
drwxr-xr-x. 3 root root 17 Jul 1 09:26 lib64
drwxr-xr-x. 2 root root 6 May 19 2020 libexec
drwxr-xr-x. 9 root root 129 Jul 27 16:03 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 May 19 2020 sbin
drwxr-xr-x. 6 root root 64 Jul 7 10:28 share
drwxr-xr-x. 2 root root 6 May 19 2020 src
[root@mr local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64'
[root@mr local]# ll
total 0
drwxr-xr-x. 2 root root 18 Jul 7 10:28 bin
drwxr-xr-x. 2 root root 6 May 19 2020 etc
drwxr-xr-x. 2 root root 6 May 19 2020 games
drwxr-xr-x. 2 root root 6 May 19 2020 include
drwxr-xr-x. 2 root root 6 May 19 2020 lib
drwxr-xr-x. 3 root root 17 Jul 1 09:26 lib64
drwxr-xr-x. 2 root root 6 May 19 2020 libexec
lrwxrwxrwx. 1 root root 35 Jul 27 16:04 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 9 root root 129 Jul 27 16:03 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 May 19 2020 sbin
drwxr-xr-x. 6 root root 64 Jul 7 10:28 share
drwxr-xr-x. 2 root root 6 May 19 2020 src
[root@mr local]#
[root@mr local]# chown -R mysql.mysql mysql*
[root@mr local]# ll
total 0
drwxr-xr-x. 2 root root 18 Jul 7 10:28 bin
drwxr-xr-x. 2 root root 6 May 19 2020 etc
drwxr-xr-x. 2 root root 6 May 19 2020 games
drwxr-xr-x. 2 root root 6 May 19 2020 include
drwxr-xr-x. 2 root root 6 May 19 2020 lib
drwxr-xr-x. 3 root root 17 Jul 1 09:26 lib64
drwxr-xr-x. 2 root root 6 May 19 2020 libexec
lrwxrwxrwx. 1 mysql mysql 35 Jul 27 16:04 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 9 mysql mysql 129 Jul 27 16:03 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 May 19 2020 sbin
drwxr-xr-x. 6 root root 64 Jul 7 10:28 share
drwxr-xr-x. 2 root root 6 May 19 2020 src
[root@mr local]#
-
- 添加环境变量
[root@mr ~]# cd /usr/local/mysql
[root@mr mysql]# ls
bin docs include lib LICENSE man README share support-files
[root@mr mysql]# cd bin/
[root@mr bin]# pwd
/usr/local/mysql/bin
[root@mr bin]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
[root@mr bin]# source /etc/profile.d/mysql.sh
[root@mr bin]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin
[root@mr bin]# which mysql
/usr/local/mysql/bin/mysql
[root@mr bin]#
[root@mr mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
'/usr/include/mysql' -> '/usr/local/mysql/include/'
[root@mr mysql]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib/
[root@mr mysql]# ldconfig
[root@mr mysql]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
MANDATORY_MANPATH /usr/local/mysql/man
-
- 建立数据存放目录,初始化数据库
[root@mr ~]# mkdir /opt/data
[root@mr ~]# ll /opt/data/
total 0
[root@mr ~]# chown -R mysql.mysql /opt/data/
[root@mr ~]# mysql --initialize --user mysql --datadir /opt/data/
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
[root@mr ~]# mysqld --initialize --user mysql --datadir /opt/data/
2022-07-27T08:21:49.390169Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-27T08:21:49.520539Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-27T08:21:49.548430Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-27T08:21:49.557890Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 29732c94-0d85-11ed-925a-000c29e8e4aa.
2022-07-27T08:21:49.558494Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-27T08:21:50.271098Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-27T08:21:50.271141Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-27T08:21:50.271798Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-27T08:21:50.425296Z 1 [Note] A temporary password is generated for root@localhost: -ff3uq#Fee%k
[root@mr ~]#
[root@mr ~]# echo '-ff3uq#Fee%k' > pass
[root@mr ~]# cat pass
-ff3uq#Fee%k
-
- 生成配置文件
[root@mr ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
-
- 配置服务启动脚本,启动mysql
[root@mr mysql]# cd support-files/
[root@mr support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@mr support-files]# file mysql.server
mysql.server: POSIX shell script, ASCII text executable
[root@mr support-files]#
方法一:
[root@mr ~]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.Logging to '/opt/data/mr.err'.
SUCCESS!
[root@mr ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 25 0.0.0.0:514 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 25 [::]:514 [::]:*
[root@mr ~]# /usr/local/mysql/support-files/mysql.server stop
Shutting down MySQL.. SUCCESS!
[root@mr ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 25 0.0.0.0:514 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 25 [::]:514 [::]:*
方法二:
[root@mr ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@mr ~]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql/
datadir=/opt/data/
[root@mr ~]# service mysqld start
Starting MySQL. SUCCESS!
[root@mr ~]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[root@mr ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 25 0.0.0.0:514 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 25 [::]:514 [::]:*
[root@mr ~]# rm -rf /etc/init.d/mysqld
[root@mr ~]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
Failed to start mysqld.service: Unit mysqld.service not found.
[root@mr ~]# service mysqld stop
Redirecting to /bin/systemctl stop mysqld.service
Failed to stop mysqld.service: Unit mysqld.service not loaded.
[root@mr ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 25 0.0.0.0:514 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 25 [::]:514 [::]:*
[root@mr ~]#
[root@mr ~]# cp /usr/lib/systemd/system/sshd.service .
[root@mr ~]# ls
sshd.service
[root@mr ~]# mv sshd.service mysqld.service
[root@mr ~]# ls
mysqld.service
[root@mr ~]#vim mysqld.service
[Unit]
Description=mysql server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@mr ~]# systemctl daemon-reload
[root@mr ~]# systemctl stop firewalld
[root@mr ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@mr ~]# vim /etc/selinux/config
SELINUX=disabled
[root@mr ~]# setenforce 0
[root@mr ~]# systemctl start mysqld
[root@mr ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 25 0.0.0.0:514 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 25 [::]:514 [::]:*
[root@mr ~]#
- 7.修改密码,使用临时密码登录
[root@mr ~]# mysql -uroot -p'-ff3uq#Fee%k'
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
[root@mr ~]# yum whatprovides libncurses.so.5
Last metadata expiration check: 0:34:58 ago on Wed 27 Jul 2022 05:02:10 PM CST.
ncurses-compat-libs-6.1-7.20180224.el8.i686 : Ncurses compatibility libraries
Repo : baseos
Matched from:
Provide : libncurses.so.5
ncurses-compat-libs-6.1-9.20180224.el8.i686 : Ncurses compatibility libraries
Repo : baseos
Matched from:
Provide : libncurses.so.5
[root@mr ~]# dnf install -y ncurses-compat-libs
Last metadata expiration check: 0:35:39 ago on Wed 27 Jul 2022 05:02:10 PM CST.
Dependencies resolved.
===================================================================================
Package Arch Version Repository Size
===================================================================================
Installing:
ncurses-compat-libs x86_64 6.1-9.20180224.el8 baseos 328 k
Transaction Summary
===================================================================================
Install 1 Package
Total download size: 328 k
Installed size: 1.0 M
Downloading Packages:
ncurses-compat-libs-6.1-9.20180224.el8.x86_64.rpm 735 kB/s | 328 kB 00:00
-----------------------------------------------------------------------------------
Total 378 kB/s | 328 kB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : ncurses-compat-libs-6.1-9.20180224.el8.x86_64 1/1
Running scriptlet: ncurses-compat-libs-6.1-9.20180224.el8.x86_64 1/1
Verifying : ncurses-compat-libs-6.1-9.20180224.el8.x86_64 1/1
Installed products updated.
Installed:
ncurses-compat-libs-6.1-9.20180224.el8.x86_64
Complete!
[root@mr ~]# mysql -uroot -p'-ff3uq#Fee%k'
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 2
Server version: 5.7.38
Copyright (c) 2000, 2022, 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> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye
[root@mr ~]# mysql -uroot -p123456
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 3
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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. mysql配置文件
mysql的配置文件为/etc/my.cnf
配置文件查找次序:若在多个配置文件中均有设定,则最后找到的最终生效
[root@mr ~]# vim .my.cnf
[client]
user=root
password=123456
[root@mr ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 4
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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>
mysql常用配置文件参数
参数 | 说明 |
---|---|
port = 3306 | 设置监听端口 |
socket = /tmp/mysql.sock | 指定套接字文件位置 |
basedir = /usr/local/mysql | 指定MySQL的安装路径 |
datadir = /data/mysql | 指定MySQL的数据存放路径 |
pid-file = /data/mysql/mysql.pid | 指定进程ID文件存放路径 |
user = mysql | 指定MySQL以什么用户的身份提供服务 |
skip-name-resolve | 禁止MySQL对外部连接进行DNS解析,使用这一选项可以消除MySQL进行DNS解析的时间。若开启该选项,则所有远程主机连接授权都要使用IP地址方式否则MySQL将无法正常处理连接请求 |
[root@mr ~]# ps -ef | grep mysqld
root 11805 1 0 17:33 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 12007 11805 0 17:33 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=mr.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 12067 8399 0 17:53 pts/0 00:00:00 grep --color=auto mysqld
[root@mr ~]#
2.1 数据库密码破解:
[root@mr ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 6
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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> exit
Bye
[root@mr ~]# mysql -uroot -p123456
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 7
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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> exit
Bye
[root@mr ~]# rm -rf .my.cnf
[root@mr ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@mr ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
skip-grant-tables
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[root@mr ~]# systemctl restart mysqld
[root@mr ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 2
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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>
mysql> set password = password('marui');
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> use mysql;
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> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.01 sec)
mysql> desc user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Reload_priv | enum('N','Y') | NO | | N | |
| Shutdown_priv | enum('N','Y') | NO | | N | |
| Process_priv | enum('N','Y') | NO | | N | |
| File_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Show_db_priv | enum('N','Y') | NO | | N | |
| Super_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Repl_slave_priv | enum('N','Y') | NO | | N | |
| Repl_client_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Create_user_priv | enum('N','Y') | NO | | N | |
| Event_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
| Create_tablespace_priv | enum('N','Y') | NO | | N | |
| ssl_type | enum('','ANY','X509','SPECIFIED') | NO | | | |
| ssl_cipher | blob | NO | | NULL | |
| x509_issuer | blob | NO | | NULL | |
| x509_subject | blob | NO | | NULL | |
| max_questions | int(11) unsigned | NO | | 0 | |
| max_updates | int(11) unsigned | NO | | 0 | |
| max_connections | int(11) unsigned | NO | | 0 | |
| max_user_connections | int(11) unsigned | NO | | 0 | |
| plugin | char(64) | NO | | mysql_native_password | |
| authentication_string | text | YES | | NULL | |
| password_expired | enum('N','Y') | NO | | N | |
| password_last_changed | timestamp | YES | | NULL | |
| password_lifetime | smallint(5) unsigned | YES | | NULL | |
| account_locked | enum('N','Y') | NO | | N | |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
45 rows in set (0.00 sec)
mysql>
mysql> select * from user/G
*************************** 1. row ***************************
Host: localhost
User: root
Select_priv: Y
........
Alter_priv: Y
Show_db_priv: Y
Super_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Execute_priv: Y
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Create_user_priv: Y
Event_priv: Y
Trigger_priv: Y
Create_tablespace_priv: Y
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: mysql_native_password
authentication_string: *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9
password_expired: N
password_last_changed: 2022-07-27 17:39:05
password_lifetime: NULL
account_locked: N
*************************** 2. row ***************************
Host: localhost
......
mysql> update user set authentication_string =password('marui') where User = 'root' and Host = 'localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> select * from user/G
*************************** 1. row ***************************
Host: localhost
User: root
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
......
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: mysql_native_password
authentication_string: *9370E4584BA357054A156F3EF8CBCE33FBAB4BF3
password_expired: N
password_last_changed: 2022-07-27 17:39:05
password_lifetime: NULL
......
mysql> exit
Bye
[root@mr ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[root@mr ~]# systemctl restart mysqld
[root@mr ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@mr ~]# mysql -uroot -pmarui
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 3
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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>
3. 多表联合查询
3.1 什么是多表联合查询
前面所讲的查询语句都是针对一个表的,但是在关系型数据库中,表与表之间是有联系的,所以在实际应用中,经常使用多表查询。多表查询就是同时查询两个或两个以上的表。
在 MySQL 中,多表查询主要有交叉连接、内连接、外连接、分组查询与子查询等5种。
3.2 交叉连接(CROSS JOIN)
3.2.1 笛卡尔积
交叉连接(CROSS JOIN):有两种,显式的和隐式的2种,一般用来返回连接表的笛卡尔积。
笛卡尔积(Cartesian product)是指两个集合 X 和 Y 的乘积。
多表查询遵循的算法就是以上提到的笛卡尔积,表与表之间的连接可以看成是在做乘法运算。在实际应用中,应避免使用笛卡尔积,因为笛卡尔积中容易存在大量的不合理数据,简单来说就是容易导致查询结果重复、混乱。
3.2.2 交叉连接
交叉连接的语法格式如下:
SELECT <字段名> FROM <表1> CROSS JOIN <表2> [WHERE子句];
或
SELECT <字段名> FROM <表1>, <表2> [WHERE子句];
语法说明如下:
- 字段名:需要查询的字段名称。
- <表1><表2>:需要交叉连接的表名。
- WHERE 子句:用来设置交叉连接的查询条件。
注意:多个表交叉连接时,在 FROM 后连续使用 CROSS JOIN 或,即可。以上两种语法的返回结果是相同的,但是第一种语法才是官方建议的标准写法。
当连接的表之间没有关系时,我们会省略掉 WHERE 子句,这时返回结果就是两个表的笛卡尔积,返回结果数量就是两个表的数据行相乘。需要注意的是,如果每个表有 1000 行,那么返回结果的数量就有 1000×1000 = 1000000 行,数据量是非常巨大的。
交叉连接可以查询两个或两个以上的表,为了更好的理解,我们就讲解两个表的交叉连接查询。
例 1
查询学生信息表和科目信息表,并得到一个笛卡尔积。
为了方便观察学生信息表和科目表交叉连接后的运行结果,我们先分别查询出这两个表的数据,再进行交叉连接查询。
1)查询 tb_students_info 表中的数据,SQL 语句和运行结果如下:
mysql> create database runtime;
Query OK, 1 row affected (0.00 sec)
mysql> use runtime;
Database changed
mysql> create table tb_students_info(id int not null primary key auto_increment,name varchar(50),age tinyint,sex varchar(4),height float,course_id int);
Query OK, 0 rows affected (0.00 sec)
mysql> desc tb_students_info;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| sex | varchar(4) | YES | | NULL | |
| height | float | YES | | NULL | |
| course_id | int(11) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
mysql> insert tb_students_info (name,age,sex,height,course_id) values('Dany',25,'W',160,1),('Green',23,'W',158,2),('Henry',23,'M',185,1),('Jane',22,'W',62,3),('Jim',24,'M',175,2),('John',21,'M',172,4),('Lily',22,'W',165,4),('Susan',23,'W',170,5),('Thomas',22,'M',178,5),('Tom',23,'M',165,5);
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql> select * from tb_students_info;
+----+--------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+--------+------+------+--------+-----------+
| 1 | Dany | 25 | W | 160 | 1 |
| 2 | Green | 23 | W | 158 | 2 |
| 3 | Henry | 23 | M | 185 | 1 |
| 4 | Jane | 22 | W | 62 | 3 |
| 5 | Jim | 24 | M | 175 | 2 |
| 6 | John | 21 | M | 172 | 4 |
| 7 | Lily | 22 | W | 165 | 4 |
| 8 | Susan | 23 | W | 170 | 5 |
| 9 | Thomas | 22 | M | 178 | 5 |
| 10 | Tom | 23 | M | 165 | 5 |
+----+--------+------+------+--------+-----------+
10 rows in set (0.00 sec)
mysql> show tables;
+-------------------+
| Tables_in_runtime |
+-------------------+
| tb_students_info |
+-------------------+
1 row in set (0.00 sec)
mysql> create table tb_course(id int not null primary key auto_increment,course_name varchar(50));
Query OK, 0 rows affected (0.01 sec)
mysql> desc tb_course;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| course_name | varchar(50) | YES | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> insert tb_course(course_name) values('Java'),('Mysql'),('Python'),('Go'),('C++');
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from tb_course;
+----+-------------+
| id | course_name |
+----+-------------+
| 1 | Java |
| 2 | Mysql |
| 3 | Python |
| 4 | Go |
| 5 | C++ |
+----+-------------+
5 rows in set (0.00 sec)
mysql> mysql> show tables;
+-------------------+
| Tables_in_runtime |
+-------------------+
| tb_course |
| tb_students_info |
+-------------------+
2 rows in set (0.00 sec)
mysql> select * from tb_students_info cross join tb_course;
+----+--------+------+------+--------+-----------+----+-------------+
| id | name | age | sex | height | course_id | id | course_name |
+----+--------+------+------+--------+-----------+----+-------------+
| 1 | Dany | 25 | W | 160 | 1 | 1 | Java |
| 1 | Dany | 25 | W | 160 | 1 | 2 | Mysql |
| 1 | Dany | 25 | W | 160 | 1 | 3 | Python |
| 1 | Dany | 25 | W | 160 | 1 | 4 | Go |
| 1 | Dany | 25 | W | 160 | 1 | 5 | C++ |
| 2 | Green | 23 | W | 158 | 2 | 1 | Java |
| 2 | Green | 23 | W | 158 | 2 | 2 | Mysql |
| 2 | Green | 23 | W | 158 | 2 | 3 | Python |
| 2 | Green | 23 | W | 158 | 2 | 4 | Go |
| 2 | Green | 23 | W | 158 | 2 | 5 | C++ |
| 3 | Henry | 23 | M | 185 | 1 | 1 | Java |
| 3 | Henry | 23 | M | 185 | 1 | 2 | Mysql |
| 3 | Henry | 23 | M | 185 | 1 | 3 | Python |
| 3 | Henry | 23 | M | 185 | 1 | 4 | Go |
| 3 | Henry | 23 | M | 185 | 1 | 5 | C++ |
| 4 | Jane | 22 | W | 62 | 3 | 1 | Java |
| 4 | Jane | 22 | W | 62 | 3 | 2 | Mysql |
| 4 | Jane | 22 | W | 62 | 3 | 3 | Python |
| 4 | Jane | 22 | W | 62 | 3 | 4 | Go |
| 4 | Jane | 22 | W | 62 | 3 | 5 | C++ |
| 5 | Jim | 24 | M | 175 | 2 | 1 | Java |
| 5 | Jim | 24 | M | 175 | 2 | 2 | Mysql |
| 5 | Jim | 24 | M | 175 | 2 | 3 | Python |
| 5 | Jim | 24 | M | 175 | 2 | 4 | Go |
| 5 | Jim | 24 | M | 175 | 2 | 5 | C++ |
| 6 | John | 21 | M | 172 | 4 | 1 | Java |
| 6 | John | 21 | M | 172 | 4 | 2 | Mysql |
| 6 | John | 21 | M | 172 | 4 | 3 | Python |
| 6 | John | 21 | M | 172 | 4 | 4 | Go |
| 6 | John | 21 | M | 172 | 4 | 5 | C++ |
| 7 | Lily | 22 | W | 165 | 4 | 1 | Java |
| 7 | Lily | 22 | W | 165 | 4 | 2 | Mysql |
| 7 | Lily | 22 | W | 165 | 4 | 3 | Python |
| 7 | Lily | 22 | W | 165 | 4 | 4 | Go |
| 7 | Lily | 22 | W | 165 | 4 | 5 | C++ |
| 8 | Susan | 23 | W | 170 | 5 | 1 | Java |
| 8 | Susan | 23 | W | 170 | 5 | 2 | Mysql |
| 8 | Susan | 23 | W | 170 | 5 | 3 | Python |
| 8 | Susan | 23 | W | 170 | 5 | 4 | Go |
| 8 | Susan | 23 | W | 170 | 5 | 5 | C++ |
| 9 | Thomas | 22 | M | 178 | 5 | 1 | Java |
| 9 | Thomas | 22 | M | 178 | 5 | 2 | Mysql |
| 9 | Thomas | 22 | M | 178 | 5 | 3 | Python |
| 9 | Thomas | 22 | M | 178 | 5 | 4 | Go |
| 9 | Thomas | 22 | M | 178 | 5 | 5 | C++ |
| 10 | Tom | 23 | M | 165 | 5 | 1 | Java |
| 10 | Tom | 23 | M | 165 | 5 | 2 | Mysql |
| 10 | Tom | 23 | M | 165 | 5 | 3 | Python |
| 10 | Tom | 23 | M | 165 | 5 | 4 | Go |
| 10 | Tom | 23 | M | 165 | 5 | 5 | C++ |
+----+--------+------+------+--------+-----------+----+-------------+
50 rows in set (0.00 sec)
mysql> select * from tb_course cross join tb_students_info where tb_course.id = tb_students_info.course_id;
+----+-------------+----+--------+------+------+--------+-----------+
| id | course_name | id | name | age | sex | height | course_id |
+----+-------------+----+--------+------+------+--------+-----------+
| 1 | Java | 1 | Dany | 25 | W | 160 | 1 |
| 2 | Mysql | 2 | Green | 23 | W | 158 | 2 |
| 1 | Java | 3 | Henry | 23 | M | 185 | 1 |
| 3 | Python | 4 | Jane | 22 | W | 62 | 3 |
| 2 | Mysql | 5 | Jim | 24 | M | 175 | 2 |
| 4 | Go | 6 | John | 21 | M | 172 | 4 |
| 4 | Go | 7 | Lily | 22 | W | 165 | 4 |
| 5 | C++ | 8 | Susan | 23 | W | 170 | 5 |
| 5 | C++ | 9 | Thomas | 22 | M | 178 | 5 |
| 5 | C++ | 10 | Tom | 23 | M | 165 | 5 |
+----+-------------+----+--------+------+------+--------+-----------+
10 rows in set (0.00 sec)
mysql> select tb_course.course_name,tb_students_info.age from tb_course cross jointb_students_info where tb_course.id = tb_students_info.course_id;
+-------------+------+
| course_name | age |
+-------------+------+
| Java | 25 |
| Mysql | 23 |
| Java | 23 |
| Python | 22 |
| Mysql | 24 |
| Go | 21 |
| Go | 22 |
| C++ | 23 |
| C++ | 22 |
| C++ | 23 |
+-------------+------+
10 rows in set (0.00 sec)
mysql>
原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/tech/database/277448.html