直入正題
以下爲安裝步驟
(1)關閉防火牆
systemctl stop firewalld.service
systemctl disable firewalld.service #禁止開機自啓動
firewall-cmd --state #顯示爲not running即爲關閉狀態
selinux 關不關感覺沒什麼影響
selinux關閉方法
setenforce 0 #重啓後失效
vi /etc/selinux/config #把SELINUX=enforce改成disabled,重啓電腦,永久生效
(2)檢查是否已安裝mysql,或者mariadb
如果系統中已有安裝會有衝突的,尤其是mariadb,centos7安裝時是默認安裝的,所以在裝系統的時候,最好去掉這個選項。
rpm -qa |grep mysql
如有
yum remove mysql*
同樣mariadb檢查一遍
rpm -qa |grep mariadb
yum remove mariadb*
(3)創建mysql用戶和用戶組
[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -d /home/mysql -g mysql -m mysql
[root@localhost ~]# passwd mysql
更改用戶 mysql 的密碼 。
新的 密碼:
無效的密碼: 密碼少於 7 個字符
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經成功更新。
(4)創建目錄並更改權限
[root@localhost ~]# mkdir -p /opt/mysql5.7 #安裝mysql的base目錄
[root@localhost ~]# mkdir -p /Mysql/my3306/{data,tmp,log} #默認3306端口,多實例的話可使用3307,3308....等端口
[root@localhost ~]# cd /Mysql/my3306/
[root@localhost my3306]# ls
data log tmp
[root@localhost my3306]# mkdir -p /Mysql/my3306/log/{binlog,iblog}
[root@localhost my3306]# cd log/
[root@localhost log]# ls
binlog iblog
更改目錄權限
[root@localhost ~]# chown -R mysql:mysql /Mysql/my3306
[root@localhost ~]# chown -R mysql:mysql /opt/mysql5.7
(5)解壓mysql tar包,並將添加到系統環境變量
[root@localhost vm_win_public]# tar -xzf mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz -C /opt
[root@localhost opt]# mv mysql-5.7.19-linux-glibc2.12-x86_64/* mysql5.7/ #移動至mysql5.7目錄下
將bin添加到系統環境變量
vi /etc/profile #所有用戶添加環境變量,重啓依舊有效
在末尾加上:
PATH=$PATH:/opt/mysql5.7/bin
export PATH
最後 source /etc/profile 重新讀取下配置文件
(6)初始化mysql數據庫
root@localhost data]# mysqld --initialize --user=mysql --basedir=/opt/mysql5.7 --datadir=/Mysql/my3306/data
2019-10-11T08:48:02.858806Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-10-11T08:48:03.060795Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-10-11T08:48:03.092030Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-10-11T08:48:03.171779Z 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: d60c9920-ec03-11e9-bb68-000c29cdd83d.
2019-10-11T08:48:03.173732Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-10-11T08:48:03.174641Z 1 [Note] A temporary password is generated for root@localhost: Jw9i(?wojYek
最後的:Jw9i(?wojYek 就是你的臨時密碼,一定要保存下來,登陸mysql要用到
如果初始化的過程中有[error],可用於通過錯誤提示來解決問題,並且當錯誤解決後一定要清空/my3306/data目錄下的所有內容,否則會報:
[root@localhost opt]# mysqld --initialize --user=mysql --basedir=/opt/mysql5.7 --datadir=/Mysql/my3306/data
2019-10-11T08:47:14.336663Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-10-11T08:47:14.338605Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2019-10-11T08:47:14.338633Z 0 [ERROR] Aborting44
(7)編輯my.cnf 控制文件
vi /etc/my.cnf 編輯配置文件
以下是配置文件的內容,也是啓動mysql的必要配置,實際使用過程中可以根據使用需要設定相應參數
[mysqld]
basedir=/opt/mysql5.7
datadir=/Mysql/my3306/data
port=3306
pid-file = /Mysql/my3306/data/mysql.pid
user = mysql
socket=/Mysql/my3306/tmp/mysql.sock
bind-address = 0.0.0.0
server-id = 1
character-set-server = utf8
[client]
port=3306
socket=/Mysql/my3306/tmp/mysql.sock
[mysql]
socket=/Mysql/my3306/tmp/mysql.sock
(8)添加並啓動mysql服務
將 mysql.server文件拷貝至/etc/init.d/目錄下
cp /opt/mysql5.7/support-files/mysql.server /etc/init.d/mysqld
編輯 /etc/init.d/mysqld 填寫basedir和datadir信息
vi /etc/init.d/mysqld
# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.
basedir=/opt/mysql5.7 #填入
datadir=/Mysql/my3306/data
# Default value, in seconds, afterwhich the script should timeout waiting
# for server start.
# Value here is overriden by value in my.cnf.
保存並退出
啓動mysql服務
[root@localhost ~]# service mysqld start
Starting MySQL.Logging to '/Mysql/my3306/data/localhost.localdomain.err'.
. SUCCESS!
[root@local
(9)登陸mysql並修改密碼
密碼就是前面(6)中所記錄的
[mysql@localhost ~]$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.19
Copyright (c) 2000, 2017, 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>
mysql>
更改初始密碼
mysql> set password for root@localhost = password('新密碼');
mysql> flush privileges
安裝完成=。=