去MySQL :: Download MySQL Community Server (Archived Versions)下载安装包

检测发现没问题
[root@xk ~]# ls
anaconda-ks.cfg mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
[root@xk ~]# md5sum mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
79b971fc3e3368f2a1e07fbafae0b914 mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
[root@xk ~]#
创建mysql数据库用户和组
[root@xk ~]# useradd -r -M -s /sbin/nologin -u 306 mysql
[root@xk ~]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)
解压该文件到/usr/local/
[root@xk ~]# cd /usr/local/
[root@xk local]# ls
bin etc games include lib lib64 libexec mysql-5.7.39-linux-glibc2.12-x86_64 sbin share src
配置已安装好的MySQL
做一个软链接
[root@xk local]# ll /usr/local/
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 Dec 23 00:17 lib64
drwxr-xr-x. 2 root root 6 Jun 22 2021 libexec
drwxr-xr-x. 9 root root 129 Dec 30 07:17 mysql-5.7.39-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Jun 22 2021 sbin
drwxr-xr-x. 5 root root 49 Dec 23 00:17 share
drwxr-xr-x. 2 root root 6 Jun 22 2021 src
[root@xk local]# ln -s /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64/ /usr/local/mysql
[root@xk local]# ll /usr/local/
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 Dec 23 00:17 lib64
drwxr-xr-x. 2 root root 6 Jun 22 2021 libexec
lrwxrwxrwx. 1 root root 47 Dec 30 07:20 mysql -> /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Dec 30 07:17 mysql-5.7.39-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Jun 22 2021 sbin
drwxr-xr-x. 5 root root 49 Dec 23 00:17 share
drwxr-xr-x. 2 root root 6 Jun 22 2021 src
修改属主为mysql
[root@xk local]# chown -R mysql.mysql /usr/local/mysql*
[root@xk local]# ll /usr/local/
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 Dec 23 00:17 lib64
drwxr-xr-x. 2 root root 6 Jun 22 2021 libexec
lrwxrwxrwx. 1 mysql mysql 47 Dec 30 07:20 mysql -> /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 mysql mysql 129 Dec 30 07:17 mysql-5.7.39-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Jun 22 2021 sbin
drwxr-xr-x. 5 root root 49 Dec 23 00:17 share
drwxr-xr-x. 2 root root 6 Jun 22 2021 src
添加环境变量
[root@xk local]# ls /usr/local/mysql
bin docs include lib LICENSE man README share support-files
[root@xk local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysqld.sh
[root@xk local]# source /etc/profile.d/mysqld.sh
[root@xk local]# which mysql
/usr/local/mysql/bin/mysql
配置其里面文件
[root@xk local]# ls /usr/local/mysql
bin docs include lib LICENSE man README share support-files
[root@xk local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysqld.sh
[root@xk local]# source /etc/profile.d/mysqld.sh
[root@xk local]# which mysql
/usr/local/mysql/bin/mysql
[root@xk local]# ln -s /usr/local/mysql/include /usr/include/mysqld
[root@xk local]# vi /etc/ld.so.conf.d/mysqld.conf
[root@xk local]# ldconfig
[root@xk local]# vi /etc/man_db.conf
[root@xk local]# vi /etc/man_db.conf
建立数据存放目录
[root@xk local]# mkdir -p /opt/data
[root@xk local]# chown -R mysql.mysql /opt/data
[root@xk local]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Dec 30 07:35 data
初始化
[root@xk local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir /opt/data/
2022-12-29T23:39:20.023295Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-12-29T23:39:20.228580Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-12-29T23:39:20.261455Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-12-29T23:39:20.320136Z 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: 044a1d04-87d2-11ed-881a-000c295105b0.
2022-12-29T23:39:20.321618Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-12-29T23:39:20.680547Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-12-29T23:39:20.680571Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-12-29T23:39:20.680928Z 0 [Warning] CA certificate ca.pem is self signed.
2022-12-29T23:39:20.715397Z 1 [Note] A temporary password is generated for root@localhost: ld2#au./edrC
保存密码放置文件
[root@xk local]# echo 'ld2#au./edrC' > pass
[root@xk local]# cat pass
ld2#au./edrC
生成配置文件
[root@xk local]# cat /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
配置服务启动脚本
[root@xk support-files]# file mysql.server
mysql.server: POSIX shell script, ASCII text executable
[root@xk support-files]# dnf -y install vim
CentOS Linux 8 - AppStream 12 kB/s | 4.3 kB 00:00
CentOS Linux 8 - BaseOS 12 kB/s | 3.9 kB 00:00
CentOS Linux 8 - Extras 4.0 kB/s | 1.5 kB 00:00
Package vim-enhanced-2:8.0.1763-16.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@xk support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@xk support-files]# vim mysql.server
[root@xk support-files]# ll
total 24
-rw-r--r--. 1 mysql mysql 773 Jun 8 2022 magic
-rwxr-xr-x. 1 mysql mysql 1061 Jun 8 2022 mysqld_multi.server
-rwxr-xr-x. 1 mysql mysql 894 Jun 8 2022 mysql-log-rotate
-rwxr-xr-x. 1 mysql mysql 10601 Dec 30 07:50 mysql.server
[root@xk support-files]# cd
[root@xk ~]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.Logging to '/opt/data/xk.err'.
SUCCESS!
[root@xk ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:49493 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 64 0.0.0.0:40381 0.0.0.0:*
LISTEN 0 64 0.0.0.0:2049 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:20048 0.0.0.0:*
LISTEN 0 64 [::]:41587 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 64 [::]:2049 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:46511 [::]:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:20048 [::]:*
连接
[Unit]
Description=mysqld server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.serve start
ExecStop=/usr/local/mysql/support-files/mysql.serve stop
[Install]
WantedBy=multi-user.target
[root@xk local]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/mysqld.service
[root@xk local]# vim /usr/lib/systemd/system/mysqld.service
[root@xk local]# /usr/local/mysql/support-files/mysql.server stop
Shutting down MySQL.. SUCCESS!
[root@xk local]# vim /usr/lib/systemd/system/mysqld.service
[root@xk local]# systemctl daemon-reload
[root@xk local]# systemctl status mysqld
● mysqld.service - mysqld server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
Active: inactive (dead)
开机自启
[root@xk ~]# systemctl status mysqld
● mysqld.service - mysql server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; ve>
Active: active (running) since Thu 2022-12-29 19:16:55 CST; 3s ago
Process: 10974 ExecStart=/usr/local/mysql/support-files/mysql.server>
Main PID: 10987 (mysqld_safe)
Tasks: 28 (limit: 4766)
Memory: 173.6M
CGroup: /system.slice/mysqld.service
├─10987 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=>
└─11189 /usr/local/mysql/bin/mysqld --basedir=/usr/local/my>
Dec 29 08:04:20 xk systemd[1]: Starting mysql server daemon...
Dec 29 08:04:20 xk mysql.server[10974]: Starting MySQL. SUCCESS!
Dec 29 08:04:20 xk systemd[1]: Started mysql server daemon.
修改密码
<code class="language-plaintext hljs">[root@xk ~]# cat /usr/local/pass
ld2#au./edrC
[root@xk ~]# yum provides libncurses.so.5
[root@xk ~]# mysql -uroot -p'aDsy#XKrg1dT'
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.39
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('runtime123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
[root@xk ~]# mysql -uroot -p'runtime123!'
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.39 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> quit
Bye
多表查询
内连接
<code class="language-plaintext hljs">mysql> SELECT s.name,c.course_name FROM tb_students_info s INNER JOIN tb_course c ON s.course_id = c.id;
+--------+-------------+
| name | course_name |
+--------+-------------+
| Dany | Java |
| Green | MySQL |
| Henry | Java |
| Jane | Python |
| Jim | MySQL |
| John | Go |
| Lily | Go |
| Susan | C++ |
| Thomas | C++ |
| Tom | C++ |
+--------+-------------+
10 rows in set (0.00 sec)
mysql> </code>
外连接
<code class="language-plaintext hljs">mysql> SELECT s.name,c.course_name FROM tb_students_info s LEFT OUTER JOIN tb_course c ON s.`course_id`=c.`id`;
+--------+-------------+
| name | course_name |
+--------+-------------+
| Dany | Java |
| Green | MySQL |
| Henry | Java |
| Jane | Python |
| Jim | MySQL |
| John | GO |
| Lily | GO |
| Susan | C++ |
| Thomas | C++ |
| Tom | C++ |
| LiMing | NULL |
+--------+-------------+
11 rows in set (0.00 sec)
mysql> SELECT s.name,c.course_name FROM tb_students_info s RIGHT OUTER JOIN tb_course c ON s.`course_id`=c.`id`;
+--------+-------------+
| name | course_name |
+--------+-------------+
| Dany | Java |
| Green | MySQL |
| Henry | Java |
| Jane | Python |
| Jim | MySQL |
| John | GO |
| Lily | GO |
| Susan | C++ |
| Thomas | C++ |
| Tom | C++ |
| NULL | Html |
+--------+-------------+
11 rows in set (0.00 sec)
</code>
分组连接
<code class="language-plaintext hljs">mysql> SELECT name,sex FROM tb_students_info GROUP BY sex;
+-------+--------+
| name | sex |
+-------+--------+
| Henry | Female |
| Dany | Male |
+-------+--------+
2 rows in set (0.00 sec)
mysql> SELECT sex, GROUP_CONCAT(name) FROM tb_students_info GROUP BY sex;
+--------+-----------------------------------+
| sex | GROUP_CONCAT(name) |
+--------+-----------------------------------+
| Female | Henry,Jim,John,Thomas,Tom |
| Male | Dany,Green,Jane,Lily,Susan,LiMing |
+--------+-----------------------------------+
2 rows in set (0.00 sec)
mysql> SELECT age,sex,GROUP_CONCAT(name) FROM tb_students_info GROUP BY age,sex;
+------+--------+--------------------+
| age | sex | GROUP_CONCAT(name) |
+------+--------+--------------------+
| 21 | Female | John |
| 22 | Female | Thomas |
| 22 | Male | Jane,Lily,LiMing |
| 23 | Female | Henry,Tom |
| 23 | Male | Green,Susan |
| 24 | Female | Jim |
| 25 | Male | Dany |
+------+--------+--------------------+
7 rows in set (0.00 sec)
</code>
交叉连接
<code class="language-plaintext hljs">mysql> SELECT * FROM tb_course CROSS JOIN tb_students_info;
+----+-------------+----+--------+------+--------+--------+-----------+
| id | course_name | id | name | age | sex | height | course_id |
+----+-------------+----+--------+------+--------+--------+-----------+
| 1 | Java | 1 | Dany | 25 | Male | 160 | 1 |
| 2 | MySQL | 1 | Dany | 25 | Male | 160 | 1 |
| 3 | Python | 1 | Dany | 25 | Male | 160 | 1 |
| 4 | GO | 1 | Dany | 25 | Male | 160 | 1 |
| 5 | C++ | 1 | Dany | 25 | Male | 160 | 1 |
| 1 | Java | 2 | Green | 23 | Male | 158 | 2 |
| 2 | MySQL | 2 | Green | 23 | Male | 158 | 2 |
| 3 | Python | 2 | Green | 23 | Male | 158 | 2 |
| 4 | GO | 2 | Green | 23 | Male | 158 | 2 |
| 5 | C++ | 2 | Green | 23 | Male | 158 | 2 |
| 1 | Java | 3 | Henry | 23 | Female | 185 | 1 |
| 2 | MySQL | 3 | Henry | 23 | Female | 185 | 1 |
| 3 | Python | 3 | Henry | 23 | Female | 185 | 1 |
| 4 | GO | 3 | Henry | 23 | Female | 185 | 1 |
| 5 | C++ | 3 | Henry | 23 | Female | 185 | 1 |
| 1 | Java | 4 | Jane | 22 | Male | 162 | 3 |
| 2 | MySQL | 4 | Jane | 22 | Male | 162 | 3 |
| 3 | Python | 4 | Jane | 22 | Male | 162 | 3 |
| 4 | GO | 4 | Jane | 22 | Male | 162 | 3 |
| 5 | C++ | 4 | Jane | 22 | Male | 162 | 3 |
| 1 | Java | 5 | Jim | 24 | Female | 175 | 2 |
| 2 | MySQL | 5 | Jim | 24 | Female | 175 | 2 |
| 3 | Python | 5 | Jim | 24 | Female | 175 | 2 |
| 4 | GO | 5 | Jim | 24 | Female | 175 | 2 |
| 5 | C++ | 5 | Jim | 24 | Female | 175 | 2 |
| 1 | Java | 6 | John | 21 | Female | 172 | 4 |
| 2 | MySQL | 6 | John | 21 | Female | 172 | 4 |
| 3 | Python | 6 | John | 21 | Female | 172 | 4 |
| 4 | GO | 6 | John | 21 | Female | 172 | 4 |
| 5 | C++ | 6 | John | 21 | Female | 172 | 4 |
| 1 | Java | 7 | Lily | 22 | Male | 165 | 4 |
| 2 | MySQL | 7 | Lily | 22 | Male | 165 | 4 |
| 3 | Python | 7 | Lily | 22 | Male | 165 | 4 |
| 4 | GO | 7 | Lily | 22 | Male | 165 | 4 |
| 5 | C++ | 7 | Lily | 22 | Male | 165 | 4 |
| 1 | Java | 8 | Susan | 23 | Male | 170 | 5 |
| 2 | MySQL | 8 | Susan | 23 | Male | 170 | 5 |
| 3 | Python | 8 | Susan | 23 | Male | 170 | 5 |
| 4 | GO | 8 | Susan | 23 | Male | 170 | 5 |
| 5 | C++ | 8 | Susan | 23 | Male | 170 | 5 |
| 1 | Java | 9 | Thomas | 22 | Female | 178 | 5 |
| 2 | MySQL | 9 | Thomas | 22 | Female | 178 | 5 |
| 3 | Python | 9 | Thomas | 22 | Female | 178 | 5 |
| 4 | GO | 9 | Thomas | 22 | Female | 178 | 5 |
| 5 | C++ | 9 | Thomas | 22 | Female | 178 | 5 |
| 1 | Java | 10 | Tom | 23 | Female | 165 | 5 |
| 2 | MySQL | 10 | Tom | 23 | Female | 165 | 5 |
| 3 | Python | 10 | Tom | 23 | Female | 165 | 5 |
| 4 | GO | 10 | Tom | 23 | Female | 165 | 5 |
| 5 | C++ | 10 | Tom | 23 | Female | 165 | 5 |
| 1 | Java | 11 | LiMing | 22 | Male | 180 | 7 |
| 2 | MySQL | 11 | LiMing | 22 | Male | 180 | 7 |
| 3 | Python | 11 | LiMing | 22 | Male | 180 | 7 |
| 4 | GO | 11 | LiMing | 22 | Male | 180 | 7 |
| 5 | C++ | 11 | LiMing | 22 | Male | 180 | 7 |
+----+-------------+----+--------+------+--------+--------+-----------+
55 rows in set (0.00 sec)
mysql> SELECT * FROM tb_course CROSS JOIN tb_students_info WHERE tb_students_info.course_id = tb_course.id;
+----+-------------+----+--------+------+--------+--------+-----------+
| id | course_name | id | name | age | sex | height | course_id |
+----+-------------+----+--------+------+--------+--------+-----------+
| 1 | Java | 1 | Dany | 25 | Male | 160 | 1 |
| 2 | MySQL | 2 | Green | 23 | Male | 158 | 2 |
| 1 | Java | 3 | Henry | 23 | Female | 185 | 1 |
| 3 | Python | 4 | Jane | 22 | Male | 162 | 3 |
| 2 | MySQL | 5 | Jim | 24 | Female | 175 | 2 |
| 4 | GO | 6 | John | 21 | Female | 172 | 4 |
| 4 | GO | 7 | Lily | 22 | Male | 165 | 4 |
| 5 | C++ | 8 | Susan | 23 | Male | 170 | 5 |
| 5 | C++ | 9 | Thomas | 22 | Female | 178 | 5 |
| 5 | C++ | 10 | Tom | 23 | Female | 165 | 5 |
+----+-------------+----+--------+------+--------+--------+-----------+
10 rows in set (0.00 sec)
</code>