mysql二进制安装

本文详细介绍了如何在Linux环境下进行MySQL的二进制安装,包括创建用户、解压软件包、配置环境变量、设置数据目录、初始化数据库和配置文件。同时,讲解了多表查询的基础概念,特别是交叉查询(笛卡尔积)的应用,并通过实例展示了如何进行多表数据匹配。
摘要由CSDN通过智能技术生成

MySQL进阶


1.mysql二进制安装

//去mysql官网下载二进制的mysql软件包
[root@hwf ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz

//创建一个mysql用户
[root@hwf ~]# useradd -M -r -s /sbin/nologin mysql	

//解压二进制MySQL软件包
[root@hwf ~]# tar -xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@hwf ~]# ls /usr/local/
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.38-linux-glibc2.12-x86_64  share

//为MySQL软件包创建软连接
[root@hwf local]# ln -s /usr/local/mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
[root@hwf local]# ll
total 0
drwxr-xr-x. 2 root root   6 Jun 22  2021 bin
drwxr-xr-x. 2 root root   6 Jun 22  2021 etc
drwxr-xr-x. 2 root root   6 Jun 22  2021 games
drwxr-xr-x. 2 root root   6 Jun 22  2021 include
drwxr-xr-x. 2 root root   6 Jun 22  2021 lib
drwxr-xr-x. 3 root root  17 Jun 28 14:19 lib64
drwxr-xr-x. 2 root root   6 Jun 22  2021 libexec
lrwxrwxrwx  1 root root  47 Jul 27 13:02 mysql -> /usr/local/mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x  9 root root 129 Jul 27 13:02 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 Jun 22  2021 sbin
drwxr-xr-x. 5 root root  49 Jun 28 14:19 share
drwxr-xr-x. 2 root root   6 Jul 22 17:09 src

//修改/usr/local/mysql的属主属组
[root@hwf local]# chown -R mysql.mysql /usr/local/mysql
[root@hwf local]# ll
total 0
drwxr-xr-x. 2 root  root    6 Jun 22  2021 bin
drwxr-xr-x. 2 root  root    6 Jun 22  2021 etc
drwxr-xr-x. 2 root  root    6 Jun 22  2021 games
drwxr-xr-x. 2 root  root    6 Jun 22  2021 include
drwxr-xr-x. 2 root  root    6 Jun 22  2021 lib
drwxr-xr-x. 3 root  root   17 Jun 28 14:19 lib64
drwxr-xr-x. 2 root  root    6 Jun 22  2021 libexec
lrwxrwxrwx  1 mysql mysql  47 Jul 27 13:02 mysql -> /usr/local/mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x  9 root  root  129 Jul 27 13:02 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root  root    6 Jun 22  2021 sbin
drwxr-xr-x. 5 root  root   49 Jun 28 14:19 share
drwxr-xr-x. 2 root  root    6 Jul 22 17:09 src

//添加环境变量
[root@hwf bin]# echo 'export PATH=//usr/local/mysql/bin:$PATH' >/etc/profile.d/mysql.sh
[root@hwf bin]# source /etc/profile.d/mysql.sh
//映射头文件
[root@hwf mysql]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@hwf mysql]# ln -s /usr/local/mysql/include/ /usr/include/mysql
//生效MySQL的man
[root@hwf 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/apache/man
MANDATORY_MANPATH                       /usr/local/mysql/man

//创建mysql数据存放目录
[root@hwf mysql]# mkdir /opt/data
[root@hwf mysql]# chown -R mysql.mysql /opt/data
[root@hwf mysql]# ll /opt/
total 0
drwxr-xr-x 2 mysql mysql 6 Jul 27 13:28 data

//初始化数据库,最有一段的数字就是随机密码,每一次初始化的密码都是不一样的
[root@hwf mysql]# mysqld --initialize --user mysql --datadir=/opt/data
2022-07-27T05:30:06.174683Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-27T05:30:06.507037Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-27T05:30:06.532849Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-27T05:30:06.537924Z 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: 2c5f176b-0d6d-11ed-b4b9-000c291ef509.
2022-07-27T05:30:06.538323Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-27T05:30:06.760472Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-27T05:30:06.760496Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-27T05:30:06.760876Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-27T05:30:06.854244Z 1 [Note] A temporary password is generated for root@localhost: pgWsevgXF8+b

//生成启动配置文件
[root@hwf ~]# vim /etc/my.cnf 
[mysqld]
basedir = /usr/local/mysql			//MySQL存放路径
datadir = /opt/data					//MySQL数据存放位置
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@hwf ~]# cp /usr/lib/systemd/system/sshd.service .
[root@hwf ~]# ls
anaconda-ks.cfg  mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz  runtime
lv1              pubic                                       sshd.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@hwf ~]# mv sshd.service mysql.service		//改个名字
[root@hwf ~]# ls

