Linux环境下安装MySQL(RPM包离线安装 & 二进制格式安装)

Linux环境下安装MySQL

  • 1、CentOS7 RPM方式安装MySQL5.7
    • 1)事先从官网/国内镜像站点中下载RPM安装包,上传至服务器:
    • 2)解压安装包
    • 3)安装
    • 4)启动服务并测试
    • 5)登录MySQL
  • 2、CentOS7 通用二进制方式安装MySQL5.7
    • 1)事先从官网/国内镜像站点中下载二进制安装包,上传至服务器:
    • 2)解压安装包
    • 3)创建用户和组(二进制格式默认没有创建用户和组)
    • 4)创建软连接(方便,名字简短)
    • 5)修改mysql-5.7.37-linux-glibc2.12-x86_64目录下所有文件的所属用户和所属组
    • 6)配置环境变量
    • 7)初始化
    • 8)为mysql提供主配置文件
    • 9)修改主配置文件
    • 10)为mysql提供sysv服务脚本
    • 11)启动MySQL
    • 12)登录MySQL

1、CentOS7 RPM方式安装MySQL5.7

1)事先从官网/国内镜像站点中下载RPM安装包,上传至服务器:

[root@localhost ~]# wget -c https://repo.huaweicloud.com/mysql/Downloads/MySQL-5.7/mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar
--2022-04-03 22:53:09--  https://repo.huaweicloud.com/mysql/Downloads/MySQL-5.7/mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar
Resolving repo.huaweicloud.com (repo.huaweicloud.com)... 59.39.0.157, 59.39.0.155, 59.39.0.156, ...
Connecting to repo.huaweicloud.com (repo.huaweicloud.com)|59.39.0.157|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 545894400 (521M) [application/octet-stream]
Saving to: ‘mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar’

100%[=================================================================================================================================>] 545,894,400 5.43MB/s   in 1m 49s 

2022-04-03 22:54:58 (4.77 MB/s) - ‘mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar’ saved [545894400/545894400]

[root@localhost ~]# ll		//查看刚下载的安装包
total 533104
-rw-------. 1 root root      1392 Mar 26 17:20 anaconda-ks.cfg
-rw-r--r--  1 root root 545894400 Dec 16 10:16 mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar

2)解压安装包

[root@localhost ~]# tar xf mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar 
[root@localhost ~]# ll mysql-community-*		//查看解压后的情况
-rw-r--r-- 1 7155 31415  26670472 Dec 16 10:01 mysql-community-client-5.7.37-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415    318884 Dec 16 10:01 mysql-community-common-5.7.37-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415   4122424 Dec 16 10:01 mysql-community-devel-5.7.37-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415  47760004 Dec 16 10:02 mysql-community-embedded-5.7.37-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415  23316972 Dec 16 10:02 mysql-community-embedded-compat-5.7.37-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 132204460 Dec 16 10:02 mysql-community-embedded-devel-5.7.37-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415   2474780 Dec 16 10:02 mysql-community-libs-5.7.37-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415   1264836 Dec 16 10:02 mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 182274360 Dec 16 10:02 mysql-community-server-5.7.37-1.el7.x86_64.rpm
-rw-r--r-- 1 7155 31415 125477564 Dec 16 10:03 mysql-community-test-5.7.37-1.el7.x86_64.rpm

/**
注:
mysql-community-client		客户端
mysql-community-devel		开发库
mysql-community-common		服务端和客户端的公共文件
mysql-community-embedded	嵌入式
mysql-community-serve		服务端
mysql-community-test		测试组件
mysql-community-libs		共享库
mysql-community-libs-compat	MySQL之前版本的共享兼容库
**/

3)安装

//先移除mariadb相关软件包
[root@localhost ~]# yum remove mariadb-libs

