服務(wù)器環(huán)境:
CentOS-7.4-x86_64-DVD-1708
MySQL版本:
mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
1谦铃,首先卸載Centos7自帶的Mariadb
# 查看系統(tǒng)自帶的Mariadb
[root@CDH-141 ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
# 卸載系統(tǒng)自帶的Mariadb(此時(shí)要根據(jù)實(shí)際情況去卸載)
[root@CDH-141 ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
2须眷,檢查Msyql是否存在
# 檢查mysql是否存在
[root@CDH-141 ~]# rpm -qa | grep mysql
[root@CDH-141 ~]#
3,查看用戶和組是否存在
(1),先檢查是否存在,如果不存在則創(chuàng)建
# 檢查mysql組和用戶是否存在士嚎,如無則創(chuàng)建
[root@CDH-141 ~]# cat /etc/group | grep mysql
[root@CDH-141 ~]# cat /etc/passwd | grep mysql
# 創(chuàng)建mysql用戶組
[root@CDH-141 ~]# groupadd mysql
# 創(chuàng)建一個(gè)用戶名為mysql的用戶米奸,并加入mysql用戶組
[root@CDH-141 ~]# useradd -g mysql mysql
# 制定password 為111111
[root@CDH-141 ~]# passwd mysql
Changing password for user mysql.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.
4,下載mysql離線安裝包tar文件
官網(wǎng)下載地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
版本選擇灌旧,可以選擇一下兩種方式:
1)使用Red Hat Enterprise Linux
Select Version:5.7.25
Select Operating System:Red Hat Enterprise Linux / Oracle Linux
Select OS Version:Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit)
列表中下載:
Compressed TAR Archive:(mysql-5.7.25-el7-x86_64.tar.gz)
2)使用Linux - Generic
Select Version:5.7.25
Select Operating System:Linux - Generic
Select OS Version:Linux - Generic (glibc 2.12) (x86, 64-bit)
列表中下載:
Compressed TAR Archive:(mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz)【本文中使用的是這個(gè)版本】
注意:上邊兩種方式找mysql離線安裝包的方式都可以绑咱。
5,上傳第四步下載的mysql TAR包
# 上傳mysql TAR包
# 或者直接使用XFTP進(jìn)行安裝
[root@CDH-141 ~]# rz
# 解壓mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
[root@CDH-141 local]# ls
bin full-path-to-mysql-VERSION-OS include lib64 mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz share
etc games lib libexec sbin src
[root@CDH-141 local]# tar -zxvf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
mysql-5.7.25-lin
...
mysql-5.7.25-linux-glibc2.12-x86_64/share/install_rewriter.sql
mysql-5.7.25-linux-glibc2.12-x86_64/share/uninstall_rewriter.sql
mysql-5.7.25-linux-glibc2.12-x86_64/support-files/magic
mysql-5.7.25-linux-glibc2.12-x86_64/support-files/mysql.server
mysql-5.7.25-linux-glibc2.12-x86_64/docs/INFO_BIN
mysql-5.7.25-linux-glibc2.12-x86_64/docs/INFO_SRC
[root@localhost ~]# cd /usr/local/
[root@CDH-141 local]# ls
bin full-path-to-mysql-VERSION-OS include lib64 mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz share
etc games lib libexec mysql-5.7.25-linux-glibc2.12-x86_64 sbin src
# 進(jìn)入/usr/local下枢泰,修改為mysql
[root@CDH-141 local]# mv mysql-5.7.27-linux-glibc2.12-x86_64/ mysql
[root@CDH-141 local]# ls
bin etc full-path-to-mysql-VERSION-OS games include lib lib64 libexec mysql mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz sbin share src
6描融,更改所屬的組和用戶
# 更改所屬的組和用戶
[root@CDH-141 ~]# cd /usr/local/
[root@CDH-141 local]# chown -R mysql mysql/
[root@CDH-141 local]# chgrp -R mysql mysql/
[root@CDH-141 local]# cd mysql/
[root@CDH-141 mysql]# mkdir data
[root@CDH-141 mysql]# chown -R mysql:mysql data
7,在/etc下創(chuàng)建my.cnf文件
# 編輯/etc/my.cnf
[root@CDH-141 mysql]# vi /etc/my.cnf
[mysql]
socket=/tmp/mysql.sock
# set mysql client default chararter
#default-character-set=utf8
[mysqld]
socket=/tmp/mysql.sock
# set mysql server port
port = 3306 # 默認(rèn)是3306衡蚂,如果這里發(fā)現(xiàn)3306已經(jīng)被占用窿克,可以更改
# set mysql install base dir
basedir=/usr/local/mysql
# set the data store dir
datadir=/usr/local/mysql/data
# set the number of allow max connnection
max_connections=1024
# set server charactre default encoding
character-set-server=utf8
# the storage engine
default-storage-engine=INNODB
# 設(shè)置MySQL對(duì)表名等不區(qū)分大小寫
lower_case_table_names=1
max_allowed_packet=200M
explicit_defaults_for_timestamp=true
#阻止過多嘗試失敗的客戶端以防止暴力破解密碼的情況,與性能并無太大的關(guān)系
max_connect_errors=30
#此參數(shù)確定數(shù)據(jù)日志文件的大小,以M為單位毛甲,根據(jù)數(shù)據(jù)更新頻率調(diào)整年叮。
innodb_log_file_size=50
#指定大小的內(nèi)存來緩沖數(shù)據(jù)和索引,最大可以把該值設(shè)置成物理內(nèi)存的80%
innodb_buffer_pool_size=10G
key_buffer_size=16M
sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# 配置GROUP_CONCAT拼接的字符串的長(zhǎng)度字節(jié)
group_concat_max_len = 102400
[mysql.server]
user=mysql
basedir=/usr/local/mysql
# 可以防止出現(xiàn)插入中文報(bào)錯(cuò);如果此時(shí)不生效玻募,可以強(qiáng)制在創(chuàng)建表的時(shí)候指定使用utf8的編碼集
[client]
default-character-set = utf8
8,進(jìn)入mysql文件夾只损,并安裝mysql
# 進(jìn)入mysql
[root@CDH-141 local]# cd /usr/local/mysql
# 安裝mysql
[root@CDH-141 mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2019-03-08 18:11:07 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2019-03-08 18:11:24 [WARNING] The bootstrap log isn't empty:
2019-03-08 18:11:24 [WARNING] 2019-03-08T10:11:07.208602Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
# 設(shè)置文件以及目錄的權(quán)限
[root@CDH-141 mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
9,啟動(dòng)MySQL
# 啟動(dòng)mysql
[root@CDH-141 mysql]# /etc/init.d/mysqld restart
ERROR! MySQL server PID file could not be found!
Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
SUCCESS!
10七咧,設(shè)置開機(jī)自啟動(dòng)
#設(shè)置開機(jī)啟動(dòng)
[root@CDH-141 mysql]# chkconfig --level 35 mysqld on
[root@CDH-141 mysql]# chkconfig --list mysqld
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@CDH-141 mysql]# chkconfig --add mysqld
[root@CDH-141 mysql]# chkconfig --list mysqld
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@CDH-141 mysql]# service mysqld status
MySQL running (26122)[ OK ]
[root@CDH-141 mysql]#
11跃惫,修改環(huán)境變量配置文件
# 進(jìn)入/etc/profile文件夾
[root@CDH-141 mysql]# vi /etc/profile
修改/etc/profile,在最后添加如下內(nèi)容
# 修改/etc/profile文件
export MYSQL_HOME=/usr/local/mysql
export PATH=$MYSQL_HOME/bin:$PATH
# 使文件生效
[root@CDH-141 mysql]# source /etc/profile
12艾栋,獲得MySQL初始密碼
# 每個(gè)人的密碼是不一樣的爆存,隨機(jī)生成的
[root@CDH-141 mysql]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2019-03-08 17:40:42
poc3u0mO_luv
[root@CDH-141 mysql]#
13,修改密碼
[root@CDH-141 mysql]# mysql -uroot -p
Enter password: #此處填寫上邊獲取到的初始密碼‘poc3u0mO_luv’
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25
Copyright (c) 2000, 2015, 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> set PASSWORD = PASSWORD('111111@123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
# 驗(yàn)證登錄
[root@localhost mysql]# mysql -uroot -p111111@123
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 5
Server version: 5.7.27 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>
14裹粤,添加遠(yuǎn)程訪問權(quán)限
注意:當(dāng)前的訪問權(quán)限是直接給root用戶添加的终蒂,如果在項(xiàng)目現(xiàn)場(chǎng)部署的時(shí)候,是不能使用root用戶的;
此時(shí)是有兩種方案的遥诉,分別做了整理:
## 方案1:直接給root用戶增加遠(yuǎn)程訪問權(quán)限
# 添加遠(yuǎn)程訪問權(quán)限
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+---------------+
3 rows in set (0.00 sec)
mysql>
--- 方案2:追加一個(gè)demo用戶拇泣,然后給demo用戶添加遠(yuǎn)程訪問的權(quán)限
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
-- 此時(shí)是給所有用戶開放的,如果需要對(duì)不不同的用戶開發(fā)需要再單獨(dú)設(shè)置
mysql> update user set host='%' where user='demo';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | demo |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+---------------+
3 rows in set (0.00 sec)
15矮锈,重啟MySQL使其生效
做上述操作之后霉翔,是需要重新啟動(dòng)MySQL,才能使其生效
# 重啟mysql
[root@CDH-141 mysql]# /etc/init.d/mysqld restart
Shutting down MySQL..[ OK ]
Starting MySQL..[ OK ]
[root@CDH-141 mysql]#
## 重啟命令2
systemctl start mysql
16苞笨,如果連接失敗债朵,需要檢查防火墻
# 第一步:關(guān)閉防火墻
systemctl stop firewalld
#################
#############
####### 注意子眶,在實(shí)際中先關(guān)閉,確定是使用開發(fā)端口的方式實(shí)現(xiàn)序芦,還是直接移除臭杰,再執(zhí)行對(duì)應(yīng)的操作
##### 第二步:此時(shí)有兩種解決方案
# 方案1:移除防火墻
systemctl disable firewalld
# 方案2:開發(fā)3306端口
# 開放端口請(qǐng)根據(jù)實(shí)際訪問規(guī)則配置,這里只是單純開啟3306端口的訪問谚中,默認(rèn)全放行渴杆,在生產(chǎn)環(huán)境禁止開放所有IP訪問
# (1) 向防火墻添加 mysql 端口:
firewall-cmd --zone=public --add-port=3306/tcp --permanent
# (2) 刷新防火墻規(guī)則:
firewall-cmd --reload
# (3) 驗(yàn)證端口,查詢防火墻開放端口:
firewall-cmd --zone=public --list-port