anaconda-ks.cfg  mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz  pubic
lv1              mysql.service                               runtime
[root@hwf ~]# mv mysql.service /usr/lib/systemd/system/
[root@hwf ~]# systemctl daemon-reload 
[root@hwf ~]# systemctl restart mysqld
[root@hwf ~]# systemctl status mysqld
● mysqld.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/rc.d/init.d/mysqld; generated)
   Active: active (running) since Wed 2022-07-27 15:58:28 CST; 6s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 3836 ExecStop=/etc/rc.d/init.d/mysqld stop (code=exited, status=0/SUCCES>
  Process: 3856 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCC>
    Tasks: 28 (limit: 23502)
   Memory: 177.7M
   CGroup: /system.slice/mysqld.service
           ├─3861 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pi>
           └─3958 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=>

Jul 27 15:58:27 hwf systemd[1]: Starting LSB: start and stop MySQL...
Jul 27 15:58:28 hwf mysqld[3856]: Starting MySQL. SUCCESS!
Jul 27 15:58:28 hwf systemd[1]: Started LSB: start and stop MySQL

//修改登录的临时密码
[root@hwf ~]# mysql -uroot -p-6U-Ql8sbWde		//用随机的密码登录
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@hwf ~]# 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.

2.mysql配置文件

mysql的配置文件为/etc/my.cnf
配置文件查找次序:若在多个配置文件中均有设定,则最后找到的最终生效

/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf

mysql常用配置文件参数:

参数说明
端口 = 3306设置监听端口
socket = /tmp/mysql.sock指定MySQL的安装路径
basedir = /usr/local/mysql指定MySQL的安装路径
datadir = /data/mysql指定MySQL的数据存放路径
pid-file = /data/mysql/mysql.pid指定进程ID文件存放路径
user = mysql指定MySQL以什么用户的身份提供服务
跳过名称解析禁止MySQL对外部连接进行DNS解析
使用这一选项可以消除MySQL进行DNS解析的时间。若开启该选项,
则所有远程主机连接授权都要使用IP地址方式否则MySQL
将无法正常处理连接请求

3.多表联合查询

3.1什么是多表联合查询

多表查询就是同时查询两个或两个以上的表。
在 MySQL 中,多表查询主要有交叉连接、内连接、外连接、分组查询与子查询等5种

3.2交叉查询
3.2.1笛卡尔积

//交叉查询的语法

SELECT <字段名> FROM <1> CROSS JOIN <2> [WHERE子句];
SELECT <字段名> FROM <1>, <2> [WHERE子句];

(1)查询 tb_students_info 表中的数据,SQL 语句和运行结果如下:

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.01 sec)