[root@localhost ~]# rpm -ivh mysql-community-client-5.7.37-1.el7.x86_64.rpm mysql-community-common-5.7.37-1.el7.x86_64.rpm mysql-community-libs-5.7.37-1.el7.x86_64.rpm mysql-community-server-5.7.37-1.el7.x86_64.rpm 

4)启动服务并测试

[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl status mysqld.service 
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2022-04-03 23:14:37 CST; 11s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 1592 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 1542 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 1595 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─1595 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Apr 03 23:14:33 localhost.localdomain systemd[1]: Starting MySQL Server...
Apr 03 23:14:37 localhost.localdomain systemd[1]: Started MySQL Server.

//获取MySQL初始化密码
[root@localhost ~]# cat /var/log/mysqld.log | grep password
2022-04-03T15:14:34.723421Z 1 [Note] A temporary password is generated for root@localhost: BSck)kuHr6Bd

5)登录MySQL

[root@localhost ~]# mysql -uroot -p
Enter password: 	//此密码为刚初始化获取的临时密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37

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

2、CentOS7 通用二进制方式安装MySQL5.7

1)事先从官网/国内镜像站点中下载二进制安装包,上传至服务器:

[root@localhost ~]# wget -c https://repo.huaweicloud.com/mysql/Downloads/MySQL-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
--2022-04-03 23:52:17--  https://repo.huaweicloud.com/mysql/Downloads/MySQL-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
Resolving repo.huaweicloud.com (repo.huaweicloud.com)... 59.39.0.153, 59.39.0.155, 59.39.0.156, ...
Connecting to repo.huaweicloud.com (repo.huaweicloud.com)|59.39.0.153|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 666603757 (636M) [application/octet-stream]
Saving to: ‘mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz’

100%[=================================================================================================================================>] 666,603,757 3.52MB/s   in 2m 14s 

2022-04-03 23:54:32 (4.73 MB/s) - ‘mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz’ saved [666603757/666603757]

[root@localhost ~]# ll		//查看刚下载的安装包
total 650988
-rw-------. 1 root root      1392 Mar 26 17:20 anaconda-ks.cfg
-rw-r--r--  1 root root 666603757 Nov 30 03:55 mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
ar

2)解压安装包

[root@localhost ~]# tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/	
//-C表示指定解压目录为/usr/local/
[root@localhost ~]# ll /usr/local/		//查看解压后的情况
total 0
drwxr-xr-x. 2 root root   6 Apr 11  2018 bin
drwxr-xr-x. 2 root root   6 Apr 11  2018 etc
drwxr-xr-x. 2 root root   6 Apr 11  2018 games
drwxr-xr-x. 2 root root   6 Apr 11  2018 include
drwxr-xr-x. 2 root root   6 Apr 11  2018 lib
drwxr-xr-x. 2 root root   6 Apr 11  2018 lib64
drwxr-xr-x. 2 root root   6 Apr 11  2018 libexec
drwxr-xr-x  9 root root 129 Apr  3 23:55 mysql-5.7.37-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 Apr 11  2018 sbin
drwxr-xr-x. 5 root root  49 Mar 26 17:17 share
drwxr-xr-x. 2 root root   6 Apr 11  2018 src

3)创建用户和组(二进制格式默认没有创建用户和组)

[root@localhost ~]# groupadd -r -g 306 mysql	//-r指定为系统组,-g创建组号为306(组号随意自定义)
[root@localhost ~]# useradd -g 306 -r -u 306 -s /bin/false mysql	  //-r指定为系统用户,-g加入组号306,-u创建用户id为306,“-s /bin/false”表示该用户不需要登录

4)创建软连接(方便,名字简短)

