Linux 介绍、安装MySQL 5.7

文章目录

一、MySQL数据库概述

  • MySQL是一种关系型数据库管理系统
  • 由瑞典MySQL AB 公司开发,目前属于Oracle旗下
  • MySQL是C/S架构

二、MySQL数据库版本

  • 社区版:MySQL Community Edition (**GPL)
    1. 可以看做是企业版的“广泛体验版(小白鼠版)”,未经各个专有系统平台的压力测试和性能测试
    2. 基于GPL协议发布,可以随意下载使用
    3. 没有任何官方技术支持服务
  • 企业版:MySQL Enterprise Edition(**commercial)
    1. 提供了比较全面的高级功能、管理工具及技术支持
    2. 安全性、稳定性、可扩展性比较好
  • 集群版:MySQL Cluster CGE(commercial)

扩充:关于版本的命名方式如何定义?

版本说明
α(Alpha)版内测版,内部交流或者专业测试人员测试用。Bug较多,普通用户最好不要安装。
β(Beta)版公测版,专业爱好者大规模测试用,存在一些缺陷,该版本也不适合一般用户安装。
γ ( Gamma )版相当成熟的测试版,与即将发行的正式版相差无几。
Final正式版本
Free自由版本
Release发行版本
Standard标准版本
Mini迷你精简版本,只有最基本的功能
Upgrade升级版本
GA(GenerallyAvailable)开发团队认为该版本是稳定版,可以在较为关键的场合使用。
Retail零售版

三、获取MySQL软件

官网:https://www.mysql.com/
第一步:单击Downloads下载
iShot2023-09-10 18.09.46.png
第二步:找到社区版
iShot2023-09-10 18.12.07.png
第三步:单击进入MySQL社区版下载页面
iShot2023-09-10 18.13.42.png
第四步:根据安装方式的不同自行选择版本、系统等信息

分别是:

  • GLIBC版本(相当于Windows中的绿色版)
  • 源码编译版本(需要源码编译三步走 => 配置 + 编译 + 安装)

iShot2023-09-10 18.31.56.png

四、安装MySQL

安装方式:

  • 使用yum安装(适用于可联通网络的情况下使用)
  • rpm安装(离线安装)
  • glibc版本安装(相当于Windows中的绿色软件)
    • 命名:mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
    • 依赖glibc库,可以安装在通用的Linux系统下
  • 源码包编译安装
    • 命名:mysql-5.7.31.tar.gz,通用的Linux下都可以编译安装。

安装方式的区别

安装方式优点缺点
rpm安装卸载简单可定制性差
glibc可定制性相比rpm包灵活些安装相比rpm包复杂些,需要手动初始化数据库
源码安装可定制性最强,根据需求和功能定制安装麻烦,需要手动初始化数据库

在企业中,数据库的安装很少使用rpm方式,大部分都是基于源码安装以及glibc安装!

YUM 源方式安装

Yum 源仓库:http://repo.mysql.com/

选择所需要的版本,进行下载。

iShot2023-09-11 00.41.01.png

[root@VM-16-10-centos opt]# wget http://repo.mysql.com/mysql57-community-release-el7-9.noarch.rpm

1、安装yum源

使用ls /etc/yum.repos.d/ -l查看系统中的yum源

[root@VM-16-10-centos opt]# ls /etc/yum.repos.d/ -l
total 8
-rw-r--r-- 1 root root 614 Sep  5 16:21 CentOS-Base.repo
-rw-r--r-- 1 root root 230 Sep  5 16:21 CentOS-Epel.repo

每个人的服务器不同查看到的yum源也不同,这并不重要,重要的是观察发现,里面并没有MySQL,因此如果我们直接去安装可能会导致安装失败,因此还需要先使用rpm -ivh mysql57-community-release-el7-9.noarch.rpm指令进行对应的yum源安装

[root@VM-16-10-centos opt]# rpm -ivh mysql57-community-release-el7-9.noarch.rpm 
warning: mysql57-community-release-el7-9.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql57-community-release-el7-9  ################################# [100%]
[root@VM-16-10-centos opt]# 