mysql> insert into tb_students_info (name,age,sex,height,course_id) values('Dany',25,'M',160,1),('Green',23,'M',158,2),('Henry',23,'W',185,1),('Jane',22,'M',162,3),('Jim',24,'W',175,2),('John',21,'W',172,4),('Lily',22,'M',165,4),('Susan',23,'M',170,5),('Thomas',22,'W',178,5),('Tom',23,'W',165,5);
Query OK, 10 rows affected (0.01 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql> select * from tb_students_info;
+----+--------+------+------+--------+-----------+
| id | name   | age  | sex  | height | course_id |
+----+--------+------+------+--------+-----------+
|  1 | Dany   |   25 | M    |    160 |         1 |
|  2 | Green  |   23 | M    |    158 |         2 |
|  3 | Henry  |   23 | W    |    185 |         1 |
|  4 | Jane   |   22 | M    |    162 |         3 |
|  5 | Jim    |   24 | W    |    175 |         2 |
|  6 | John   |   21 | W    |    172 |         4 |
|  7 | Lily   |   22 | M    |    165 |         4 |
|  8 | Susan  |   23 | M    |    170 |         5 |
|  9 | Thomas |   22 | W    |    178 |         5 |
| 10 | Tom    |   23 | W    |    165 |         5 |
+----+--------+------+------+--------+-----------+
10 rows in set (0.00 sec)

(2)查询 tb_course 表中的数据,SQL 语句和运行结果如下:
mysql> create table tb_course(id int not null primary key auto_increment,course_name varchar(50));
Query OK, 0 rows affected (0.11 sec)

mysql> insert tb_course(course_name) values('java'),('mysql'),('python'),('go'),('c+++');
Query OK, 5 rows affected (0.00 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)

(3)使用 CROSS JOIN 查询出两张表中的笛卡尔积,SQL 语句和运行结果如下:
mysql> select * from tb_students_info cross join tb_course;
+----+--------+------+------+--------+-----------+----+-------------+
| id | name   | age  | sex  | height | course_id | id | course_name |
+----+--------+------+------+--------+-----------+----+-------------+
|  1 | Dany   |   25 | M    |    160 |         1 |  1 | java        |
|  1 | Dany   |   25 | M    |    160 |         1 |  2 | mysql       |
|  1 | Dany   |   25 | M    |    160 |         1 |  3 | python      |
|  1 | Dany   |   25 | M    |    160 |         1 |  4 | go          |
|  1 | Dany   |   25 | M    |    160 |         1 |  5 | c++         |
|  2 | Green  |   23 | M    |    158 |         2 |  1 | java        |
|  2 | Green  |   23 | M    |    158 |         2 |  2 | mysql       |
|  2 | Green  |   23 | M    |    158 |         2 |  3 | python      |
|  2 | Green  |   23 | M    |    158 |         2 |  4 | go          |
|  2 | Green  |   23 | M    |    158 |         2 |  5 | c++         |
|  3 | Henry  |   23 | W    |    185 |         1 |  1 | java        |
|  3 | Henry  |   23 | W    |    185 |         1 |  2 | mysql       |
|  3 | Henry  |   23 | W    |    185 |         1 |  3 | python      |
|  3 | Henry  |   23 | W    |    185 |         1 |  4 | go          |
|  3 | Henry  |   23 | W    |    185 |         1 |  5 | c++         |
|  4 | Jane   |   22 | M    |    162 |         3 |  1 | java        |
|  4 | Jane   |   22 | M    |    162 |         3 |  2 | mysql       |
|  4 | Jane   |   22 | M    |    162 |         3 |  3 | python      |
|  4 | Jane   |   22 | M    |    162 |         3 |  4 | go          |
|  4 | Jane   |   22 | M    |    162 |         3 |  5 | c++         |
|  5 | Jim    |   24 | W    |    175 |         2 |  1 | java        |
|  5 | Jim    |   24 | W    |    175 |         2 |  2 | mysql       |
|  5 | Jim    |   24 | W    |    175 |         2 |  3 | python      |
|  5 | Jim    |   24 | W    |    175 |         2 |  4 | go          |
|  5 | Jim    |   24 | W    |    175 |         2 |  5 | c++         |
|  6 | John   |   21 | W    |    172 |         4 |  1 | java        |
|  6 | John   |   21 | W    |    172 |         4 |  2 | mysql       |
|  6 | John   |   21 | W    |    172 |         4 |  3 | python      |
|  6 | John   |   21 | W    |    172 |         4 |  4 | go          |
|  6 | John   |   21 | W    |    172 |         4 |  5 | c++         |
|  7 | Lily   |   22 | M    |    165 |         4 |  1 | java        |
|  7 | Lily   |   22 | M    |    165 |         4 |  2 | mysql       |
|  7 | Lily   |   22 | M    |    165 |         4 |  3 | python      |
|  7 | Lily   |   22 | M    |    165 |         4 |  4 | go          |
|  7 | Lily   |   22 | M    |    165 |         4 |  5 | c++         |
|  8 | Susan  |   23 | M    |    170 |         5 |  1 | java        |
|  8 | Susan  |   23 | M    |    170 |         5 |  2 | mysql       |
|  8 | Susan  |   23 | M    |    170 |         5 |  3 | python      |
|  8 | Susan  |   23 | M    |    170 |         5 |  4 | go          |
|  8 | Susan  |   23 | M    |    170 |         5 |  5 | c++         |
|  9 | Thomas |   22 | W    |    178 |         5 |  1 | java        |
|  9 | Thomas |   22 | W    |    178 |         5 |  2 | mysql       |
|  9 | Thomas |   22 | W    |    178 |         5 |  3 | python      |
|  9 | Thomas |   22 | W    |    178 |         5 |  4 | go          |
|  9 | Thomas |   22 | W    |    178 |         5 |  5 | c++         |
| 10 | Tom    |   23 | W    |    165 |         5 |  1 | java        |
| 10 | Tom    |   23 | W    |    165 |         5 |  2 | mysql       |
| 10 | Tom    |   23 | W    |    165 |         5 |  3 | python      |
| 10 | Tom    |   23 | W    |    165 |         5 |  4 | go          |
| 10 | Tom    |   23 | W    |    165 |         5 |  5 | c++         |
+----+--------+------+------+--------+-----------+----+-------------+
50 rows in set (0.00 sec)

查询 tb_course 表中的 id 字段和 tb_students_info 表中的 course_id 字段相等的内容, SQL 语句和运行结果如下:
mysql> select * from tb_course cross join tb_students_info where tb_course.id = tb_sttudents_info.course_id;
+----+-------------+----+--------+------+------+--------+-----------+
| id | course_name | id | name   | age  | sex  | height | course_id |
+----+-------------+----+--------+------+------+--------+-----------+
|  1 | java        |  1 | Dany   |   25 | M    |    160 |         1 |
|  2 | mysql       |  2 | Green  |   23 | M    |    158 |         2 |
|  1 | java        |  3 | Henry  |   23 | W    |    185 |         1 |
|  3 | python      |  4 | Jane   |   22 | M    |    162 |         3 |
|  2 | mysql       |  5 | Jim    |   24 | W    |    175 |         2 |
|  4 | go          |  6 | John   |   21 | W    |    172 |         4 |
|  4 | go          |  7 | Lily   |   22 | M    |    165 |         4 |
|  5 | c++         |  8 | Susan  |   23 | M    |    170 |         5 |
|  5 | c++         |  9 | Thomas |   22 | W    |    178 |         5 |
|  5 | c++         | 10 | Tom    |   23 | W    |    165 |         5 |
+----+-------------+----+--------+------+------+--------+-----------+
10 rows in set (0.00 sec)

//查询两表学习java的人
mysql> select tb_students_info.name,tb_course.course_name from tb_students_info cross join tb_course where tb_students_info.course_id = tb_course.id and tb_course.course_name = 'java';
+-------+-------------+
| name  | course_name |
+-------+-------------+
| Dany  | java        |
| Henry | java        |
+-------+-------------+
2 rows in set (0.00 sec)

//查看学习java的年龄
mysql> select tb_students_info.name,tb_course.course_name,tb_students_info.age fromtb_students_info cross join tb_course where tb_students_info.course_id = tb_course.idd and tb_course.course_name = 'java';
+-------+-------------+------+
| name  | course_name | age  |
+-------+-------------+------+
| Dany  | java        |   25 |
| Henry | java        |   23 |
+-------+-------------+------+
2 rows in set (0.00 sec)

tb_students_info cross join tb_course where tb_students_info.course_id = tb_course.idd and tb_course.course_name = 'java';
+-------+-------------+------+
| name  | course_name | age  |
+-------+-------------+------+
| Dany  | java        |   25 |
| Henry | java        |   23 |
+-------+-------------+------+
2 rows in set (0.00 sec)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1we11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值