1. 下載相應(yīng)版本的安裝包
當(dāng)前最新mysql5.7版本為 mysql5.7.28
官網(wǎng)鏈接: https://dev.mysql.com/downloads/mysql/
百度網(wǎng)盤鏈接: 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
把腳本和壓縮包放置在同一個(gè)目錄中,可自定義路徑等配置參數(shù)
2. 安裝
當(dāng)前環(huán)境為Centos7.4,如之前安裝過
# 刪除mariadb依賴包
yum -y remove mariadb-libs-5.5.56-2.el7.x86_64
# 創(chuàng)建mysql用戶和組
useradd -UM mysql -s /sbin/nologin
-U 創(chuàng)建與用戶同名的組
-M 不創(chuàng)建用戶的主目錄
-s 指定用戶登錄后的shell,/sbin/nologin禁止登錄系統(tǒng)
# 解壓壓縮包
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
# 創(chuàng)建所需目錄
mkdir -p mysql/{tmp,errorslogs} data
# 更改目錄所屬
chown -R mysql:mysql mysql/ data/
家目錄與數(shù)據(jù)目錄分離和建立軟鏈接是為了方便遷移和版本升級(jí)
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. 初始化服務(wù)
/opt/mysql/bin/mysqld --initialize --datadir=/opt/data --user=mysql --basedir=/opt/mysql/
初始化成功后會(huì)在當(dāng)前目錄下errorslogs/200-error.log日志文件(由配置文件log-error參數(shù)定義)下生成一個(gè)隨機(jī)密碼,初次登陸使用.
隨機(jī)密碼: 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服務(wù)開機(jī)自動(dòng)啟動(dòng)
# 復(fù)制啟動(dòng)服務(wù)文件至/etc/init.d/目錄下
cp /opt/mysql/support-files/mysql.server /etc/init.d/mysqld
# 賦予啟動(dòng)服務(wù)文件可執(zhí)行權(quán)限和用戶
chmod +x /etc/init.d/mysqld
chown -R mysql:mysql /etc/init.d/mysqld
# 修改啟動(dòng)服務(wù)文件basedir和datadir參數(shù)
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
# 啟動(dòng)mysqld服務(wù)
service mysqld start
# 添加自啟動(dòng)
chkconfig --add mysqld
chkconfig mysqld on
# 添加環(huán)境變量
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參數(shù),則會(huì)報(bào)錯(cuò)
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版本完成
如有疑問,可留下評(píng)論.
原創(chuàng)簡(jiǎn)書作者: 風(fēng)吹蛋生丶