执行完上一步以后可以使用指令yum list | grep mysql进行检查,查看服务器中是否已经存在mysql相关的文件iShot2023-09-11 00.46.28.png

2、使用yum源一键安装MySQL

使用指令yum install -y mysql-community-server一键安装,虽然这条指令看起来像是只安装了服务,但其实在安装的时候会顺带将组件,库,服务器和客户端一起安装
安装遇到密钥过期

Failing package is: mysql-community-client-5.7.39-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

解决⽅案:
首先执行指令:rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
然后重新安装MySQL:yum install -y mysql-community-server

检查安装
使用指令ls /etc/my.cnf 和which mysqld以及which mysql如果都能查到,那么恭喜你,你的MySQL已经安装成功了
启动MySQL
使用指令systemctl start mysqld启动mysql服务(该服务必须要超管权限启动),此时可以再使用指令查看一下,发现确实有一个mysqld的守护进程被启动了
甚至可以使用netstat -nltp查看mysql的服务端口号(这说明其实它是一种网络服务)

3、登录MySQL

方案一
1.获取临时密码
[root@VM-4-11-centos MySQL]# sudo grep 'temporary password' /var/log/mysqld.log
2023-06-07T02:58:34.085200Z 1 [Note] A temporary password is generated for root@localhost: kQs***7OrS_M

2.使用临时密码登录
[root@VM-4-11-centos MySQL]# mysql -h 127.0.0.1 -P 3306 -u root -p
Enter password: 
方案二

使用vim /etc/my.cnf打开MySQL配置文件,并给最后加上skip-grant-tables (意为跳过密码认证)
配置完以后,为了让其生效,需要将MySQL服务重启,使用systemctl restart mysqld就可以重启了(也可以先用systemctl stop mysqld暂停服务再用systemctl start mysqld启动)。
此时再登陆就不用密码了(遇到输入密码时直接跳过)

-h:指定要连接的 MySQL 数据库所在的主机名或 IP 地址。在这里,127.0.0.1 表示要连接的主机为本地主机,即连接本机上的 MySQL 数据库。不指明-h,默认连上本地服务器搭建的MySQL。
-P:指定要连接的 MySQL 数据库所在的端口号。在这里,3306 表示 MySQL 数据库的默认端口号。不指明-P,默认使用配置文件指明的端口号连接MySQL。
-u:指定要连接 MySQL 数据库的用户名。在这里,root 表示使用 root 用户名连接 MySQL 数据库。
-p:表示连接 MySQL 数据库时需要输入密码。在这里,-p 后面没有指定密码,所以会提示输入密码。
当你输入完整的命令后,按下回车键,会提示输入密码,输入正确的密码后,就可以连接到 MySQL 数据库了

RPM方式安装

1、获取rpm依赖包

第一步:单击Downloads下载
iShot2023-09-10 18.09.46.png
第二步:找到社区版
iShot2023-09-10 18.12.07.png
第三步:单击进入MySQL社区版下载页面
iShot2023-09-10 18.13.42.png
第四步:根据下方信息、选择版本、系统等信息
iShot2023-09-11 00.05.08.png

如果服务器可以联通网络环境,可在服务器直接执行wget命令直接进行下载。
如果服务器处于内网环境,无法联通网络环境,在本机下载后上传至服务器中。

在线下载软件rpm包

[root@VM-16-10-centos ~]# cd /tmp
[root@VM-16-10-centos tmp]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.42-1.el7.x86_64.rpm-bundle.tar

