• 基于Centos7.6

1.清理MariaBD环境(MariaDB与MySQL可能会有冲突)

rpm -qa |grep mariadb

yum -y remove mariadb-libs

2.路径规划与创建

软件安装目录 /usr/local/mysql/
数据文件存储位置 /data/mysql/date/3306
日志文件存储位置 /data/mysql/logs/3306
binlog文件存储位置 /binlog/3306
缓存目录位置 /data/mysql/tmp/3306

1
2
3
4
5
mkdir -p /usr/local/mysql
mkdir -p /data/mysql/data/3306
mkdir -p /data/mysql/logs/3306
mkdir -p /binlog/3306
mkdir -p /data/mysql/tmp/3306

​ 正式环境软件安装目录、数据文件存储目录、binlog文件目录应该分别放在三个磁盘上

3.创建用户配置权限

1
2
3
useradd mysql -s /sbin/nologin		#创建mysql用户和组,不需要其登录系统
id mysql #查看用户和属组信息
chown -R mysql.mysql /usr/local/mysql /data /binlog

4.下载安装包、依赖包,上传至/tmp,解压、移动、安装依赖包

安装包下载路径:https://downloads.mysql.com/archives/community/

依赖包下载路径:https://pan.baidu.com/s/1t-YUaG9gyDuse9fo1CTdpQ 提取码:aqrt

1
2
3
4
tar zxvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.28-linux-glibc2.12-x86_64/* /usr/local/mysql
rm -rf mysql-5.7.28-linux-glibc2.12-x86_64 #该文件夹为空,删除即可
rpm -ivh libaio-devel-0.3.109-13.el7.x86_64.rpm

5.更改权限,设置环境变量

1
2
cd /usr/local/mysql
chown -R mysql. *
1
vi /etc/profile		#增加如下环境变量参数
1
2
3
4
export PATH=/usr/local/mysql/bin:$PATH
#把安装目录加进去,告诉系统找MySQL命令的时候去这个目录找
source /etc/profile #使配置文件生效
mysql -V #设置正确应该能看到版本

6.MySQL初始化

mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data/3306

1
2
3
4
5
6
7
8
9
 #如下回显表示初始化成功
2021-03-30T17:16:23.673331Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please
use --explicit_defaults_for_timestamp server option (see documentation for more details).2021-03-30T17:16:23.911662Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-03-30T17:16:23.947089Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-03-30T17:16:24.004815Z 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: a7605f01-917b-11eb-a488-000c298178f9.2021-03-30T17:16:24.005892Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_execu
ted' cannot be opened.2021-03-30T17:16:24.418994Z 0 [Warning] CA certificate ca.pem is self signed.
2021-03-30T17:16:24.884481Z 1 [Warning] root@localhost is created with an empty password ! Please c
onsider switching off the --initialize-insecure option.
#使用参数mysqld –initialize有初始化密码,使用参数mysqld –initialize-insecure为空密码

7.修改配置文件

1
2
3
4
5
6
7
8
9
10
11
cat > /etc/my.cnf <<EOF
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/data/3306
server_id=6
port=3306
socket=/tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock
EOF

8.准备MySQL启动脚本

1
2
cd /usr/local/mysql/support-files/
cp mysql.server /etc/init.d/mysqld #拷贝MySQL的启动脚本至系统软件管理目录中

9.启动数据库,修改登录密码

1
2
service mysqld start或systemctl start mysqld
mysql #此时是空密码,输入mysql直接可以进入数据库
1
2
SET password=PASSWORD('root');	#修改密码为root
mysql -uroot -proot #使用新密码登录数据库,至此安装完毕