[root@localhost local]# ln -sv mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql
‘/usr/local/mysql’ -> ‘mysql-5.7.37-linux-glibc2.12-x86_64’
[root@localhost local]# ll		//查看软连接情况
total 0
drwxr-xr-x. 2 root root   6 Apr 11  2018 bin
drwxr-xr-x. 2 root root   6 Apr 11  2018 etc
drwxr-xr-x. 2 root root   6 Apr 11  2018 games
drwxr-xr-x. 2 root root   6 Apr 11  2018 include
drwxr-xr-x. 2 root root   6 Apr 11  2018 lib
drwxr-xr-x. 2 root root   6 Apr 11  2018 lib64
drwxr-xr-x. 2 root root   6 Apr 11  2018 libexec
lrwxrwxrwx  1 root root  35 Apr  4 00:00 mysql -> mysql-5.7.37-linux-glibc2.12-x86_64
drwxr-xr-x  9 root root 129 Apr  3 23:55 mysql-5.7.37-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 Apr 11  2018 sbin
drwxr-xr-x. 5 root root  49 Mar 26 17:17 share
drwxr-xr-x. 2 root root   6 Apr 11  2018 src

5)修改mysql-5.7.37-linux-glibc2.12-x86_64目录下所有文件的所属用户和所属组

[root@localhost local]# chown -R mysql:mysql /usr/local/mysql/*
*/	//此处有显示问题,需要闭合上方语句末尾的注释符“/*”,与正常命令无关
[root@localhost local]# ll mysql-5.7.37-linux-glibc2.12-x86_64/		//查看更改情况
total 272
drwxr-xr-x  2 mysql mysql   4096 Apr  3 23:55 bin
drwxr-xr-x  2 mysql mysql     55 Apr  3 23:55 docs
drwxr-xr-x  3 mysql mysql   4096 Apr  3 23:55 include
drwxr-xr-x  5 mysql mysql    230 Apr  3 23:55 lib
-rw-r--r--  1 mysql mysql 259253 Nov 30 04:09 LICENSE
drwxr-xr-x  4 mysql mysql     30 Apr  3 23:55 man
-rw-r--r--  1 mysql mysql    566 Nov 30 04:09 README
drwxr-xr-x 28 mysql mysql   4096 Apr  3 23:55 share
drwxr-xr-x  2 mysql mysql     90 Apr  3 23:55 support-files

6)配置环境变量

【可以先配,也可以后配,先配可以在初始化时路径写短一点(原本需要完整写出路径mysql/bin/mysqld,现在路径可以直接写mysqld),仅此而已】

[root@localhost ~]# vim /etc/profile.d/mysql.sh
[root@localhost ~]# cat /etc/profile.d/mysql.sh 
export PATH=$PATH:/usr/local/mysql/bin

[root@localhost ~]# source /etc/profile.d/mysql.sh 		//执行shell脚本

7)初始化

[root@localhost ~]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
//--initialize表示初始化 --user表示用户为mysql --basedir表示指定基本目录 --datadir表示指定数据目录
2022-04-04T01:13:57.988521Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-04-04T01:13:58.153281Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-04-04T01:13:58.246230Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-04-04T01:13:58.311390Z 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: 811a524e-b3b4-11ec-8062-000c294d6b8d.
2022-04-04T01:13:58.312521Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-04-04T01:13:58.792680Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-04-04T01:13:58.792694Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-04-04T01:13:58.793824Z 0 [Warning] CA certificate ca.pem is self signed.
2022-04-04T01:13:58.890990Z 1 [Note] A temporary password is generated for root@localhost: *+/.j9AB).SR
//注:最下方末尾处为MySQL临时密码,需要记住

