文章目录
Linux二进制部署MySQL
1. 准备工作
- 检查系统中的Mysql或mariadb,如果存在将其卸载
[root@db01 ~]# rpm -qa | grep mysql
mysql-community-libs-5.7.36-1.el7.x86_64
mysql-community-common-5.7.36-1.el7.x86_64
mysql-community-client-5.7.36-1.el7.x86_64
mysql80-community-release-el7-4.noarch
mysql-community-server-5.7.36-1.el7.x86_64
[root@localhost ~]# rpm -qa | grep mariadb
[root@db01 ~]# yum -y remove mysql-community-common-5.7.36-1.el7.x86_64
...
[root@db01 ~]# rpm -qa | grep mysql
mysql80-community-release-el7-4.noarch
[root@db01 ~]# yum -y remove mysql80-community-release-el7-4.noarch
...
- 下载MySQL 5.7 安装包
前往mysql官网下载二进制安装包,https://dev.mysql.com/downloads/mysql/5.7.html#downloads(注意:选择操作系统时选Linux-Generic) 。
[root@db01 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
...
2. 创建用户
之所以需要创建用户,主要的目的是为了统一MySQL的用户。
[root@db01 ~]# useradd -M -s /sbin/nologin -r mysql
3. 安装依赖包
之所以要安装依赖包,主要的目的是提前安装好所有需要用到的插件包,防止MySQL在安装之后无法使用。
[root@db01 ~]# yum install -y ncurses-devel libaio-devel gcc gcc-c++ numactl libaio glibc cmake autoconf
4. 解压缩MySQL 压缩包
[root@db01 ~]# tar -xf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@db01 local]# ln -s /usr/local/mysql-5.7.35-linux-glibc2.12-x86_64/ /usr/local/mysql
5. 将MySQL目录授权
之所以需要授权,主要的目的是为了让MySQL启动的时候不至于应为权限的问题导致启动报错。
[root@db01 local]# chown -R mysql.mysql /usr/local/mysql/
[root@db01 local]# chown -R mysql.mysql /usr/local/mysql-5.7.36-linux-glibc2.12-x86_64/
6. MySQL数据库初始化
在启动之前,我们需要将MySQL初始化一下,生成一些默认的配置和数据库以及数据库密码等。
- 创建保存数据的目录
[root@db01 local]# mkdir /mysql_data
[root@db01 local]# chown -R mysql.mysql /mysql_data/
[root@db01 local]# touch /var/log/mysqld.log
[root@db01 local]# chown -R mysql.mysql /var/log/mysqld.log
- 初始化
[root@db01 local]# cd /usr/local/mysql
[root@db01 mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/mysql_data
7. 编写配置文件
在启动MySQL之前,我们需要编写一个默认的配置文件/etc/my.cnf。
[root@db01 mysql]# vim /etc/my.cnf
[root@db01 mysql]# cat /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/mysql_data
port=3306
socket=/usr/local/mysql/mysql.sock
character-set-server=utf8mb4
log-error=/var/log/mysqld.log
pid-file=/tmp/mysqld.pid
[mysql]
socket=/usr/local/mysql/mysql.sock
[client]
socket=/usr/local/mysql/mysql.sock
8. 生成启动脚本,并启动mysql
将MySQL的启动脚本复制到对应的目录,为启动做准备。
basedir=/usr/local/mysql
datadir=/mysql_data
9. 启动并,测试
所有的准备工作做完之后,我们就可以启动了,然后登录到数据库,修改默认的密码。一切都做完之后就可以正常运行了。
[root@db01 ~]# cd /usr/local/mysql/bin
[root@db01 bin]# cp mysql /usr/bin/
[root@db01 bin]# mysql -uroot -pgoUaahghs8?r
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.36
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>
10. 加入Systemd管理
为了更方便使用,我们将MySQL服务加入到Systemd之中,方便统一管理。
# 1.配置system管理MySQL
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@db01 ~]# systemctl daemon-reload
[root@db01 ~]# systemctl start mysql
[root@db01 ~]# systemctl status mysql
● mysql.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysql.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2022-01-09 12:49:30 CST; 3s ago
Docs: man:mysqld(8)
https://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 2143 (mysqld)
CGroup: /system.slice/mysql.service
└─2143 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
Jan 09 12:49:30 db01 systemd[1]: Started MySQL Server.