使用Linux-Generic 下的源码包进行安装:

一、安装前的准备工作:

数据库版本:MySQL 5.7
系统:Centos 6/7

1.系统安装好之后需要配置好网卡,关闭防火墙及SELINUX:
关闭Linux防火墙命令:
Centos 6:

[root@local ~] # service iptables stop
[root@local ~] # chkconfig iptables off

Centos 7:

[root@local ~] # systemctl stop firewalld
[root@local ~] # systemctl disable firewalld

修改SELINUX配置:

[root@local ~] # vim /etc/sysconfig/selinux
SELINUX=enforcing

修改为:SELINUX=disabled
修改完成后,保存并退出,然后重启系统。
如果当前系统无法重启,使用如下命令修改当前系统环境:

[root@local ~] # getenforce
Enforcing
[root@local ~] # setenforce 0
[root@local ~] # getenforce
Permissive

2.新建数据库执行文件目录(后面会把编译好的mysql程序安装到这个目录,如果想安装到其他目录可以自定义)

[root@local ~] # mkdir -p /data

3.配置yum源,安装mysql的依赖包:

[root@local ~] #  yum install -y cmake make gcc gcc-c++ libaio ncurses ncurses-devel numactl

本次使用本地yum源,配置方式参考:Linux系统配置本地yum源(Centos 6.5)

二、开始安装:

1、下载MySQL安装包,本次以版本5.7.26为例:
地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
下载安装包:

其中需要注意的是,下载的是Linux-Generic 下的源码包,我觉的是类似里面安装的包,与在Souce Code中的不一样,此方法不用cmake编译,免去了一些繁琐的步骤。

2、解压后,将mysql-5.7.26-linux-glibc2.12-x86_64里面的东西放到 /data 文件夹下(或者新的磁盘下,一般会放到新磁盘下进行管理)

[root@local ~] # tar -zxvf  mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz

3、添加系统mysql组和mysql用户
添加系统mysql组:

[root@local ~] # groupadd mysql

添加mysql用户:

[root@local ~] # useradd -r -g mysql mysql (添加完成后可用id mysql查看)

4、安装:

修改当前目录拥有者为mysql用户:

[root@local ~] # mkdir /data
[root@local ~] # chown mysql:mysql  /data
[root@local ~] # cd /data
[root@local data] # mkdir -p mysql/app  mysql/log/binlog  mysql/mysql_data
[root@local data] # chown -R mysql:mysql  mysql
[root@local data] # mv /root/mysql-5.7.26-linux-glibc2.12-x86_64 /data/mysql/app/mysql5.7

生产环境可以使用软链接部署:

[root@local data] # cd mysql
[root@local data] # ln -s /data/mysql/app/mysql5.7 /data/mysql/mysql

这样便于MySQL升级,减少修改与MySQL相关的环境变量。

初始化数据库:

[root@local ~] # cd /data/mysql/mysql/bin
[root@local /data/mysql/mysql/bin]  # ./mysqld --initialize --user=mysql --basedir=/data/mysql/mysql --datadir=/data/mysql/mysql_data

注意:如果安装MySQL8版本,参数文件中的参数“lower_case_table_names=1”也需要在MySQL初始化操作中特别指定,例如:
[root@local /data/mysql/mysql/bin] # ./mysqld --initialize --user=mysql --basedir=/data/mysql/mysql --datadir=/data/mysql/mysql_data --lower_case_table_names=1
并且在参数文件中写明“lower_case_table_names=1”,不写默认还是等于“0”,启动MySQL会报错。

注意:
–basedir指定了mysql的家目录
–datadir指定了安装目录
回车后,如果命令成功,则会在右下角root@localhost后产生的登录mysql的默认密码密码,一定要记录下来。

5、执行以下命令创建RSA private key ,

[root@local /data/mysql/mysql/bin]  # ./mysql_ssl_rsa_setup --datadir=/data/mysql/mysql_data