[root@localhost ~]# ll /usr/local/mysql/data/
total 110660
-rw-r----- 1 mysql mysql       56 Apr  4 09:13 auto.cnf
-rw------- 1 mysql mysql     1676 Apr  4 09:13 ca-key.pem
-rw-r--r-- 1 mysql mysql     1112 Apr  4 09:13 ca.pem
-rw-r--r-- 1 mysql mysql     1112 Apr  4 09:13 client-cert.pem
-rw------- 1 mysql mysql     1676 Apr  4 09:13 client-key.pem
-rw-r----- 1 mysql mysql      436 Apr  4 09:13 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 Apr  4 09:14 ibdata1
-rw-r----- 1 mysql mysql 50331648 Apr  4 09:14 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Apr  4 09:13 ib_logfile1
drwxr-x--- 2 mysql mysql     4096 Apr  4 09:13 mysql
drwxr-x--- 2 mysql mysql     8192 Apr  4 09:13 performance_schema
-rw------- 1 mysql mysql     1676 Apr  4 09:13 private_key.pem
-rw-r--r-- 1 mysql mysql      452 Apr  4 09:13 public_key.pem
-rw-r--r-- 1 mysql mysql     1112 Apr  4 09:13 server-cert.pem
-rw------- 1 mysql mysql     1676 Apr  4 09:13 server-key.pem
drwxr-x--- 2 mysql mysql     8192 Apr  4 09:13 sys
//初始化后可以看出data目录生成且里面文件也自动生成

8)为mysql提供主配置文件

[root@localhost ~]# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
vim /etc/my.cnf
//一般是需要拷贝上方命令路径的cnf文件,但发现当前系统中没有该文件,因此决定拷贝其它系统已经做好的cnf文件
[root@localhost ~]# scp 192.168.25.133:/etc/my.cnf /etc/
The authenticity of host '192.168.25.133 (192.168.25.133)' can't be established.
ECDSA key fingerprint is SHA256:1/h5W6MDWcERvpmdi4MjevXjD/xOrnxqRtsuzvhihJE.
ECDSA key fingerprint is MD5:54:da:bf:c3:e1:82:0b:a5:09:03:95:f2:af:1d:2d:e8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.25.133' (ECDSA) to the list of known hosts.
root@192.168.25.133's password: 
my.cnf                                                                                                                                   100%  960   493.6KB/s   00:00    

9)修改主配置文件

[root@localhost ~]# vim /etc/my.cnf	
/**
修改如下行指定mysql数据文件的存放位置:
datadir = /usr/local/mysql/data
修改如下行指定mysql套接字文件的存放位置:
socket=/tmp/mysql.sock
修改如下行指定mysql进程id文件的存放位置:
pid-file=/usr/local/mysql/data/mysqld.pid
**/

[root@localhost ~]# cat /etc/my.cnf		//下方为修改后结果显示
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/usr/local/mysql/data/mysqld.pid

10)为mysql提供sysv服务脚本

[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# ll /etc/init.d/
total 52
-rw-r--r--. 1 root root 18281 May 22  2020 functions
-rwxr-xr-x  1 root root 10576 Apr  4 09:42 mysqld
-rwxr-xr-x. 1 root root  4569 May 22  2020 netconsole
-rwxr-xr-x. 1 root root  7928 May 22  2020 network
-rw-r--r--. 1 root root  1160 Oct  2  2020 README
//由于观察到上方mysqld拥有执行(x)权限,因此不需要另外添加权限

11)启动MySQL

[root@localhost ~]# chkconfig --add mysqld		//添加为系统服务
[root@localhost ~]# chkconfig mysqld on		//开机自启动
[root@localhost ~]# systemctl start mysqld		//启动MySQL服务
[root@localhost ~]# systemctl status mysqld		//查看MySQL状态
● mysqld.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
   Active: active (running) since Mon 2022-04-04 09:47:01 CST; 1min 33s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 2226 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/mysqld.service
           ├─2236 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/mysqld.pid
           └─2388 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-err...

Apr 04 09:47:00 localhost.localdomain systemd[1]: Starting LSB: start and stop MySQL...
Apr 04 09:47:01 localhost.localdomain mysqld[2226]: Starting MySQL. SUCCESS!
Apr 04 09:47:01 localhost.localdomain systemd[1]: Started LSB: start and stop MySQL.

12)登录MySQL

[root@localhost ~]# mysql -uroot -p
Enter password: 	//此密码为刚初始化获取的临时密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37

Copyright (c) 2000, 2022, 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> 
//登录成功

你可能感兴趣的:(linux,mysql)