部署MYSQL5.7版本

一、yum 安裝mysql 5.7版本

1.下載 YUM 倉庫文件

打開網(wǎng)址: https://dev.mysql.com/downloads/repo/yum/

image

![image](//upload-images.jianshu.io/upload_images/11414906-4501cb7b0060de95.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000

或者右鍵后 點(diǎn)擊 復(fù)制鏈接地址, 之后用 wget 命令下載

# wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

2.安裝 YUM Repo 文件

# yum localinstall mysql80-community-release-el7-1.noarch.rpm

可以選擇5.5 5.6 5.7 8.0版本,我們此次安裝mysql5.7版本

// 查看 YUM 倉庫關(guān)于 MySQL 的所有倉庫列表
# yum repolist all | grep mysql

// 只查看啟用的
# yum repolist enabled | grep mysql

// 安裝 YUM 管理工具包,此包提供了 yum-config-manager 命令工具
# yum install yum-utils

// 禁用 8.0
# yum-config-manager --disable mysql80-community

// 啟用 5.7
# yum-config-manager --enable mysql57-community

再次確認(rèn)啟用的 MySQL 倉庫

# yum repolist enabled | grep mysql

image

3.開始安裝 MySQL

# yum install -y  mysql-community-server

4. 管理 MySQL 服務(wù)

// 啟動(dòng)
# systemctl start mysqld.service

// 查看狀態(tài)
# systemctl status mysqld.service

// 開機(jī)自啟動(dòng)
# systemctl enable mysqld.server

// 查看監(jiān)聽端口,默認(rèn) 3306
#  ss -natl |grep 3306

5. 初始化 Mysql

MySQL服務(wù)器初始化(從MySQL 5.7開始):

在 MySQL 服務(wù)器初始啟動(dòng)時(shí),如果服務(wù)器的數(shù)據(jù)目錄為空,則會(huì)發(fā)生以下情況:

  • MySQL 服務(wù)器已初始化。
  • 在數(shù)據(jù)目錄中生成SSL證書和密鑰文件。
  • validate_password插件安裝并啟用。
  • 將創(chuàng)建一個(gè)超級(jí)用戶 帳戶'root'@'localhost'。并會(huì)設(shè)置超級(jí)用戶的密碼,將其存儲(chǔ)在錯(cuò)誤日志文件中。要顯示它,請(qǐng)使用以下命令:
# grep 'temporary password' /var/log/mysqld.log
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
2019-02-14T01:46:25.542086Z 1 [Note] A temporary password is generated for root@localhost: U.CXJi_d6-yK


image

6.啟動(dòng)mysql后重置密碼問題

①修改密碼過簡報(bào)錯(cuò)

[root@localhost ~]  mysqladmin -p'Wzge2E0Qp*LH' password  '123'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
mysqladmin: unable to change password; error: 'Your password does not satisfy the current policy requirements'
#mysqladmin:[警告]在命令行界面上使用密碼可能不安全。
#警告:由于密碼將以純文本形式發(fā)送到服務(wù)器,請(qǐng)使用ssl連接以確保密碼安全。
#mysqladmin:無法更改密碼;錯(cuò)誤提示:“您的密碼不符合當(dāng)前的策略要求”

注意:
MySQL的 validate_password 插件默認(rèn)安裝。這將要求密碼包含至少一個(gè)大寫字母,一個(gè)小寫字母,一個(gè)數(shù)字和一個(gè)特殊字符,并且密碼總長度至少為8個(gè)字符。

②修改符合要求密碼依然報(bào)錯(cuò)

  • 取消密碼復(fù)雜度

編輯 my.cnf配置文件, 在 [mysqld]配置塊兒中添加如下內(nèi)容

skip-grant-tables    //數(shù)據(jù)庫免密登錄
plugin-load=validate_password.so 
validate-password=OFF

保存退出后,重啟服務(wù), 進(jìn)入數(shù)據(jù)庫修改密碼。

mysql> use mysql;
mysql> update user set authentication_string=password("123") where user="root";  
mysql> flush privileges;  

③如果登陸過期需要再次修改

png
mysql> alter user user() identified by "123456";



二、 源碼部署mysql 5.7版本

1. 需要先卸載一些軟件

// centos7 中需要先卸載 mariadb-libs 軟件包
# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
# rpm -e --nodeps mariadb-libs

2. 安裝依賴包

# yum -y install gcc gcc-c++ ncurses ncurses-devel cmake bison bison-devel

相關(guān)依賴包的作用:

cmake:
    由于從 MySQL5.5 版本開始棄用了常規(guī)的 configure 編譯方法,所以需要 CMake 編譯器,用于設(shè)置 mysql 的編譯參數(shù)。如:安裝目錄、數(shù)據(jù)存放目錄、字符編碼、排序規(guī)則等。   

Boost:
    從 MySQL 5.7.5 開始 Boost 庫是必需的,mysql 源碼中用到了 C++的 Boost 庫,要求必須安裝 boost1.59.0 或以上版本, 這個(gè)選擇源碼安裝

GCC:
    是 Linux 下的 C 語言編譯工具,mysql 源碼編譯完全由 C 和 C++編寫,要求必須安裝GCC    

bison:
    Linux 下 C/C++語法分析器    

ncurses:
    字符終端處理庫

3.下載源碼

mysql5.7.x https://dev.mysql.com/downloads/mysql/

image

假如是 8.0

image

接下來 右鍵選擇 復(fù)制鏈接地址(這里是 5.7

image

開始下載

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.23.tar.gz

同時(shí)你還應(yīng)該下載

wget http://downloads.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz

  • 我手中有mysql和boost的安裝啟動(dòng)包。故我直接將他們解壓安裝到 /usr/local/src
tar -zxvf mysql-5.7.25.tar.gz 
mv boost_1_59_0.tar.gz  mysql-5.7.25
mv mysql-5.7.25 /usr/local/src/
cd /usr/local/src/mysql-5.7.25/
mkdir configure
cd configure

4. 使用cmake進(jìn)行生成編譯環(huán)境

cmake .. -DBUILD_CONFIG=mysql_release \
-DINSTALL_LAYOUT=STANDALONE \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DENABLE_DTRACE=OFF \
-DWITH_EMBEDDED_SERVER=OFF \
-DWITH_INNODB_MEMCACHED=ON \
-DWITH_SSL=bundled \
-DWITH_ZLIB=system \
-DWITH_PAM=ON \
-DCMAKE_INSTALL_PREFIX=/var/mysql/ \
-DINSTALL_PLUGINDIR="/var/mysql/lib/plugin" \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EDITLINE=bundled \
-DFEATURE_SET=community \
-DCOMPILATION_COMMENT="MySQL Server (GPL)" \
-DWITH_DEBUG=OFF \
-DWITH_BOOST=..

解釋一下上面編譯中用到的參數(shù)含義

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ # 指定安裝目錄
-DMYSQL_DATADIR=/data/mysql \ # 指定數(shù)據(jù)存放目錄
-DDOWNLOAD_BOOST=1 \ # 從MySQL 5.7.5開始Boost庫是必需的
-DWITH_BOOST=../boost_1_59_0
-DSYSCONFDIR=/etc \ # 指定配置文件目錄
-DENABLED_LOCAL_INFILE=1 \ # 指定允許使用load data infile功能,就是加載本地文件
-DENABLE_DTRACE=0
-DDEFAULT_CHARSET=utf8mb4 \ # 指定默認(rèn)的字符編碼
-DDEFAULT_COLLATION=utf8mb4_general_ci \ # 指定默認(rèn)排序規(guī)則
-DWITH_EMBEDDED_SERVER=1 # 編譯使用 libmysqld 嵌入式庫

更多參數(shù): https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html#option_cmake_enabled_local_infile

錯(cuò)誤匯總:

1.如果中途編譯失敗了,需要?jiǎng)h除cmake生成的預(yù)編譯配置參數(shù)的緩存文件和make編譯后生成的文件,再重新編譯。

2.如果報(bào)錯(cuò)

make[2]: *** [libmysqld/examples/mysql_client_test_embedded] 錯(cuò)誤 1   
make[1]: *** [libmysqld/examples/CMakeFiles/mysql_client_test_embedded.dir/all] 錯(cuò)誤 2

加上

   -DWITH_EMBEDDED_SERVER=OFF

當(dāng)你看到如下類似信息,表示成功

image
編譯安裝
// 編譯很消耗系統(tǒng)資源,小內(nèi)存可能編譯通不過,最少 2 G 以上的內(nèi)存。
make -j `grep processor /proc/cpuinfo | wc -l`  
make install
//如果機(jī)器選擇運(yùn)行內(nèi)存或者合數(shù)足夠
make & make install

5. 初始化數(shù)據(jù)庫

5.1 添加mysql用戶

useradd -s /sbin/nologin mysql

5.2 新建數(shù)據(jù)庫文件夾和日志文件夾,并更改用戶為mysql

mkdir /mysql_data
mkdir /var/mysql/log
chown -R mysql:mysql /mysql_data/
chown -R mysql:mysql /var/mysql/log

5.3 修改配置文件 my.cnf

將[mysqld]項(xiàng)下的內(nèi)容替換為:

[mysqld]
port=3306
datadir=/mysql_data
log_error=/var/mysql/log/error.log
basedir=/var/mysql/

5.4 初始化數(shù)據(jù)庫

/var/mysql/bin/mysqld  --initialize --user=mysql
查看數(shù)據(jù)文件是否生成:

[root@localhost configure]# ll /mysql_data/
總用量 110620
-rw-r-----. 1 mysql mysql       56 10月  2 19:44 auto.cnf
-rw-r-----. 1 mysql mysql      419 10月  2 19:44 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 10月  2 19:44 ibdata1
-rw-r-----. 1 mysql mysql 50331648 10月  2 19:44 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 10月  2 19:44 ib_logfile1
drwxr-x---. 2 mysql mysql     4096 10月  2 19:44 mysql
drwxr-x---. 2 mysql mysql     4096 10月  2 19:44 performance_schema
drwxr-x---. 2 mysql mysql    12288 10月  2 19:44 sys
查看日志文件是否生成:

[root@localhost mysql]# ll /var/mysql/log/
總用量 4
-rw-r-----. 1 mysql mysql 802 10月  2 19:47 error.log 

6. 配置啟動(dòng)文件和環(huán)境變量

6.1配置啟動(dòng)文件

cp /var/mysql/support-files/mysql.server /etc/init.d/mysqld

6.2修改啟動(dòng)文件

vim /etc/init.d/mysqld
找到如下二行:

basedir=
datadir=
修改為:

basedir=/var/mysql/
datadir=/mysql_data

6.3啟動(dòng)數(shù)據(jù)庫

[root@localhost mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
可以看到提示,已經(jīng)成功啟動(dòng)。當(dāng)然你也可以使用systemctl來啟動(dòng)MySQL,但執(zhí)行后,不會(huì)有任何提示。

[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl enable mysqld
mysqld.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig mysqld on

7. 配置mysql的環(huán)境變量

vim /root/.bash_profile
找到下面一行:

PATH=$PATH:$HOME/bin
修改為:

PATH=$PATH:$HOME/bin:/var/mysql/bin

8. 登錄設(shè)置

8.1查看root密碼

[root@localhost configure]# cat /var/mysql/log/error.log |grep 'A temporary password'
2019-03-09T10:33:54.050288Z 1 [Note] A temporary password is generated for root@localhost: i9Ni=:Gy9N7<

8.2 登錄修改密碼

[root@localhost configure]# mysql -uroot 
-bash: mysql: command not found
  • 因?yàn)閙ysql命令的路徑在/var/mysql/bin/mysql下面,所以你直接使用mysql命令時(shí),
    系統(tǒng)在/usr/bin下面查此命令,所以找不到了

    解決辦法

[root@localhost configure]# ln -s /var/mysql/bin/mysql /usr/bin/
[root@localhost configure]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25

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>
  • 使用賬戶有限制
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

所以我們使用與yum相同的方法來改變我們的密碼即可

mysql> alter user user() identified by "1";

最終我們完成數(shù)據(jù)庫的配置

[root@localhost configure]# mysql -uroot -p1
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 3
Server version: 5.7.25 MySQL 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> 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容