--2023-09-11 00:12:28--  https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.42-1.el7.x86_64.rpm-bundle.tar
Resolving downloads.mysql.com (downloads.mysql.com)... 23.59.130.206, 2600:1417:76:681::2e31, 2600:1417:76:68e::2e31
Connecting to downloads.mysql.com (downloads.mysql.com)|23.59.130.206|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.42-1.el7.x86_64.rpm-bundle.tar [following]
--2023-09-11 00:12:29--  https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.42-1.el7.x86_64.rpm-bundle.tar
Resolving cdn.mysql.com (cdn.mysql.com)... 23.195.85.5, 2600:1417:76:683::1d68, 2600:1417:76:684::1d68
Connecting to cdn.mysql.com (cdn.mysql.com)|23.195.85.5|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 557496320 (532M) [application/x-tar]
Saving to: ‘mysql-5.7.42-1.el7.x86_64.rpm-bundle.tar’

100%[===============================================================================>] 557,496,320 11.8MB/s   in 48s    

2023-09-11 00:13:18 (11.1 MB/s) - ‘mysql-5.7.42-1.el7.x86_64.rpm-bundle.tar’ saved [557496320/557496320]

[root@VM-16-10-centos tmp]# ls
8a6be2f9e39171ef24ae0f7757c5d095          setRps.log
cpuidle_support.log                       set_xps.log
cvm_init.log                              stargate.lock
disable_rt_runtime_share.log              systemd-private-3d61e5d88c45475cb107cf7583d8ba0c-ntpd.service-3V759x
gpudirect_rdma_setup.log                  systemd-private-a410c5a0dcd24bcd845e00141710e244-ntpd.service-hSU1rK
gpudirect_rdma_setup.sh                   tat_agent
mysql-5.7.42-1.el7.x86_64.rpm-bundle.tar  tlinux_xps.log
net_affinity.log                          virtio_blk_affinity.log
nv_driver_install.log                     virtio_blk_affinity_udev.log
nvenc_ai_sdk_install.log                  yddaemon.log
nvenc_ai_sdk_install.sh                   ydeye_install.log
nv_gpu_conf.log
[root@VM-16-10-centos tmp]# 

现在,mysql-5.7.42-1.el7.x86_64.rpm-bundle.tar 已经是下载完成。

2、解压安装包

压缩包中包含MySQL安装所需要的全部rpm依赖包。

[root@VM-16-10-centos tmp]# tar -xvf mysql-5.7.42-1.el7.x86_64.rpm-bundle.tar 
mysql-community-client-5.7.42-1.el7.x86_64.rpm
mysql-community-common-5.7.42-1.el7.x86_64.rpm
mysql-community-devel-5.7.42-1.el7.x86_64.rpm
mysql-community-embedded-5.7.42-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.42-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.42-1.el7.x86_64.rpm
mysql-community-libs-5.7.42-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.42-1.el7.x86_64.rpm
mysql-community-server-5.7.42-1.el7.x86_64.rpm
mysql-community-test-5.7.42-1.el7.x86_64.rpm

[root@VM-16-10-centos tmp]# ls
8a6be2f9e39171ef24ae0f7757c5d095                         net_affinity.log
cpuidle_support.log                                      nv_driver_install.log
cvm_init.log                                             nvenc_ai_sdk_install.log
disable_rt_runtime_share.log                             nvenc_ai_sdk_install.sh
gpudirect_rdma_setup.log                                 nv_gpu_conf.log
gpudirect_rdma_setup.sh                                  setRps.log
mysql-5.7.42-1.el7.x86_64.rpm-bundle.tar                 set_xps.log
mysql-community-client-5.7.42-1.el7.x86_64.rpm           stargate.lock
mysql-community-common-5.7.42-1.el7.x86_64.rpm           systemd-private-3d61e5d88c45475cb107cf7583d8ba0c-ntpd.service-3V759x
mysql-community-devel-5.7.42-1.el7.x86_64.rpm            systemd-private-a410c5a0dcd24bcd845e00141710e244-ntpd.service-hSU1rK
mysql-community-embedded-5.7.42-1.el7.x86_64.rpm         tat_agent
mysql-community-embedded-compat-5.7.42-1.el7.x86_64.rpm  tlinux_xps.log
mysql-community-embedded-devel-5.7.42-1.el7.x86_64.rpm   virtio_blk_affinity.log
mysql-community-libs-5.7.42-1.el7.x86_64.rpm             virtio_blk_affinity_udev.log
mysql-community-libs-compat-5.7.42-1.el7.x86_64.rpm      yddaemon.log
mysql-community-server-5.7.42-1.el7.x86_64.rpm           ydeye_install.log
mysql-community-test-5.7.42-1.el7.x86_64.rpm

