安装 | MySQL

1、通用指南

  • 安装mysql的操作系统
  • mysql版本和安装包格式
  • 安装包下载地址
  • 验证安装包的完整性
  • 安装布局

1.1、安装mysql的操作系统

  • 安装mysql的操作系统

  • mysql版本和发布类型

  • 安装mysql的操作系统

  • mysql版本和发布类型

常见平台

Oracle Linux / Red Hat / CentOS
Microsoft Windows
Apple
Solaris

其他平台

Canonical
SUSE
Debian
Microsoft Windows Server
Various Linux

1.2、mysql版本和安装包格式

1.2.1、版本

版本类型

类型特点
开发版有新特性,但不适合生产库使用
通用版 / 生产版 / 稳定版稳定,供生产使用

版本命名格式

三个数字 + 一个可选后缀

示例

mysql-8.0.1-dmr

8 代表主版本为8
0 表示次版本为0
1 表示版本的补丁号(数字越大,补丁次数越多,通常也越靠谱)

前两位数字组成系列号,同一组序列号的数据库拥有一个稳定的特性集,故mysql 8.0和mysql 8.2数据库的特性是相近的,而mysql 5.7和mysql 8.0差别会很大

第三位数字是该系mysql的补丁号,一般越大越好

最后一个可选后缀表示mysql的稳定程度(ga > rc > dmr)

可选后缀

后缀名特点含义
dmr不稳定着重引入新特性,若引入的新特性不好使,可能会在下一版本被剔除
rc稳定可能会引入新特性,但重点转移到修改新特性的bug,以及稳定早期引入的老特性
无后缀(ga)最稳定可用于生产的版本

总结

mysql开发的演变过程为:dmr => rc => ga,故ga是最靠谱的

1.2.1、安装包格式

由操作系统决定安装包的格式
通常情况下,使用二进制安装包即可

种类好处坏处
二进制安装包(binary distribution)安装简易不可修改、读取mysql代码
源包(source distribution)较难安装可以读取、修改源码

1.3、安装包下载地址

https://dev.mysql.com/downloads/

平台工具
RPM-based LinuxYum
Debian-based LinuxAPT
SUSE Linux Enterprise ServerSLES

1.4、验证安装包的完整性

使用安装包前,需确保安装包完整且未被篡改
检查完整性的方式

  • MD5校验和
  • GNU Privacy Guard(签名检查)
  • RPM内置的完整性验证机制

注意:
若MD5校验或GPG签名不匹配,则需要更换安装包

1.4.1、MD5校验和

工具名一般为:md5sum

[root@localhost soft]# md5sum mysql-5.7.37-el7-x86_64.tar.gz 
99cfaadf01156f156412070b4fdf46bc  mysql-5.7.37-el7-x86_64.tar.gz

在这里插入图片描述

1.5、安装布局

根据安装方式和操作系统的不同,mysql的安装布局分为以下几类

  • Windows上安装
  • Linux二进制包安装
  • macOS安装
  • 源码安装

以上几类的安装布局不同,即安装后mysql的目录不同

1.5.1、Linux二进制包安装

安装后布局如下:
在这里插入图片描述

2、在Linux上使用二进制包安装mysql

注意:

  • 若以前安装过mysql,则在重新安装mysql前,必须把之前安装的mysql删除干净。
  • MySQL依赖libaio库,若缺失此库,则数据目录初始化以及随后的服务器启动可能会失败
查看libaio库的信息
[root@localhost soft]# yum search libaio
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
=========================================== N/S matched: libaio ===========================================
libaio.x86_64 : Linux-native asynchronous I/O access library

  Name and summary matches only, use "search all" for everything.

若没有该库,则安装
[root@localhost soft]# yum install libaio

2.1、解压并安装

步骤大致如下

2.1.1、创建mysql用户和组

创建mysql组
[root@localhost soft]# groupadd mysql
创建mysql用户,该用户为系统用户,主组为mysql组,且无登录数据库所在主机的权限
[root@localhost soft]# useradd -r -g mysql -s /bin/false mysql

2.1.2、解压安装包

[root@localhost local]# cd /usr/local
[root@localhost local]# tar xvf /soft/mysql-5.7.37-el7-x86_64.tar.gz
创建软链接
[root@localhost local]# ln -s /usr/local/mysql-5.7.37-el7-x86_64 mysql 
将mysql相关程序的路径写入环境变量PATH中,以便使用程序名直接调用程序
[root@localhost mysql]# export PATH=$PATH:/usr/local/mysql/bin

2.1.3、新建目录mysql-files

新建目录,该目录作为系统环境变量secure_file_priv的路径,用于限制导入导入的路径。

[root@localhost local]# cd mysql
[root@localhost mysql]# mkdir mysql-files
[root@localhost mysql]# chown mysql:mysql mysql-files
[root@localhost mysql]# chmod 750 mysql-files

2.1.4、初始化数据库

安装完数据库软件后,需要初始化数据字典(即初始化一个数据库)

创建一个数据库以及一个随机密码的超级用户,并退出。