此程序创建SSL证书和密钥文件以及RSA密钥对文件,以支持使用SSL的安全连接,以及在未加密连接上使用RSA的安全密码交换(如果这些文件丢失的话)。mysql_ssl_rsa_setup也可以用于创建新的SSL文件,如果现有的SSL文件已经过期。

命令执行后会在执行的目录/data/mysql/mysql_data下生成如下文件:

ca.pem               Self-signed CA certificate
ca-key.pem           CA private key
server-cert.pem      Server certificate
server-key.pem       Server private key
client-cert.pem      Client certificate
client-key.pem       Client private key

6、在/etc目录下配置MySQL数据库的配置文件my.cnf,添加如下初始配置参数,生产环境根据实际情况添加或修改相关参数:

[root@local ~] # vim /etc/my.cnf    (直接把下面内容复制上就行)
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8
basedir=/data/mysql/mysql
datadir=/data/mysql/mysql_data
socket=/tmp/mysql.sock
# sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#1不分大小写;0分大小写
lower_case_table_names = 1
log-error=/data/mysql/log/mysqld.log 
pid-file=/data/mysql/mysql_data/mysqld.pid

如果是用Centos 7系统可能会自带或以前安装过MySQL可能会在/etc/有my.cnf配置文件,可以提前卸载掉已安装的MySQL或mariaDB:

[root@liandoyun ~]# rpm -qa|grep mysql
[root@liandoyun ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.60-1.el7.x86_64
[root@liandoyun ~]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7.x86_64

(注意:目录log和日志文件mysqld.log需要手动创建并修改属主属组)

MySQL参数文件读取位置(Linux平台):
/etc/my.cnf ----- 全局选项,默认位置。
$MYSQL_HOME/my.cnf ---- MySQL安装目录下。
defaults-extra-file ---- 启动MySQL时用–defaults-extra-file=path 指定文件位置。
~/.my.cnf ---- 用户家目录下隐藏文件 .my.cnf。

7、添加开机启动

[root@local ~] # cp /data/mysql5.7/support-files/mysql.server  /etc/init.d/mysqld

8、修改

[root@local ~] #  vim /etc/init.d/mysqld   
#添加路径 在46行 
     basedir=/data/mysql5.7
     datadir=/data/mysql5.7/data

9、启动mysql
Centos 6:

[root@local ~] # service mysqld start

Centos 7:

[root@local ~] # /etc/init.d/mysqld start

注意:如果启动失败,例如:Starting MySQL… ERROR! The server quit without updating PID file,一般是目录访问权限导致。注意检查!!!

加入开机起动
Centos 6:

[root@local ~] # chkconfig --add mysqld

Centos 7配置较为复杂,暂不配置。

10、修改系统环境配置,可以访问mysql相关命令,不通过cd到bin目录就可以执行:

[root@local ~] # vim /etc/profile
#添加一下参数:
export PATH=/data/mysql/mysql/bin:$PATH

11、修改密码
使用默认密码登录MySQL:

[root@local ~] # mysql  -uroot  -p"q5sj.Hes*"

修改root密码,并只能本地访问:

mysql> alter user 'root'@'localhost' identified by 'root123456';   
mysql> flush privileges;    #刷新权限

如果想创建新用户,并能远程访问,可以使用下面语句:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;  
mysql> flush privileges;

至此,使用源码安装MySQL完成!

三、升级MySQL8.0

1.下载MySQL8.0安装包,同样是Generic免安装。然后解压指定位置。

[root@local ~]# xz -d mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz
[root@local ~]# tar -xvf mysql-8.0.23-linux-glibc2.12-x86_64.tar
[root@local ~]# mv mysql-8.0.23-linux-glibc2.12-x86_64 /data/mysql8
[root@local ~]# cd /data
[root@local data]# chown -R mysql:mysql mysql8/

2.更改配置文件my.cnf

MySQL8.0版本参数文件有所更改,主要涉及欧sql_mode、basedir、密码认证插件和字符集设置等,尤其是生产环境涉及很多的参数可能会在MySQL8中废弃,要确认一下,可参考MySQL8.0官方文档:MySQL8中添加、弃用、移除的选项和参数变量

3.执行升级
升级前建议备份全库,然后再按照步骤进行正式升级:
(1)登陆数据库执行一下操作,并关闭4.7版本的数据库。

mysql> select version();
+------------+
| version()  |
+------------+
|  5.7.33    |
+------------+
1 row in set (0.00 sec)

mysql> show variables like 'innodb_fast_shutdown';
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| innodb_fast_shutdown | 1     |
+----------------------+-------+
1 row in set (0.00 sec)

# 确保数据都刷到硬盘上,更改成0
mysql> set global innodb_fast_shutdown=0;
Query OK, 0 rows affected (0.00 sec)

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

mysql> exit
Bye

(2)执行数据库升级(后台执行,执行后会启动8.0版本数据库)。查看数据库日志中升级情况是否报错。

[root@centos ~]# /data/mysql8/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql & 
[1] 23333
[root@centos ~]# tailf /data/mysql_data/log/mysqld.log
2021-06-23T02:04:44.835267Z 0 [System] [MY-010116] [Server] /data/app/mysql8/bin/mysqld (mysqld 8.0.23) starting as process 20081
2021-06-23T02:04:44.848333Z 0 [ERROR] [MY-010340] [Server] Error message file '/data/app/mysql5.7/share/english/errmsg.sys' had only 1121 error messages, but it should contain at least 5358 error messages. Check that the above file is the right version for this program!
2021-06-23T02:04:44.866024Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2021-06-23T02:04:44.917118Z 1 [System] [MY-011012] [Server] Starting upgrade of data directory.
2021-06-23T02:04:44.917458Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-06-23T02:04:46.689769Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-06-23T02:04:47.356183Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /tmp/mysqlx.sock
2021-06-23T02:04:48.751924Z 2 [System] [MY-011003] [Server] Finished populating Data Dictionary tables with data.
2021-06-23T02:04:49.882061Z 5 [System] [MY-013381] [Server] Server upgrade from '50700' to '80023' started.
2021-06-23T02:04:56.492935Z 5 [System] [MY-013381] [Server] Server upgrade from '50700' to '80023' completed.
2021-06-23T02:04:56.713761Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2021-06-23T02:04:56.718191Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2021-06-23T02:04:56.784343Z 0 [System] [MY-010931] [Server] /data/app/mysql8/bin/mysqld: ready for connections. Version: '8.0.23'  socket: '/tmp/mysql.sock'  port: 1106  MySQL Community Server - GPL.
2021-06-23T02:33:14.561198Z 0 [System] [MY-010116] [Server] /data/mysql/bin/mysqld (mysqld 8.0.23) starting as process 20448
2021-06-23T02:33:14.565447Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2021-06-23T02:33:14.582811Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-06-23T02:33:15.008075Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-06-23T02:33:15.166554Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /tmp/mysqlx.sock
2021-06-23T02:33:15.196208Z 0 [System] [MY-010229] [Server] Starting XA crash recovery...
2021-06-23T02:33:15.220334Z 0 [System] [MY-010232] [Server] XA crash recovery finished.
2021-06-23T02:33:15.312976Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2021-06-23T02:33:15.313630Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2021-06-23T02:33:15.424688Z 0 [System] [MY-010931] [Server] /data/mysql/bin/mysqld: ready for connections. Version: '8.0.23'  socket: '/tmp/mysql.sock'  port: 1106  MySQL Community Server - GPL.

(3)日志没有错误,可以登录数据库中查看升级情况。

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 11
Server version: 8.0.23 MySQL Community Server - GPL

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> select version();
+-----------+
| version() |
+-----------+
| 8.0.23    |
+-----------+
1 row in set (0.00 sec)