3、安装MySQL

rpm -ivh mysql-community-common-5.7.42-1.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-community-libs-5.7.42-1.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-community-libs-compat-5.7.42-1.el7.x86_64.rpm  --force --nodeps
rpm -ivh mysql-community-client-5.7.42-1.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-community-server-5.7.42-1.el7.x86_64.rpm --force --nodeps

在执行rpm依赖包时,可能会出现以下错误:
_安装时候出现 warning: mysql-community-server-5.7.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY _
原因分析:由于yum安装了旧版本的GPG keys造成的,从 RPM 版本 4.1 开始,在安装或升级软件包时会检查软件包的签名。
解决办法:在安装命令之后增加 --force --nodeps 即可解决

glibc版本安装

根据上面信息选择:https://dev.mysql.com/downloads/mysql/5.7.html
进去以后,系统选择Linux - Generic,最后选择64位的点击Download
iShot2023-09-10 17.46.20.png
跳转到下载页面,选择直接下载即可
iShot2023-09-10 17.48.43.png

1、选择一个安装路径

我这里是在usr/local目录下新建一个mysql文件夹

[root@VM-16-10-centos ~]# cd /usr/local
[root@VM-16-10-centos local]# ls
bin  etc  games  include  lib  lib64  libexec  qcloud  sbin  share  src
[root@VM-16-10-centos local]# mkdir mysql
[root@VM-16-10-centos local]# 

2、进入创建好的MySQL目录下

[root@VM-16-10-centos local]# cd /mysql

3、使用远程工具将下载好的压缩包上传到Linux服务器上

[root@VM-16-10-centos mysql]# ls
mysql-5.7.43-linux-glibc2.12-x86_64.tar.gz

4、解压压缩包

[root@VM-16-10-centos mysql]# tar -zxvf mysql-5.7.43-linux-glibc2.12-x86_64.tar.gz 

5、解压完成以后,重命名一下解压后的目录名称

我是因为文件名称稍微有些长,可选操作

[root@VM-16-10-centos mysql]# mv mysql-5.7.43-linux-glibc2.12-x86_64/ mysql-5.7

安装压缩的tar文件二进制文件 分发,在您选择的安装位置解压 (通常/usr/local/mysql)。这将创建 目录如下表所示。

目录内容目录
binmysqld
服务器,客户端和实用程序
docsMySQL手册在信息格式
manUnix手册页
include包含(头)文件
lib
share错误消息,字典,和SQL数据库安装
support-files杂项支持文件

6、创建用户组和用户

# 创建名为mysqlgroup的用户组
[root@VM-16-10-centos mysql]# groupadd mysqlgroup
# 添加用户htt到mysqlgroup用户组
[root@VM-16-10-centos mysql]# useradd -r -g mysqlgroup mysql

7、配置MySQL

7.1、进入到刚才解压的MySQL目录下
[root@VM-16-10-centos mysql]# ls
mysql-5.7  mysql-5.7.43-linux-glibc2.12-x86_64.tar.gz
[root@VM-16-10-centos mysql]# cd mysql-5.7/
[root@VM-16-10-centos mysql-5.7]# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files
7.2、创建一个名为data的目录
[root@VM-16-10-centos mysql-5.7]# mkdir data
[root@VM-16-10-centos mysql-5.7]# ls
bin  data  docs  include  lib  LICENSE  man  README  share  support-files
7.3、给用户组中的mysql用户赋予data目录的权限
[root@VM-16-10-centos mysql-5.7]# chown mysql:mysqlgroup -R /usr/local/mysql/mysql-5.7/data/
7.4、配置my.cnf文件
[root@VM-16-10-centos mysql-5.7]# vim /etc/my.cnf