[root@localhost mysql]# mysqld --initialize --user=mysql
2022-05-08T01:48:10.526243Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-05-08T01:48:11.429024Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-05-08T01:48:11.512296Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-05-08T01:48:11.533502Z 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: eaf6a70d-ce70-11ec-aba2-000c291f971e.
2022-05-08T01:48:11.534546Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-05-08T01:48:12.309815Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-05-08T01:48:12.309828Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-05-08T01:48:12.310292Z 0 [Warning] CA certificate ca.pem is self signed.
2022-05-08T01:48:12.450728Z 1 [Note] A temporary password is generated for root@localhost: l)yIP;nz-4Fv

注意:

可以通过命令行参数或编辑参数文件,控制mysqld服务的使用
mysqld读取的默认参数文件: /etc/my.cnf 或者 /etc/mysql/my.cnf

也可以指定初始化数据目录的路径

mysqld --initialize --user=mysql --basedir=/opt/mysql/mysql --datadir=/opt/mysql/mysql/data

2.1.5、配置安全连接

创建ssl和rsa文件

[root@localhost mysql]# mysql_ssl_rsa_setup

2.1.6、启动数据库服务(mysqld server)

[root@localhost bin]# mysqld_safe --user=mysql &

注意
我的/etc/my.cnf的配置如下

[root@localhost soft]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

如果没有目录/var/log/mariadb,需要新建并赋予权限

[root@localhostsoft]# ll /var/log|grep mariadb drwxr-x---. 2 mysql mysql     25 May 8 11:15 mariadb

若启动数据库时报如下错误

[root@localhost mysql]# mysqld_safe --user=mysql &
[1] 2458
[root@localhost mysql]# 2022-05-08T03:14:00.864673Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.

[1]+  Exit 1                  mysqld_safe --user=mysql

需要创建报错中缺失的文件:/var/log/mariadb/mariadb.log

[root@localhost mysql]# touch /var/log/mariadb/mariadb.log

创建完成后,再次启动数据库

[root@localhost mysql]# mysqld_safe --user=mysql &
[1] 2562
[root@localhost mysql]# 2022-05-08T03:15:56.131000Z mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
2022-05-08T03:15:56.171614Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
2022-05-08T03:15:57.857256Z mysqld_safe mysqld from pid file /var/run/mariadb/mariadb.pid ended

[1]+  Done                    mysqld_safe --user=mysql

检查进程发现并没有mysqld进程,故查看mysqld_safe的日志文件

[root@localhost mysql]# ps -ef|grep mysqld
root       2933   1194  0 11:29 pts/0    00:00:00 grep --color=auto mysqld

[root@localhost mysql]# tail /var/log/mariadb/mariadb.log
2022-05-08T03:23:48.951108Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
2022-05-08T03:23:48.951111Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-05-08T03:23:48.951113Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-05-08T03:23:48.951550Z 0 [Warning] CA certificate ca.pem is self signed.
2022-05-08T03:23:48.951581Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2022-05-08T03:23:48.951795Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2022-05-08T03:23:48.951826Z 0 [Note] IPv6 is available.
2022-05-08T03:23:48.951833Z 0 [Note]   - '::' resolves to '::';
2022-05-08T03:23:48.951847Z 0 [Note] Server socket created on IP: '::'.
2022-05-08T03:23:48.970949Z 0 [ERROR] Can't start server: can't check PID filepath: No such file or directory

由报错知,缺少pid文件,故检查配置文件/etc/my.cnf

[root@localhost mariadb]# cat /etc/my.cnf 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

经过检查,发现没有目录/var/run/mariadb,故无法生成文件/var/run/mariadb/mariadb.pid

[root@localhost mariadb]# mkdir -p /var/run/mariadb
[root@localhost mariadb]# chown mysql:mysql /var/run/mariadb
[root@localhost mariadb]# chmod 750 /var/run/mariadb

创建目录/var/run/mariadb后,再次启动数据库服务

mysqld_safe --user=mysql &

检查

启动mysqld时,自动生成了mariadb.pid文件
[root@localhost mariadb]# cd  /var/run/mariadb
[root@localhost mariadb]# ll
total 4
-rw-r-----. 1 mysql mysql 5 May  8 11:35 mariadb.pid

mysqld_safe的日志无报错
[root@localhost mariadb]# tail /var/log/mariadb/mariadb.log
2022-05-08T03:35:51.770445Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-05-08T03:35:51.771620Z 0 [Warning] CA certificate ca.pem is self signed.
2022-05-08T03:35:51.771758Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2022-05-08T03:35:51.772403Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2022-05-08T03:35:51.772474Z 0 [Note] IPv6 is available.
2022-05-08T03:35:51.772489Z 0 [Note]   - '::' resolves to '::';
2022-05-08T03:35:51.772518Z 0 [Note] Server socket created on IP: '::'.
2022-05-08T03:35:51.783438Z 0 [Note] Event Scheduler: Loaded 0 events
2022-05-08T03:35:51.783678Z 0 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.7.37'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server (GPL)

能看到mysqld进程
[root@localhost mariadb]# ps -ef|grep mysqld
root       2943   1194  0 11:35 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=mysql
mysql      3078   2943  0 11:35 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/var/lib/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root       3109   1194  0 11:36 pts/0    00:00:00 grep --color=auto mysqld

登入登出数据库
自动启停数据库
设置数据库时区

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值