二进制安装MySQL其实就是把已经编译好的mysql,做了个压缩包,下载下来,解压缩,简单配置之后,就能使用,‘安装’速度快,往往用于mysql的快速部署。

1、准备工作

  • 检查系统中的Mysql或mariadb,如果存在将其卸载 ```bash [root@localhost ~]# rpm -qa | grep mysql [root@localhost ~]# rpm -qa | grep mariadb mariadb-libs-5.5.68-1.el7.x86_64 [root@localhost ~]# yum -y remove mariadb-libs.x86_64 Loaded plugins: fastestmirror Resolving Dependencies —> Running transaction check —-> Package mariadb-libs.x86_64 1:5.5.68-1.el7 will be erased —> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 1:net-snmp-5.7.2-49.el7_9.1.x86_64 —> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 1:net-snmp-agent-libs-5.7.2-49.el7_9.1.x86_64 —> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-9.el7.x86_64 —> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-9.el7.x86_64 —> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 1:net-snmp-agent-libs-5.7.2-49.el7_9.1.x86_64 —> Processing Dependency: mysql-libs for package: 1:net-snmp-5.7.2-49.el7_9.1.x86_64 —> Running transaction check —-> Package net-snmp.x86_64 1:5.7.2-49.el7_9.1 will be erased —-> Package net-snmp-agent-libs.x86_64 1:5.7.2-49.el7_9.1 will be erased —-> Package postfix.x86_64 2:2.10.1-9.el7 will be erased —> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================================

Package Arch Version Repository Size

Removing: mariadb-libs x86_64 1:5.5.68-1.el7 @anaconda 4.4 M Removing for dependencies: net-snmp x86_64 1:5.7.2-49.el7_9.1 @updates 850 k net-snmp-agent-libs x86_64 1:5.7.2-49.el7_9.1 @updates 2.1 M postfix x86_64 2:2.10.1-9.el7 @anaconda 12 M

Transaction Summary

Remove 1 Package (+3 Dependent packages)

Installed size: 20 M Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Erasing : 1:net-snmp-5.7.2-49.el7_9.1.x86_64 1/4 Erasing : 1:net-snmp-agent-libs-5.7.2-49.el7_9.1.x86_64 2/4 Erasing : 2:postfix-2.10.1-9.el7.x86_64 3/4 Erasing : 1:mariadb-libs-5.5.68-1.el7.x86_64 4/4 warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave Verifying : 2:postfix-2.10.1-9.el7.x86_64 1/4 Verifying : 1:net-snmp-agent-libs-5.7.2-49.el7_9.1.x86_64 2/4 Verifying : 1:mariadb-libs-5.5.68-1.el7.x86_64 3/4 Verifying : 1:net-snmp-5.7.2-49.el7_9.1.x86_64 4/4

Removed: mariadb-libs.x86_64 1:5.5.68-1.el7

Dependency Removed: net-snmp.x86_64 1:5.7.2-49.el7_9.1 net-snmp-agent-libs.x86_64 1:5.7.2-49.el7_9.1 postfix.x86_64 2:2.10.1-9.el7

Complete! [root@localhost ~]#

  1. - 下载MySQL 5.7安装包
  2. 前往mysql官网下载二进制安装包,[https://dev.mysql.com/downloads/mysql/5.7.html#downloads](https://dev.mysql.com/downloads/mysql/5.7.html#downloads)(注意:选择操作系统时选Linux-Generic)。我这里下载的是mysql-5.7.26-linux-glibc2.12-x86_64。<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/682281/1631604058141-505de087-b8ca-4be0-a34d-63fc87cabdf9.png?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_53%2Ctext_5bCP6Ziz54ix5oqA5pyvQOaKlumfsw%3D%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10#clientId=u337ce800-1a5e-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=670&id=ud6c823dd&margin=%5Bobject%20Object%5D&name=image.png&originHeight=1340&originWidth=1864&originalType=binary&ratio=1&rotation=0&showTitle=false&size=246642&status=done&style=none&taskId=ucd21daac-76f9-4837-aaea-37d4b247bd3&title=&width=932)<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/682281/1631604088947-87fa7d03-fb22-4712-b3cd-7e02696cfcdc.png?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_47%2Ctext_5bCP6Ziz54ix5oqA5pyvQOaKlumfsw%3D%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10#clientId=u337ce800-1a5e-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=512&id=u886f24eb&margin=%5Bobject%20Object%5D&name=image.png&originHeight=1024&originWidth=1648&originalType=binary&ratio=1&rotation=0&showTitle=false&size=173285&status=done&style=none&taskId=uba9b7cfb-eaa2-4a73-bcaf-6f76fc588b3&title=&width=824)
  3. ```bash
  4. [root@localhost ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
  5. --2021-09-14 15:25:05-- https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
  6. Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
  7. Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.
  8. HTTP request sent, awaiting response... 302 Found
  9. Location: https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz [following]
  10. --2021-09-14 15:25:06-- https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
  11. Resolving cdn.mysql.com (cdn.mysql.com)... 23.44.151.164
  12. Connecting to cdn.mysql.com (cdn.mysql.com)|23.44.151.164|:443... connected.
  13. HTTP request sent, awaiting response... 200 OK
  14. Length: 666328842 (635M) [application/x-tar-gz]
  15. Saving to: ‘mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz’
  16. 100%[=========================================================================================>] 666,328,842 26.3MB/s in 23s
  17. 2021-09-14 15:25:30 (27.6 MB/s) - ‘mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz’ saved [666328842/666328842]

2、创建用户

之所以需要创建用户,主要的目的是为了统一MySQL的用户。

  1. [root@localhost ~]# useradd -M -s /sbin/nologin -r mysql

3、安装依赖包

之所以要安装依赖包,主要的目的是提前安装好所有需要用到的插件包,防止MySQL在安装之后无法使用。

  1. [root@localhost ~]# yum install -y ncurses-devel libaio-devel gcc gcc-c++ numactl libaio glibc cmake autoconf

4、解压MySQL压缩包

我们需要解压刚下载下来的MySQL压缩包,方便其进行安装。

  1. [root@localhost ~]# tar -xf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
  2. [root@localhost ~]# ln -s /usr/local/mysql-5.7.35-linux-glibc2.12-x86_64/ /usr/local/mysql
  3. [root@localhost ~]# ll /usr/local/mysql/
  4. total 272
  5. drwxr-xr-x. 2 root root 4096 Sep 14 15:42 bin
  6. drwxr-xr-x. 2 root root 55 Sep 14 15:43 docs
  7. drwxr-xr-x. 3 root root 4096 Sep 14 15:42 include
  8. drwxr-xr-x. 5 root root 230 Sep 14 15:43 lib
  9. -rw-r--r--. 1 7161 31415 259200 Jun 7 20:52 LICENSE
  10. drwxr-xr-x. 4 root root 30 Sep 14 15:42 man
  11. -rw-r--r--. 1 7161 31415 566 Jun 7 20:52 README
  12. drwxr-xr-x. 28 root root 4096 Sep 14 15:43 share
  13. drwxr-xr-x. 2 root root 90 Sep 14 15:43 support-files

5、将MySQL目录授权

之所以需要授权,主要的目的是为了让MySQL启动的时候不至于应为权限的问题导致启动报错。

  1. [root@localhost local]# chown -R mysql.mysql /usr/local/mysql/
  2. [root@localhost local]# chown -R mysql.mysql /usr/local/mysql-5.7.36-linux-glibc2.12-x86_64

6、MySQL数据库初始化

在启动之前,我们需要将MySQL初始化一下,生成一些默认的配置和数据库以及数据库密码等。

  • 创建保存数据的目录 ```bash [root@localhost mysql]# mkdir /mysql_data [root@localhost mysql]# chown -R mysql.mysql /mysql_data/

[root@localhost local]# touch /var/log/mysqld.log [root@localhost local]# chown -R mysql.mysql /var/log/mysqld.log

  1. - 初始化
  2. ```bash
  3. [root@localhost mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/mysql_data
  4. 2021-09-14T07:59:12.785658Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
  5. 2021-09-14T07:59:14.770442Z 0 [Warning] InnoDB: New log files created, LSN=45790
  6. 2021-09-14T07:59:15.030108Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
  7. 2021-09-14T07:59:15.113597Z 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: a79a5fbd-1531-11ec-a40a-000c294d81ef.
  8. 2021-09-14T07:59:15.117014Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
  9. 2021-09-14T07:59:16.480600Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
  10. 2021-09-14T07:59:16.480683Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
  11. 2021-09-14T07:59:16.481652Z 0 [Warning] CA certificate ca.pem is self signed.
  12. 2021-09-14T07:59:17.481417Z 1 [Note] A temporary password is generated for root@localhost: nWHw,xjom9Nr // 默认的密码

7、编写配置文件

在启动MySQL之前,我们需要编写一个默认的配置文件/etc/my.cnf。

  1. [root@localhost mysql]# vim /etc/my.cnf
  2. [root@localhost mysql]# cat /etc/my.cnf
  3. [mysqld]
  4. basedir=/usr/local/mysql
  5. datadir=/mysql_data
  6. port=3306
  7. socket=/usr/local/mysql/mysql.sock
  8. character-set-server=utf8mb4
  9. log-error=/var/log/mysqld.log
  10. pid-file=/tmp/mysqld.pid
  11. [mysql]
  12. socket=/usr/local/mysql/mysql.sock
  13. [client]
  14. socket=/usr/local/mysql/mysql.sock

8、生成启动脚本,并启动mysql

将MySQL的启动脚本复制到对应的目录,为启动做准备。

  1. [root@master ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
  2. [root@localhost mysql]# chmod +x /etc/init.d/mysqld
  3. [root@localhost mysql]# vim /etc/init.d/mysqld
  4. basedir=/usr/local/mysql
  5. datadir=/mysql_data

9、启动,并测试

所有的准备工作做完之后,我们就可以启动了,然后登录到数据库,修改默认的密码。一切都做完之后就可以正常运行了。

  1. [root@localhost mysql]# /etc/init.d/mysqld start
  2. Starting MySQL. SUCCESS!
  3. [root@localhost mysql]# mysql -uroot -pnWHw,xjom9Nr
  4. Warning: Using a password on the command line interface can be insecure.
  5. Welcome to the MySQL monitor. Commands end with ; or \g.
  6. Your MySQL connection id is 2
  7. Server version: 5.7.35
  8. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  13. mysql>

10、加入Systemd管理

为了更方便使用,我们将MySQL服务加入到Systemd之中,方便统一管理。

#1.配置system管理MySQL
[root@localhost mysql]# vim /usr/lib/systemd/system/mysql.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/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000

#2.重新加载启动文件列表
[root@localhost mysql]# systemctl daemon-reload
[root@localhost mysql]# systemctl start mysql
[root@localhost mysql]# systemctl status mysql
● mysql.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysql.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-09-14 16:19:09 CST; 4s ago
     Docs: man:mysqld(8)
           https://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 1797 (mysqld)
   CGroup: /system.slice/mysql.service
           └─1797 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf

Sep 14 16:19:09 localhost.localdomain systemd[1]: Started MySQL Server.

至此,以二级制的方式部署MySQL结束。