# 删除my.cnf文件中所有内容,复制如下内容
[mysqld]
# 设置MySQL绑定监听的IP地址,0.0.0.0表示监听所有IP,允许远程连接。
bind-address=0.0.0.0
# 设置MySQL服务的端口号,默认是3306
port=3306
# 指定运行mysqld进程的系统用户为mysql
user=mysql
# 设置MySQL的安装基目录
basedir=/usr/local/mysql/mysql-5.7
# 设置MySQL的数据文件目录
datadir=/usr/local/mysql/mysql-5.7/data
# 设置本地连接时使用的Unix socket文件路径
socket=/tmp/mysql.sock
# 设置错误日志文件路径
log-error=/usr/local/mysql/mysql-5.7/data/mysql.err
# 设置存放进程PID的文件路径
pid-file=/usr/local/mysql/mysql-5.7/data/mysql.pid
# 设置最大连接数,包括TCP和socket连接
max_connections=10000
# 设置单个用户最大并发连接数
max_user_connections=2000
# 设置空闲连接超时时间,单位秒
wait_timeout=200
# 设置服务器默认字符集
character_set_server=utf8mb4
# 禁用symbolic link符号链接支持
symbolic-links=0
# 设置timestamp列默认值声明方式
explicit_defaults_for_timestamp=true
# 表名区分大小写,设置为1表示不区分
lower_case_table_names=1

8、初始化MySQL

8.1、先进入安装目录的bin目录下
[root@VM-16-10-centos ~]# cd /usr/local/mysql/mysql-5.7/bin

[root@VM-16-10-centos bin]# ls
innochecksum       mysqladmin                  mysqld_multi      mysqlpump                  mysqlxtest
lz4_decompress     mysqlbinlog                 mysqld_safe       mysql_secure_installation  perror
myisamchk          mysqlcheck                  mysqldump         mysqlshow                  replace
myisam_ftdump      mysql_client_test_embedded  mysqldumpslow     mysqlslap                  resolveip
myisamlog          mysql_config                mysql_embedded    mysql_ssl_rsa_setup        resolve_stack_dump
myisampack         mysql_config_editor         mysqlimport       mysqltest_embedded         zlib_decompress
my_print_defaults  mysqld                      mysql_install_db  mysql_tzinfo_to_sql
mysql              mysqld-debug                mysql_plugin      mysql_upgrade
8.2、初始化MySQL的数据目录
[root@VM-16-10-centos bin]# ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/mysql-5.7 --datadir=/usr/local/mysql/mysql-5.7/data --user=mysql --initialize

参数详解:

参数名详解
./mysqld启动mysqld服务器程序
–defaults-file=/etc/my.cnf指定配置文件路径为/etc/my.cnf
–basedir=/mysql/mysql-5.7指定MySQL的安装目录
–datadir=/mysql/mysql-5.7/data指定数据目录路径
–user=htt以htt用户身份启动
–initialize初始化数据目录,创建必要的系统表等

如果执行以后控制台报了如下错误:

./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

执行如下命令,安装一下即可:

[root@VM-16-10-centos ~]# yum -y install libaio

再次执行刚才的命令

8.3、查看日志中的初始化密码
[root@VM-16-10-centos bin]# cat /usr/local/mysql/mysql-5.7/data/mysql.err
2023-09-10T12:53:38.716159Z 0 [Warning] InnoDB: New log files created, LSN=45790
2023-09-10T12:53:38.773954Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-09-10T12:53:38.839652Z 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: 0fe79387-4fd9-11ee-b94a-525400b60c59.
2023-09-10T12:53:38.843114Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2023-09-10T12:53:39.702688Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2023-09-10T12:53:39.702701Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2023-09-10T12:53:39.705625Z 0 [Warning] CA certificate ca.pem is self signed.
2023-09-10T12:53:40.232114Z 1 [Note] A temporary password is generated for root@localhost: w5MtalGaKD&W

在输出日志的最后一行,root@localhost: 后面的内容就是初始化密码。

9、启动MySQL

