07、以源码的方式部署MySQL

源码安装大约是部署当中最麻烦的一种部署方式了,但是其可以自定义部署各种插件,可以让我们的MySQL数据库按照我们定制的那样去部署安装。

 

数据库官网下载地址 https://downloads.mysql.com/archives/community/

1、下载MySQL数据库源代码

本次实验我们也是使用MySQL 5.7,下面我们开始下载MySQL数据库源代码包。

07、以源码的方式部署MySQL

[root@localhost ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34.tar.gz
--2021-09-14 17:13:20--  https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34.tar.gz
Resolving downloads.mysql.com (downloads.mysql.com)... 137.254.60.14
Connecting to downloads.mysql.com (downloads.mysql.com)|137.254.60.14|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.34.tar.gz [following]
--2021-09-14 17:13:21--  https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.34.tar.gz
Resolving cdn.mysql.com (cdn.mysql.com)... 23.44.151.164
Connecting to cdn.mysql.com (cdn.mysql.com)|23.44.151.164|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 56150251 (54M) [application/x-tar-gz]
Saving to: ‘mysql-5.7.34.tar.gz’

100%[=========================================================================================>] 56,150,251  30.7MB/s   in 1.7s   

2021-09-14 17:13:23 (30.7 MB/s) - ‘mysql-5.7.34.tar.gz’ saved [56150251/56150251]

 

2、解压源代码包

把下载下来的源代码包解压,得到源代码,用于后面的代码编译。

[root@localhost ~]# tar -xf mysql-5.7.34.tar.gz 
[root@localhost ~]# cd mysql-5.7.34
[root@localhost mysql-5.7.34]# ll
total 504
drwxr-xr-x.  2 7161 31415   4096 Mar 26 15:08 BUILD
drwxr-xr-x.  6 7161 31415   4096 Mar 26 15:08 client
drwxr-xr-x.  4 7161 31415   4096 Mar 26 15:08 cmake
-rw-r--r--.  1 7161 31415  27989 Mar 26 14:58 CMakeLists.txt
-rw-r--r--.  1 7161 31415  14529 Mar 26 14:58 config.h.cmake
-rw-r--r--.  1 7161 31415  24834 Mar 26 14:58 configure.cmake
drwxr-xr-x.  2 7161 31415   4096 Mar 26 15:08 dbug
drwxr-xr-x.  2 7161 31415     80 Mar 26 15:08 Docs
-rw-r--r--.  1 7161 31415  67369 Mar 26 14:58 Doxyfile-perfschema
drwxr-xr-x.  7 7161 31415   4096 Mar 26 15:08 extra
drwxr-xr-x.  5 7161 31415   4096 Mar 26 15:08 include
-rw-r--r--.  1 7161 31415    333 Mar 26 14:58 INSTALL
drwxr-xr-x.  5 7161 31415    135 Mar 26 15:08 libbinlogevents
drwxr-xr-x.  3 7161 31415     39 Mar 26 15:08 libbinlogstandalone
drwxr-xr-x.  4 7161 31415    275 Mar 26 15:08 libmysql
drwxr-xr-x.  3 7161 31415    204 Mar 26 15:08 libmysqld
drwxr-xr-x.  2 7161 31415   4096 Mar 26 15:08 libservices
-rw-r--r--.  1 7161 31415 257591 Mar 26 14:58 LICENSE
drwxr-xr-x.  2 7161 31415   4096 Mar 26 15:08 man
drwxr-xr-x. 10 7161 31415   4096 Mar 26 15:08 mysql-test
drwxr-xr-x.  2 7161 31415   4096 Mar 26 15:08 mysys
drwxr-xr-x.  2 7161 31415    280 Mar 26 15:08 mysys_ssl
drwxr-xr-x. 10 7161 31415    131 Mar 26 15:08 packaging
drwxr-xr-x. 18 7161 31415   4096 Mar 26 15:08 plugin
drwxr-xr-x.  4 7161 31415     36 Mar 26 15:08 rapid
-rw-r--r--.  1 7161 31415    566 Mar 26 14:58 README
drwxr-xr-x.  2 7161 31415   4096 Mar 26 15:08 regex
drwxr-xr-x.  4 7161 31415   4096 Mar 26 15:08 scripts
drwxr-xr-x.  2 7161 31415      6 Mar 26 15:07 source_downloads
drwxr-xr-x.  8 7161 31415  20480 Mar 26 15:08 sql
drwxr-xr-x.  2 7161 31415    198 Mar 26 15:08 sql-common
drwxr-xr-x. 14 7161 31415    186 Mar 26 15:08 storage
drwxr-xr-x.  2 7161 31415   4096 Mar 26 15:08 strings
drwxr-xr-x.  4 7161 31415    241 Mar 26 15:08 support-files
drwxr-xr-x.  2 7161 31415     98 Mar 26 15:08 testclients
drwxr-xr-x.  5 7161 31415     70 Mar 26 15:08 unittest
-rw-r--r--.  1 7161 31415     88 Mar 26 14:58 VERSION
drwxr-xr-x.  3 7161 31415   4096 Mar 26 15:08 vio
drwxr-xr-x.  2 7161 31415     32 Mar 26 15:08 win
drwxr-xr-x. 11 7161 31415   4096 Mar 26 15:08 zlib
[root@localhost mysql-5.7.34]# 

3、安装依赖包

安装依赖包有利于后面编译MySQL的时候的流畅度。

[root@localhost mysql-5.7.34]# yum install -y ncurses-devel libaio-devel gcc gcc-c++ glibc cmake autoconf openssl openssl-devel

4、创建数据存放目录

创建这个目录的主要目的是为了创建数据库之后存放数据库保存的数据的目录。

[root@localhost mysql-5.7.34]# /usr/local/mysql-5.7.34/data

5、安装boots

一组C++ 库的集合,提供了线性代数、伪随机数生成、多线程、图像处理、正则表达式、单元测试等功能。

[root@localhost ~]# wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
[root@localhost ~]# tar xf boost_1_59_0.tar.gz -C /usr/local/

6、编译MySQL

编译MySQL是将MySQL源代码编译成MySQL可执行文件。

[root@localhost ~]# cd mysql-5.7.34

# 生成CMAKE
[root@localhost ~]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.34 \
-DMYSQL_DATADIR=/usr/local/mysql-5.7.34/data \
-DMYSQL_UNIX_ADDR=/usr/local/mysql-5.7.34/tmp/mysql.sock \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/boost_1_59_0 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_SSL=system \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0

# 编译
[root@localhost mysql-5.7.34]# make


# 安装
[root@localhost mysql-5.7.34]# make install

#程序存放位置
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.34 \
#数据存放位置
-DMYSQL_DATADIR=/usr/local/mysql-5.7.34/data \
#socket文件存放位置
-DMYSQL_UNIX_ADDR=/usr/local/mysql-5.7.34/tmp/mysql.sock \
#使用utf8字符集
-DDEFAULT_CHARSET=utf8 \
#校验规则
-DDEFAULT_COLLATION=utf8_general_ci \
#使用其他额外的字符集
-DWITH_EXTRA_CHARSETS=all \
#支持的存储引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
#禁用的存储引擎
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
#启用zlib库支持(zib、gzib相关)
-DWITH_ZLIB=bundled \
#启用SSL库支持(安全套接层)
-DWITH_SSL=bundled \
#启用本地数据导入支持
-DENABLED_LOCAL_INFILE=1 \
#编译嵌入式服务器支持
-DWITH_EMBEDDED_SERVER=1 \
# mysql5.6支持了google的c++mock框架了,允许下载,否则会安装报错。
-DENABLE_DOWNLOADS=1 \
#禁用debug(默认为禁用)
-DWITH_DEBUG=0

7、创建数据库用户

之所以创建数据库用户,是为了同意数据库用户,防止数据库启动时报权限错误。

[root@localhost mysql-5.7.34]# useradd mysql -s /sbin/nologin -M

8、初始化MySQL配置

MySQL服务启动需要Mysql配置,默认为:/etc/my.cnf。

  • 编写MySQL配置
[root@localhost mysql]# vim /etc/my.cnf
[root@localhost mysql]# cat /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql-5.7.34
datadir=/usr/local/mysql-5.7.34/data
port=3306
socket=/usr/local/mysql-5.7.34/tmp/mysql.sock
character-set-server=utf8mb4
log-error=/var/log/mysqld.log
pid-file=/usr/local/mysql-5.7.34/tmp/mysqld.pid
[mysql]
socket=/usr/local/mysql-5.7.34/tmp/mysql.sock
[client]
socket=/usr/local/mysql-5.7.34/tmp/mysql.sock

 

9、目录授权

目录授权主要目的是为了MySQL数据库启动的时候不会因为权限问题二导致报错。

[root@localhost mysql]# chown -R mysql.mysql /usr/local/mysql-5.7.34/

 

10、初始化MySQL服务

再MySQL服务启动之前,我们还需要将其初始化一下,生成默认的数据及配置。

[root@localhost mysql-5.7.34]# cd /usr/local/mysql-5.7.34/
[root@localhost mysql-5.7.34]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql-5.7.34 --datadir=/usr/local/mysql-5.7.34/data

 

11、启动MySQL服务

一切都准备好之后,我们就可以舒舒服服的启动我们的Mysql服务了。

[root@localhost mysql-5.7.34]# /usr/local/mysql-5.7.34/support-files/mysql.server start
Starting MySQL. SUCCESS!
[root@localhost mysql-5.7.34]# netstat -nutlp | grep [m]ysql
tcp6       0      0 :::3306                 :::*                    LISTEN      1372/mysqld  

12、尝试连接MySQL服务

启动服务之后,我们就可以去连接Mysql服务。

# 查看默认生成的密码
[root@localhost mysql-5.7.34]# cat /var/log/mysqld.log | grep 'temporary password'
2021-09-14T14:55:31.740536Z 1 [Note] A temporary password is generated for root@localhost: &LiK&vObh0ew

# 使用密码连接MySQL服务
[root@localhost mysql-5.7.34]# mysql -uroot -p"&LiK&vObh0ew"
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.34

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> 

13、将MySQL服务加入systemd

使用systemd服务管理MySQL服务,这样方便平常的操作。

[root@localhost mysql]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql-5.7.34/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
[root@localhost mysql-5.7.34]# systemctl daemon-reload
[root@localhost mysql-5.7.34]# systemctl start mysqld
[root@localhost mysql-5.7.34]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-09-14 23:16:49 CST; 6s ago
     Docs: man:mysqld(8)
           https://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 33666 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─33666 /usr/local/mysql-5.7.34/bin/mysqld --defaults-file=/etc/my.cnf

Sep 14 23:16:49 localhost.localdomain systemd[1]: Started MySQL Server.

# 重新尝试连接
[root@localhost mysql-5.7.34]# mysql -uroot -p"&LiK&vObh0ew"
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.34

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源码部署全部完成。

     
上一篇:2021-11-07


下一篇:2021-11-07