MySQL5.7三种单实例部署方式

1、MySQL单实例部署

MySQL官方网址:https://www.mysql.com/

MySQL有多种安装方式:Yum存储库安装、二进制安装、RPM安装包安装。在开始安装之前、我们先来进行基础系统初始化。

1.1、系统初始化

# 配置主机名称
hostnamectl --static --transient  set-hostname mysql-01

# 安装依赖包
yum install -y epel-release
yum install -y chrony conntrack ipvsadm ipset jq iptables curl sysstat libseccomp wget socat git

# 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat && iptables -P FORWARD ACCEPT

# 关闭swap分区
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

# 关闭SELinux
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

# 关闭无关服务
systemctl stop postfix && systemctl disable postfix

默认的 CentOS7 存储库是没有MySQL数据库的、我们需要把MySQL存储库添加到CentOS7中。

# 默认的CentOS7存储库
[root@mysql-01 ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * epel: mirrors.ustc.edu.cn
 * extras: mirrors.163.com
 * updates: mirrors.163.com
repo id           repo name                                            status
base/7/x86_64     CentOS-7 - Base                                      10,072
epel/x86_64       Extra Packages for Enterprise Linux 7 - x86_64       13,550
extras/7/x86_64   CentOS-7 - Extras                                    453
updates/7/x86_64  CentOS-7 - Updates                                   1,729
repolist: 25,804

1.2、Yum Repository安装MySQL(RPM)

Yum存储库链接:https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

我们去MySQL官方网站把Yum存储库文件下载到服务器上。最新的Yum存储库可以安装MySQL所有的软件版本。

image-20210308100057846

image-20210308100131931

image-20210308100158902

image-20210308100236606

image-20210308100303287

# 下载MySQL存储库
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
[root@mysql-01 ~]# wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
--2021-03-08 09:47:21--  https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.50.239.110
Connecting to repo.mysql.com (repo.mysql.com)|23.50.239.110|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 26024 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql80-community-release-el7-3.noarch.rpm’

100%[=========================================================================>] 26,024      --.-K/s   in 0.09s   

2021-03-08 09:47:22 (293 KB/s) - ‘mysql80-community-release-el7-3.noarch.rpm’ saved [26024/26024]

# 安装MySQL存储库
[root@mysql-01 ~]# rpm -Uvh mysql80-community-release-el7-3.noarch.rpm 
warning: mysql80-community-release-el7-3.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql80-community-release-el7-3  ################################# [100%]
[root@mysql-01 ~]# 

# 更新CentOS7存储库
[root@mysql-01 ~]# yum makecache
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                                       | 5.5 kB  00:00:00     
 * base: mirrors.163.com
 * epel: mirrors.ustc.edu.cn
......

更新完存储库之后再次执行 yum repolist 命令我们可以看到存储库里面新增了MySQL数据库的存储库。

[root@mysql-01 ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * epel: mirrors.ustc.edu.cn
 * extras: mirrors.163.com
 * updates: mirrors.163.com
mysql-connectors-community                                     | 2.6 kB  00:00:00     
mysql-tools-community                                          | 2.6 kB  00:00:00     
mysql80-community                                              | 2.6 kB  00:00:00     
(1/3): mysql-tools-community/x86_64/primary_db                 |  85 kB  00:00:00     
(2/3): mysql-connectors-community/x86_64/primary_db            |  72 kB  00:00:00     
(3/3): mysql80-community/x86_64/primary_db                     | 142 kB  00:00:00     
repo id                           repo name                                           status
base/7/x86_64                     CentOS-7 - Base                                     10,072
epel/x86_64                       Extra Packages for Enterprise Linux 7 - x86_64      13,550
extras/7/x86_64                   CentOS-7 - Extras                                   453
mysql-connectors-community/x86_64 MySQL Connectors Community                          185
mysql-tools-community/x86_64      MySQL Tools Community                               123
mysql80-community/x86_64          MySQL 8.0 Community Server                          229
updates/7/x86_64                  CentOS-7 - Updates                                  1,729
repolist: 26,341

默认情况下,默认启用最新GA系列(当前为MySQL 8.0)的子存储库,而所有其他系列(例如,MySQL 5.7系列)的子存储库均被禁用。使用此命令可查看MySQL Yum存储库中的所有子存储库,并查看已启用或禁用了哪些子存储库。

[root@mysql-01 ~]# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64  MySQL Cluster 7.5 Community  disabled
mysql-cluster-7.5-community-source  MySQL Cluster 7.5 Community  disabled
mysql-cluster-7.6-community/x86_64  MySQL Cluster 7.6 Community  disabled
mysql-cluster-7.6-community-source  MySQL Cluster 7.6 Community  disabled
mysql-cluster-8.0-community/x86_64  MySQL Cluster 8.0 Community  disabled
mysql-cluster-8.0-community-source  MySQL Cluster 8.0 Community  disabled
mysql-connectors-community/x86_64   MySQL Connectors Community   enabled:    185
mysql-connectors-community-source   MySQL Connectors Community - disabled
mysql-tools-community/x86_64        MySQL Tools Community        enabled:    123
mysql-tools-community-source        MySQL Tools Community - Sour disabled
mysql-tools-preview/x86_64          MySQL Tools Preview          disabled
mysql-tools-preview-source          MySQL Tools Preview - Source disabled
mysql55-community/x86_64            MySQL 5.5 Community Server   disabled
mysql55-community-source            MySQL 5.5 Community Server - disabled
mysql56-community/x86_64            MySQL 5.6 Community Server   disabled
mysql56-community-source            MySQL 5.6 Community Server - disabled
mysql57-community/x86_64            MySQL 5.7 Community Server   disabled
mysql57-community-source            MySQL 5.7 Community Server - disabled
mysql80-community/x86_64            MySQL 8.0 Community Server   enabled:    229
mysql80-community-source            MySQL 8.0 Community Server - disabled
[root@mysql-01 ~]# 

从上面我们可以看到当前除了 mysql80-community/x86_64 、其他的MySQL版本都已经被禁止了;这里我们需要安装5.7版本的,所以我们把 MySQL 8.0 进行禁用,然后再启用 MySQL 5.7 版本。

# 禁用MySQL 8.0
yum-config-manager --disable mysql80-community

# 启用MySQL 5.7
yum-config-manager --enable mysql57-community

# 注:运行 yum-config-manager命令提示 -bash: yum-config-manager: command not found 命令未找到,因为 yum-config-manager 在 yum-utils包里面;由于系统默认没有这个命令,需要另外进行安装。
yum -y install yum-utils

然后我们执行下面的命令安装 MySQL 5.7 数据库

# 安装MySQL数据库
yum install mysql-community-server

Yum安装完成以后、MySQL默认的数据目录在 /var/lib/mysql 里面;这里我们通过下面的命令来启动并初始化 MySQL(初始化过程中MySQL会在 /var/lib/mysql 目录自动生成数据文件) :

[root@mysql-01 ~]# cd /var/lib/mysql
[root@mysql-01 mysql]# ls
# 启动并初始化MySQL
[root@mysql-01 mysql]# systemctl start mysqld
[root@mysql-01 mysql]# ls
auto.cnf    client-cert.pem  ibdata1      ibtmp1      mysql.sock.lock     public_key.pem   sys
ca-key.pem  client-key.pem   ib_logfile0  mysql       performance_schema  server-cert.pem
ca.pem      ib_buffer_pool   ib_logfile1  mysql.sock  private_key.pem     server-key.pem

# 设置MySQL开机启动
[root@mysql-01 mysql]# systemctl enable mysqld
[root@mysql-01 mysql]# mysql --version
mysql  Ver 14.14 Distrib 5.7.33, for Linux (x86_64) using  EditLine wrapper
[root@mysql-01 mysql]# 

# 查看MySQL服务器初始化的时候会创建 'root@localhost' 超级用户账号;设置超级用户的密码并将其存储在错误日志文件中。
[root@mysql-01 mysql]# grep 'password' /var/log/mysqld.log 
2021-03-08T02:05:35.569352Z 1 [Note] A temporary password is generated for root@localhost: X!;awTXzb6(d
2021-03-08T02:05:54.221382Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO)
[root@mysql-01 mysql]#

安装完成之后我们通过 mysql –version 命令查看一下MySQL数据库版本、并用上面查询到的默认密码登录数据库;我们还需要把默认的数据库密码修改掉、然后授权用户可以远程访问、具体操作如下:

# 查看MySQL版本
[root@mysql-01 ~]# mysql --version
mysql  Ver 14.14 Distrib 5.7.33, for Linux (x86_64) using  EditLine wrapper
[root@mysql-01 ~]# mysql -u root -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.33

Copyright (c) 2000, 2021, 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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
# 更新数据库密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'z0ukun@2021';
Query OK, 0 rows affected (0.00 sec)
# 查看数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

# 进入mysql数据库
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> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
3 rows in set (0.00 sec)
# 注:用 select host ,user from user; 命令发现root用户的host是localhost,localhost 表示只能进行本地访问,不能进行远程连接;

# 修改root用户的host权限为%
mysql> update user set host = "%" where user = "root";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

# 刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye
[root@mysql-01 ~]# 

然后我们用 Navicat 测试连接、MySQL现在已经可以正常连接数据库了。

image-20210308132100975

1.3、二进制预编译安装(Generic)

预编译版下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz

前面我们通过Yum存储库的方式安装了MySQL、安装过程还是非常简单的;但是正是因为这种简单的安装过程导致MySQL的自定义程度较低;比如我们如何定义MySQL的安装参数、MySQL的安装位置等。下面我们再一起来看看如何通过二进制进行预编译版本安装。

这里我们去MySQL官网下载对应的Linux – Generic预编译版即可:

[root@mysql-01 ~]# rpm -q glibc
glibc-2.17-323.el7_9.x86_64
glibc-2.17-323.el7_9.i686
[root@mysql-01 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
--2021-03-08 13:52:21--  https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz [following]
--2021-03-08 13:52:22--  https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
Resolving cdn.mysql.com (cdn.mysql.com)... 23.45.53.26
Connecting to cdn.mysql.com (cdn.mysql.com)|23.45.53.26|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 661718255 (631M) [application/x-tar-gz]
Saving to: ‘mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz’

100%[========================================================================================>] 661,718,255 4.76MB/s   in 2m 13s 

2021-03-08 13:54:36 (4.73 MB/s) - ‘mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz’ saved [661718255/661718255]

[root@mysql-01 ~]# 

微信图片_20210308134826

Yum安装之所以简单、是因为MySQL自动帮我们完成了用户组的创建、解包、安装、MySQL初始化等工作;那么如果需要自己进行二进制预编译安装、就需要手动去执行这些操作。

# 创建用户和用户组
groupadd mysql && useradd -r -g mysql -s /bin/false mysql

# 我们把mysql安装到 /usr/local目录下面
cd /usr/local && tar -zxvf /root/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
[root@mysql-01 local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql-5.7.33-linux-glibc2.12-x86_64  sbin  share  src
[root@mysql-01 local]# 

# 软连接mysql(也可以直接通过mv、copy等命令操作)
ln -s mysql-5.7.33-linux-glibc2.12-x86_64 mysql

注:这里我们没有直接去修改 mysql-5.7.33-linux-glibc2.12-x86_64 目录的名称、而是通过软连接的方式来生成一个mysql连接;这样做的目的是为了后期多版本共存或者方便版本升级。

现在我们可以开始MySQL数据库初始化了、初始化没有编译安装的过程;这里我们新建一个mysql-files目录、在初始化过程中会有相关文件写入到这个目录中、所以我们对这个目录进行授权。

[root@mysql-01 local]# pwd
/usr/local
[root@mysql-01 local]# cd mysql && mkdir mysql-files && chmod 750 mysql-files
# 授权当前目录所属用户和所属用户组
[root@mysql-01 mysql]# chown -R mysql . && chgrp -R mysql .
# 执行初始化命令并指定mysql软件目录和数据目录
[root@mysql-01 mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2021-03-08T11:49:15.320030Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-03-08T11:49:18.000767Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-03-08T11:49:18.451042Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-03-08T11:49:18.535804Z 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: 5098f355-8004-11eb-b5c3-005000000100.
2021-03-08T11:49:18.553911Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-03-08T11:49:19.660769Z 0 [Warning] CA certificate ca.pem is self signed.
# 这里生成了一个默认的临时密码(这个密码要保存一下、后面会用到)
2021-03-08T11:49:19.885793Z 1 [Note] A temporary password is generated for root@localhost: OY;kalp8J8xp
# 配置SSL加密连接
[root@mysql-01 mysql]# bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
# 再次修改当前目录所属用户和所属用户组
[root@mysql-01 mysql]# chown -R root . && chown -R mysql data mysql-files
[root@mysql-01 mysql]# 

# MySQL5.7版本以后就不再提供mysql.cnf配置文件模板了
[root@mysql-01 support-files]# cd ..
[root@mysql-01 mysql]# ls
bin  data  docs  include  lib  LICENSE  man  mysql-files  README  share  support-files
[root@mysql-01 mysql]# cd support-files/
[root@mysql-01 support-files]# ls
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
# 这里我们手动创建/etc/my.cnf配置文件并插入如下内容
[root@mysql-01 support-files]# vi /etc/my.cnf
[root@mysql-01 support-files]# cat /etc/my.cnf 
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

# 启动MySQL、这里有两种方法:
# 方法一
[root@mysql-01 support-files]# bin/mysqld_safe --user=mysql &

# 方法二
[root@mysql-01 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@mysql-01 mysql]# chkconfig --add mysqld
[root@mysql-01 mysql]# chkconfig mysqld on
[root@mysql-01 mysql]# systemctl start mysqld
[root@mysql-01 mysql]# systemctl status mysqld
● mysqld.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
   Active: active (running) since Mon 2021-03-08 20:09:12 CST; 5s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 9864 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/mysqld.service
           ├─9875 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/mysql-01.pid
           └─9987 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=mysql-01.err --pid-file=/usr...

Mar 08 20:09:09 mysql-01 systemd[1]: Starting LSB: start and stop MySQL...
Mar 08 20:09:09 mysql-01 mysqld[9864]: Starting MySQL.Logging to '/usr/local/mysql/data/mysql-01.err'.
Mar 08 20:09:12 mysql-01 mysqld[9864]: .. SUCCESS!
Mar 08 20:09:12 mysql-01 systemd[1]: Started LSB: start and stop MySQL.
[root@mysql-01 mysql]# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      925/sshd            
tcp6       0      0 :::22                   :::*                    LISTEN      925/sshd            
tcp6       0      0 :::3306                 :::*                    LISTEN      9987/mysqld         
[root@mysql-01 mysql]#

MySQL启动以后我们使用mysql命令进入数据库发现系统提示我们 command not found ;这是因为系统环境变量里面并没有关于MySQL启动脚本的环境变量、我们重新对 /etc/profile 文件赋值以后再次使用 mysql 就可以正常使用了(密码为前面的临时密码):

[root@mysql-01 mysql]# mysql
-bash: mysql: command not found
[root@mysql-01 mysql]# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@mysql-01 mysql]# source /etc/profile
[root@mysql-01 mysql]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33

Copyright (c) 2000, 2021, 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命令、也可以通过下面的方式来执行mysql命令:

[root@mysql-01 mysql]# /usr/local/mysql/bin/mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.33

Copyright (c) 2000, 2021, 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.

# 剩下的操作就和Yum安装MySQL数据库的操作是一样的啦、这里就不再详细介绍了。
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'z0ukun@2021';
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.33    |
+-----------+
1 row in set (0.00 sec)

mysql> 

如果你发现前面的数据库安装的有问题想要进行初始化可以执行下面的命令:

[root@mysql-01 mysql]# yum install psmisc -y 
[root@mysql-01 mysql]# killall -9 mysqld
[root@mysql-01 mysql]# rm -rf /usr/local/mysql/data
[root@mysql-01 mysql]# chown -R mysql . && chgrp -R mysql .
[root@mysql-01 mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[root@mysql-01 mysql]# /bin/mysql_ssl_rsa_setup
[root@mysql-01 mysql]# chown -R root .
[root@mysql-01 mysql]# chown -R mysql data mysql-files

至此、二进制预编译安装MySQL就已经完成了。

1.4、源码包安装(Source Code)

源码包下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.33.tar.gz

现在我们再一起来看看如何通过源码包编译安装、源码包安装需要用到几个工具:cmake、C++、GCC等工具库。

yum install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make cmake -y
[root@mysql-01 ~]# groupadd mysql && useradd -r -g mysql -s /bin/false mysql
[root@mysql-01 ~]# tar -zxvf mysql-boost-5.7.33.tar.gz && cd mysql-5.7.33
[root@mysql-01 mysql-5.7.33]#


# 这里一定要下载带boost的版本、否则CMAKE的时候会报boost错误
[root@mysql-01 mysql-5.7.33]# cmake . \
-DWITH_BOOST=boost/ \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DINSTALL_MANDIR=/usr/share/man \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DEXTRA_CHARSET=all \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1

# CMAKE完成以后、我们就可以开始make编译了(make的过程较长、过程略过)
[root@mysql-01 mysql-5.7.33]# make
[root@mysql-01 mysql-5.7.33]# make install

# 初始化数据库
[root@mysql-01 mysql-5.7.33]# find / -name mysql
/run/lock/subsys/mysql
/etc/selinux/targeted/active/modules/100/mysql
/root/mysql-5.7.33/client/mysql
/root/mysql-5.7.33/include/mysql
/root/mysql-5.7.33/rapid/plugin/group_replication/include/mysql
/root/mysql-5.7.33/rapid/plugin/group_replication/libmysqlgcs/include/mysql
/usr/local/mysql-5.7.33-linux-glibc2.12-x86_64/bin/mysql
/usr/local/mysql-5.7.33-linux-glibc2.12-x86_64/include/mysql
/usr/local/mysql
[root@mysql-01 mysql-5.7.33]# cd /usr/local/mysql
[root@mysql-01 local]# mkdir mysql-files && chmod 750 mysql-files
# 授权当前目录所属用户和所属用户组
[root@mysql-01 mysql]# chown -R mysql . && chgrp -R mysql .
# 执行初始化命令并指定mysql软件目录和数据目录
[root@mysql-01 mysql]# 
[root@mysql-01 mysql]# ls
bin  docs  include  lib  LICENSE  man  mysql-files  mysql-test  README  README-test  share  support-files
[root@mysql-01 mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2021-03-08T14:38:01.938587Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-03-08T14:38:03.354165Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-03-08T14:38:03.550694Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-03-08T14:38:03.602296Z 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: e39b7daf-801b-11eb-9073-005000000100.
2021-03-08T14:38:03.629377Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-03-08T14:38:03.959177Z 0 [Warning] CA certificate ca.pem is self signed.
# 初始化数据库生成的临时密码
2021-03-08T14:38:04.016088Z 1 [Note] A temporary password is generated for root@localhost: 9UGZ_fE1oS2,
[root@mysql-01 mysql]# ls
bin  data  docs  include  lib  LICENSE  man  mysql-files  mysql-test  README  README-test  share  support-files
[root@mysql-01 mysql]# chown -R root . && chown -R mysql data mysql-files

# 添加MySQL数据库到启动项并启动数据库
[root@mysql-01 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@mysql-01 mysql]# chkconfig --add mysqld
[root@mysql-01 mysql]# chkconfig mysqld on
[root@mysql-01 mysql]# systemctl start mysql
[root@mysql-01 mysql]# systemctl status mysql
● mysqld.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
   Active: active (running) since Tue 2021-03-09 08:15:17 CST; 5min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 7004 ExecStop=/etc/rc.d/init.d/mysqld stop (code=exited, status=0/SUCCESS)
  Process: 7025 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/mysqld.service
           ├─7036 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/mys...
           └─7148 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/loca...

Mar 09 08:15:16 mysql-01 systemd[1]: Starting LSB: start and stop MySQL...
Mar 09 08:15:16 mysql-01 mysqld[7025]: Starting MySQL.Logging to '/usr/local/mysql/data/mysql-01.err'.
Mar 09 08:15:17 mysql-01 mysqld[7025]: SUCCESS!
Mar 09 08:15:17 mysql-01 systemd[1]: Started LSB: start and stop MySQL.
[root@mysql-01 mysql]# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      925/sshd            
tcp6       0      0 :::22                   :::*                    LISTEN      925/sshd            
tcp6       0      0 :::3306                 :::*                    LISTEN      7148/mysqld         
[root@mysql-01 mysql]#

# 剩下的操作就和前面的操作一样啦、请小伙伴们自行查看
[root@mysql-01 mysql]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33

Copyright (c) 2000, 2021, 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>

1.5、MySQL常用命令

# 启动mysql服务
systemctl start mysqld.service

# 停止mysql服务
systemctl stop mysqld.service

# 重启mysql服务
systemctl restart mysqld.service

# 查看mysql服务当前状态
systemctl status mysqld.service

# 设置mysql服务开机自启动
systemctl enable mysqld.service

# 停止mysql服务开机自启动
systemctl disable mysqld.service

# 查看所有已启动的服务
systemctl list-units --type=service

推荐文章

8条评论

  1. Asking questions are truly good thiing if you
    are not understanding anything totally, however this ppost gives nice
    understtanding even.
    site

  2. Does your site have a contact page? I’m having trouble locating it but,
    I’d like to shoot you an e-mail. I’ve got some
    suggestions for your blog you might be interested in hearing.
    Either way, great site and I look forward to seeing it
    expand over time.

  3. Superb blog! Do yoou have any suggestions for aspiring writers?
    I’m hoping to start myy owwn blog soon but I’m a little lost on everything.

    Would you propose starting with a free platform like WordPress oor go for a
    paid option? There arre so many options out there that
    I’m totally overwhelmed ..Any suggestions? Thanks a lot!

    website

  4. Yes! Finally something about 0mniartist. asmr 0mniartist

  5. Wow, this post is nice, my younger sister is analyzing these things,
    thus I am going to inform her. asmr 0mniartist

  6. My relatives all the time say that I am wasting my time here
    at web, however I know I am getting experience all the time by reading
    thes fastidious content. asmr 0mniartist

  7. Hello there, I believe your site could be having web browser
    compatibility issues. When I look at your blog in Safari, it looks fine however, when opening in Internet Explorer, it’s got some
    overlapping issues. I just wanted to provide you with a quick heads up!
    Apart from that, excellent website! asmr 0mniartist

  8. Have you ever considered writing an ebook or guest authoring on other blogs?
    I have a blog based on the same information you discuss
    and would love to have you share some stories/information. I
    know my visitors would enjoy your work. If you are
    even remotely interested, feel free to shoot me an email.
    0mniartist asmr

发表评论

邮箱地址不会被公开。 必填项已用*标注