9.1、进入support-files目录下
[root@VM-16-10-centos ~]# cd /usr/local/mysql/mysql-5.7/support-files
[root@VM-16-10-centos support-files]# ls
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
9.2、 运行mysql.server来启动MySQL服务
[root@VM-16-10-centos support-files]# sudo ./mysql.server start
Starting MySQL. SUCCESS! 

至此,MySQL数据库启动成功!

10、登录并配置数据库

10.1、进入MySQL的bin目录
[root@VM-16-10-centos support-files]# cd /usr/local/mysql/mysql-5.7/bin
[root@VM-16-10-centos bin]# ls
innochecksum       mysqladmin                  mysqld_multi      mysqlpump                  mysqlxtest
lz4_decompress     mysqlbinlog                 mysqld_safe       mysql_secure_installation  perror
myisamchk          mysqlcheck                  mysqldump         mysqlshow                  replace
myisam_ftdump      mysql_client_test_embedded  mysqldumpslow     mysqlslap                  resolveip
myisamlog          mysql_config                mysql_embedded    mysql_ssl_rsa_setup        resolve_stack_dump
myisampack         mysql_config_editor         mysqlimport       mysqltest_embedded         zlib_decompress
my_print_defaults  mysqld                      mysql_install_db  mysql_tzinfo_to_sql
mysql              mysqld-debug                mysql_plugin      mysql_upgrade
10.2、登录MySQL,粘贴上登录密码
[root@VM-16-10-centos bin]# ./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.43

Copyright (c) 2000, 2023, 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.3 修改初始密码
# 这里我修改成root
mysql> SET PASSWORD = PASSWORD('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)
10.4 将root用户的密码过期策略设置为永不过期
mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.00 sec)
10.5、刷新
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
10.6 修改访问权限
10.6.1、访问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> 
10.6.2、打开root用户的远程访问权限,允许任何主机使用root用户连接MySQL数据库
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> 
10.6.3、刷新
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> 

11、设置开机自启

可选操作

11.1、将MySQL的服务脚本创建一个符号链接到系统服务脚本目录下,文件名为mysql
[root@VM-16-10-centos ~]# ln -s /usr/local/mysql/mysql-5.7/support-files/mysql.server /etc/init.d/mysql
[root@VM-16-10-centos ~]# 

参数详解:

参数名称参数解释
ln -s创建一个符号链接
/usr/local/mysql/mysql-5.7/support-files/mysql.serverMySQL数据库自带的服务脚本,用于控制MySQL服务器的启动和停止
/etc/init.d/mysql系统服务脚本存放的位置
11.2、将MySQL的客户端程序mysql创建一个符号链接到系统命令目录/usr/bin下
[root@VM-16-10-centos ~]# ln -s /usr/local/mysql/mysql-5.7/bin/mysql /usr/bin/mysql
[root@VM-16-10-centos ~]# 

参数详解:

