centos7二进制安装mysql5.7.28

作者: 风吹蛋生丶

1. 下载相应版本的安装包

当前最新mysql5.7版本为 mysql5.7.28
官网链接: https://dev.mysql.com/downloads/mysql/
百度网盘链接: https://pan.baidu.com/s/1opAZZ_v6qysV0UJmjKTeLw
提取码: 9k5y
获取文件名为:  mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz

部署脚本: https://pan.baidu.com/s/1GiOa_gt9kdN2N-aKtUVilw
提取码: gneu
把脚本和压缩包放置在同一个目录中,可自定义路径等配置参数

2. 安装

当前环境为Centos7.4,如之前安装过

# 删除mariadb依赖包
yum -y remove mariadb-libs-5.5.56-2.el7.x86_64

# 创建mysql用户和组
useradd -UM mysql -s /sbin/nologin
-U 创建与用户同名的组
-M 不创建用户的主目录
-s 指定用户登录后的shell,/sbin/nologin禁止登录系统

# 解压压缩包
cd /opt
tar -xf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz

# 重命名
mv mysql-5.7.28-linux-glibc2.12-x86_64 mysql-5.7.28

# 建立软链接
ln -s mysql-5.7.28 mysql

# 创建所需目录
mkdir -p  mysql/{tmp,errorslogs}  data

# 更改目录所属
chown -R mysql:mysql  mysql/  data/

家目录与数据目录分离建立软链接是为了方便迁移和版本升级

3. 配置文件

[root@localhost /]# cat /etc/my.cnf
[mysqld]
basedir=/opt/mysql
datadir=/opt/data
socket=/opt/mysql/tmp/mysql.socket
user=mysql
port=3306
server_id=200
log-error=/opt/mysql/errorslogs/200-error.log
pid-file=/opt/mysql/tmp/mysqld.pid
[mysql]
socket=/opt/mysql/tmp/mysql.socket
default-character-set=utf8
[mysqldump]
socket=/opt/mysql/tmp/mysql.socket
default-character-set=utf8

4. 初始化服务

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

初始化成功后会在当前目录下errorslogs/200-error.log日志文件(由配置文件log-error参数定义)下生成一个随机密码,初次登陆使用.
随机密码: apTdatWJ+5?X

[root@localhost mysql]# grep "temporary password" errorslogs/200-error.log 
2019-11-26T03:13:37.243650Z 1 [Note] A temporary password is generated for root@localhost: apTdatWJ+5?X

5. 配置mysql服务开机自动启动

# 复制启动服务文件至/etc/init.d/目录下
cp /opt/mysql/support-files/mysql.server /etc/init.d/mysqld

# 赋予启动服务文件可执行权限和用户
chmod +x /etc/init.d/mysqld
chown -R mysql:mysql /etc/init.d/mysqld

# 修改启动服务文件basedir和datadir参数
sed -i "s#^basedir=.*#basedir=/opt/mysql#" /etc/init.d/mysqld
sed -i "s#^datadir=.*#datadir=/opt/data#" /etc/init.d/mysqld
[root@localhost ~]# grep -E "^basedir|^datadir" "/etc/init.d/mysqld"
basedir=/opt/mysql 
datadir=/opt/data

# 启动mysqld服务
service mysqld start

# 添加自启动
chkconfig --add mysqld 
chkconfig mysqld on

# 添加环境变量
echo "export PATH=$PATH:/opt/mysql/bin" >> /etc/profile
source /etc/profile

6.初次登陆重置mysql密码

mysql -uroot -p'apTdatWJ+5?X' --connect-expired-password -e "set password=password('123456');"

安装后第一次登录如不添加--connect-expired-password参数,则会报错
Please use --connect-expired-password option or invoke mysql in interactive mode.

[root@localhost ~]# mysql -uroot -p'123456'
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 6
Server version: 5.7.28 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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> 

至此,Centos7安装mysql5.7.28版本完成
如有疑问,可留下评论.

posted @ 2020-12-08 10:42  风吹蛋生丶  阅读(476)  评论(0编辑  收藏  举报