参数名称参数解释
ln -s创建一个符号链接
/usr/bin/mysql在系统命令目录下创建mysql符号链接
/usr/local/mysql/mysql-5.7/bin/mysqlMySQL客户端程序mysql的原始路径
10.3、重启MySQL
[root@VM-16-10-centos ~]# service mysql restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL. SUCCESS! 
10.4、给/etc/init.d/mysql文件添加可执行权限
[root@VM-16-10-centos ~]# chmod +x /etc/init.d/mysql
10.5、将mysql服务添加为开机自动启动的服务
[root@VM-16-10-centos ~]# chkconfig --add mysql
10.6、查看服务列表
[root@VM-16-10-centos ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysql           0:off   1:off   2:on    3:on    4:on    5:on    6:off
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off

2-5级别显示为on,表示开机自动启动
到这里将MySQL设置为开机自动启动就设置好了!
其实这里可以直接使用systemctl管理器进行管理。

源码安装

获取源码包

第一步:单击Downloads下载
iShot2023-09-10 18.09.46.png
第二步:找到社区版
iShot2023-09-10 18.12.07.png
第三步:单击进入MySQL社区版下载页面
iShot2023-09-10 18.13.42.png
第四步:按照下方信息选择,下载源码包
iShot2023-09-11 13.32.54.png

注意:需要下载带 Includes Boost Headers 的包

我这里服务器可直接联通网络环境,使用wget命令下载即可

[root@VM-16-10-centos ~]# cd /usr/local
[root@VM-16-10-centos local]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-5.7.42.tar.gz
--2023-09-11 13:39:36--  https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-5.7.42.tar.gz
Resolving downloads.mysql.com (downloads.mysql.com)... 23.5.250.230, 2600:1417:76:681::2e31, 2600:1417:76:68e::2e31
Connecting to downloads.mysql.com (downloads.mysql.com)|23.5.250.230|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://cdn.mysql.com/archives/mysql-5.7/mysql-boost-5.7.42.tar.gz [following]
--2023-09-11 13:39:37--  https://cdn.mysql.com/archives/mysql-5.7/mysql-boost-5.7.42.tar.gz
Resolving cdn.mysql.com (cdn.mysql.com)... 23.44.53.22, 2600:1417:76:684::1d68, 2600:1417:76:683::1d68
Connecting to cdn.mysql.com (cdn.mysql.com)|23.44.53.22|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 53288125 (51M) [application/x-tar-gz]
Saving to: ‘mysql-boost-5.7.42.tar.gz’

100%[===============================================================================>] 53,288,125  9.67MB/s   in 4.4s   

2023-09-11 13:39:42 (11.6 MB/s) - ‘mysql-boost-5.7.42.tar.gz’ saved [53288125/53288125]

[root@VM-16-10-centos local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql-boost-5.7.42.tar.gz  qcloud  sbin  share  src
[root@VM-16-10-centos local]# 

安装依赖库

[root@VM-16-10-centos local]# yum -y install ncurses-devel libaio-devel gcc gcc-c++ glibc cmake autoconf openssl openssl-devel libstdc++*  libtool lrzsz 

创建用户以及相关目录

# 创建 mysql 用户,禁止登录
useradd -M -s /sbin/nologin mysql
# 创建数据目录
mkdir -p /usr/local/mysql/data
# 创建日志目录
mkdir -p /var/log/mysql/
# 创建tmp目录
mkdir -p /usr/local/mysql/tmp
# 创建sock文件
touch /usr/local/mysql/tmp/mysql.sock
# 赋予权限
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /var/log/mysql

解压和编译

[root@VM-16-10-centos local]# tar -zxvf mysql-boost-5.7.42.tar.gz 
[root@VM-16-10-centos local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  mysql-5.7.42  mysql-boost-5.7.42.tar.gz  qcloud  sbin  share  src
[root@VM-16-10-centos local]# cd mysql-5.7.42/
[root@VM-16-10-centos mysql-5.7.42]# ls
boost           configure.cmake      INSTALL              LICENSE     plugin            sql            unittest
BUILD           dbug                 libbinlogevents      man         rapid             sql-common     VERSION
client          Docs                 libbinlogstandalone  mysql-test  README            storage        vio
cmake           Doxyfile-perfschema  libmysql             mysys       regex             strings        win
CMakeLists.txt  extra                libmysqld            mysys_ssl   scripts           support-files
config.h.cmake  include              libservices          packaging   source_downloads  testclients
[root@VM-16-10-centos mysql-5.7.42]# cd boost/
[root@VM-16-10-centos boost]# ls
boost_1_59_0

注意:-DWITH_BOOST=/usr/local/src/mysql-版本号/boost/boost_版本,有可能boost版本不一定是1.59,那么需要自己查看一下。

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/tmp/mysql.sock \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/mysql-5.7.42/boost/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

上面的命令要在/usr/local/mysql-5.7.42目录下执行。
出现下面的结果就表示执行成功了。

-- INSTALL mysqlclient.pc lib/pkgconfig
-- Skipping deb packaging on unsupported platform .
-- CMAKE_BUILD_TYPE: RelWithDebInfo
-- COMPILE_DEFINITIONS: _GNU_SOURCE;_FILE_OFFSET_BITS=64;HAVE_CONFIG_H
-- CMAKE_C_FLAGS: -fPIC -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing  -Wall -Wextra -Wformat-security -Wvla -Wwrite-strings -Wdeclaration-after-statement
-- CMAKE_CXX_FLAGS: -fPIC -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing  -Wall -Wextra -Wformat-security -Wvla -Woverloaded-virtual -Wno-unused-parameter
-- CMAKE_CXX_FLAGS_DEBUG: -DENABLED_DEBUG_SYNC -DSAFE_MUTEX -g
-- CMAKE_CXX_FLAGS_RELWITHDEBINFO: -ffunction-sections -fdata-sections -O3 -g -DNDEBUG
-- CMAKE_CXX_FLAGS_RELEASE: -ffunction-sections -fdata-sections -O3 -DNDEBUG
-- CMAKE_CXX_FLAGS_MINSIZEREL: -ffunction-sections -fdata-sections -Os -DNDEBUG
-- CMAKE_C_LINK_FLAGS: 
-- CMAKE_CXX_LINK_FLAGS: 
-- CMAKE_EXE_LINKER_FLAGS  
-- CMAKE_MODULE_LINKER_FLAGS  
-- CMAKE_SHARED_LINKER_FLAGS  
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/mysql-5.7.42

编译和安装

编译的时间可能会有些长

iShot2023-09-11 13.50.28.png

初始化

**1. initialize **

[root@VM-16-10-centos mysql-5.7.42]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2023-09-11T06:40:52.819165Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-09-11T06:40:53.114638Z 0 [Warning] InnoDB: New log files created, LSN=45790
2023-09-11T06:40:53.195438Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-09-11T06:40:53.217213Z 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: 275e2cf3-506e-11ee-acc4-525400b60c59.
2023-09-11T06:40:53.227839Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2023-09-11T06:40:53.383331Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2023-09-11T06:40:53.383347Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2023-09-11T06:40:53.383844Z 0 [Warning] CA certificate ca.pem is self signed.
2023-09-11T06:40:53.464127Z 1 [Note] A temporary password is generated for root@localhost: T4:Da3azA8,h

其中最行一行输出的尾部部分root@localhost: ,就是登录数据库时所需的密码。

创建配置文件

[root@VM-16-10-centos mysql-5.7.42]# vim /etc/my.cnf

[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/tmp/mysql.sock
pid-file=/usr/local/mysql/tmp/mysqld.pid
port=3306
character-set-server=utf8
log-error=/var/log/mysql/mysqld.log
 
 
[mysql]
default-character-set=utf8
socket=/usr/local/mysql/tmp/mysql.sock
 
[client]
default-character-set=utf8
socket=/usr/local/mysql/tmp/mysql.sock

启动测试

1. 启动

# 启动
[root@VM-16-10-centos mysql-5.7.42]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.Logging to '/var/log/mysql/mysqld.log'.
 SUCCESS!

** 2. 登录**

[root@VM-16-10-centos mysql-5.7.42]# /usr/local/mysql/bin/mysql -uroot -p'T4:Da3azA8,h'
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.42

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

3. 修改密码

mysql> SET PASSWORD = PASSWORD('rootpwd');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

4. 查询版本和编码格式

select version();
show variables like '%character%';

5. 开启root远程登录

use mysql;
select host,user,plugin from user;
 
# if plugin == caching_sha2_password
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'rootpwd';
update user set host = '%' where user = 'root';
flush privileges;
 
# if plugin == mysql_native_password 
update user set host = '%' where user = 'root';
flush privileges;

配置环境变量

echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
tail -1 /etc/profile
source /etc/profile
which mysql

mysql -V

注册系统服务与开机自启动

1. 注册系统服务

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/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
systemctl daemon-reload

pkill -9 mysqld
 
systemctl start mysqld
 
systemctl status mysqld

2. 设置开机自启动

systemctl enable mysqld
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值