實戰三種方式部署 MySQL5.7

来源:https://www.cnblogs.com/wangenzhi/archive/2019/03/21/10569446.html
-Advertisement-
Play Games

作者:不悔 原文鏈接: "https://www.opsbj.com/2019/03/20/mysql install/" 常見的 MySQL 安裝方式有如下三種: 1. RPM 包方式:這種方式安裝適合對資料庫要求不太高的場合,安裝速度快; 2. 通用二進位包方式:安裝速度相較於源碼方式快,可以自 ...


作者:不悔
原文鏈接:https://www.opsbj.com/2019/03/20/mysql-install/

常見的 MySQL 安裝方式有如下三種:

  1. RPM 包方式:這種方式安裝適合對資料庫要求不太高的場合,安裝速度快;
  2. 通用二進位包方式:安裝速度相較於源碼方式快,可以自定義安裝目錄。
  3. 源碼編譯安裝:安裝過程比較慢,機器性能不好的情況下,大約需要30分鐘左右,通常適用於mysql定製化的安裝,比如需要加入一些第三方的插件及依賴庫等

環境說明

OS 版本 MySQL 版本
CentOS 7.5.1804 5.7.25

一、RPM 包方式安裝

1.1 獲取 RPM 包

訪問 MySQL 官網,下載最新版 mysql5.7 的 rpm 包。

  • 點擊 DOWNLOADS --> 點擊 Community 社區版 --> 選擇 MySQL Community Server

-w1030

  • 選擇 MySQL Community Server 5.7 -> 而後選擇對應的軟體平臺版本

-w990

  • 選擇下載 RPM Bundle 這裡包含了所有 MySQL 的 RPM 包。

-w1041

-w902

1.2 安裝 MySQL

下載 Bundle 包解壓以後,可以看到包含了所有 MySQL 相關的 RPM 包:

-w1178

其中 client、common、libs、server 四個程式包是必須安裝的:

mysql-community-client-5.7.25-1.el7.x86_64.rpm
mysql-community-common-5.7.25-1.el7.x86_64.rpm
mysql-community-libs-5.7.25-1.el7.x86_64.rpm
mysql-community-server-5.7.25-1.el7.x86_64.rpm

在執行安裝之前,先檢查是否已經安裝過(CentOS7 以後預設安裝的 mariadb)

$ rpm -qa|egrep "mariadb|mysql"
mariadb-libs-5.5.60-1.el7_5.x86_64
# 我這裡存在 mariadb-libs 會造成衝突,所以卸載掉
rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
# 卸載之後就可以進行安裝使用 yum 或者 rpm -ivh
$ yum -y install mysql-community-client-5.7.25-1.el7.x86_64.rpm mysql-community-common-5.7.25-1.el7.x86_64.rpm mysql-community-libs-5.7.25-1.el7.x86_64.rpm mysql-community-server-5.7.25-1.el7.x86_64.rpm

安裝完成後 MySQL 的預設配置文件為 /etc/my.cnf 接下來我們就可以啟動 MySQL 啦

$ systemctl start mysqld.service
$ systemctl enable mysqld.service
$ systemctl status mysqld.service

1.3 修改 MySQL 預設密碼

MySQL 5.7 以後,不在允許使用空密碼進行登錄,預設會初始化一個密碼到 MySQL Error 日誌中,配置參數 log-error= 指定的文件。

$ cat /var/log/mysqld.log | grep password
2019-03-20T02:44:49.359004Z 1 [Note] A temporary password is generated for root@localhost: /qrsXHttL6Mr

連接實例並修改預設密碼

$ mysql -uroot -p
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 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> set password for 'root'@'localhost'=password('MyNewPass4!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

以後通過 update set 語句修改密碼:

mysql> use mysql;
mysql> update user set authentication_string=PASSWORD('NewPass@2019') where user='root';
mysql> flush privileges;

註意:mysql 5.7 預設密碼檢查策略要求密碼必須包含:大小寫字母、數字和特殊符號,並且長度不能少於8位。否則會提示 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 錯誤。查看 MySQL 密碼策略

二、通用二進位包方式安裝

官方文檔:https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html

2.1 獲取安裝包

選擇 Linux - generic 64 位安裝包

-w1049

2.2 安裝 MySQL

MySQL 依賴於 libaio 庫。 如果未在本地安裝此庫,則數據目錄初始化和後續伺服器啟動步驟將失敗。 如有必要,請使用適當的包管理器進行安裝。 例如,在基於Yum 的系統上:

$ yum -y install libaio

創建 MySQL 用戶和組

$ groupadd mysql
$ useradd -r -g mysql -s /bin/false mysql

解壓到指定目錄

$ tar xf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
$ cd /usr/local/
$ ln -sv mysql-5.7.25-linux-glibc2.12-x86_64/ mysql

修改解壓目錄下所有文件屬主及屬組

$ cd /usr/local/mysql
$ chown -R root.mysql ./*

創建數據目錄,以 /data/mysql/data 為例

$ mkdir -pv /data/mysql/{data,log}
$ chown -R mysql.mysql /data/mysql

準備 MySQL 配置文件,我這裡用的是線上工具生成的 my.cnf 文件,工具鏈接

$ cat /etc/my.cnf

[client]
port = 3306
socket = /data/mysql/mysql.sock

[mysqld]
port = 3306
socket = /data/mysql/mysql.sock
pid_file = /var/run/mysql.pid
datadir = /data/mysql/data
basedir = /usr/local/mysql
default_storage_engine = InnoDB
max_allowed_packet = 128M
max_connections = 2048
open_files_limit = 65535
skip-name-resolve
lower_case_table_names=1
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
innodb_buffer_pool_size = 128M
innodb_log_file_size = 128M
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 0
key_buffer_size = 16M
log-error = /data/mysql/log/mysql_error.log
log-bin = /data/mysql/log/mysql_bin.log
slow_query_log = 1
slow_query_log_file = /data/mysql/log/mysql_slow_query.log
long_query_time = 5
tmp_table_size = 16M
max_heap_table_size = 16M
query_cache_type = 0
query_cache_size = 0
server-id=1

複製啟動腳本

$ cp support-files/mysql.server /etc/init.d/mysqld
$ chmod +x /etc/init.d/mysqld
$ chkconfig --add mysqld

初始化資料庫

$ ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data

此時會生成一個臨時密碼,可以在 mysql_error.log 文件找到

$ grep password /data/mysql/log/mysql_error.log
2019-03-20T05:37:28.267207Z 1 [Note] A temporary password is generated for root@localhost: H_wgkXR&f1=t

生成 SSL

$ ./bin/mysql_ssl_rsa_setup --basedir=/usr/local/mysql --datadir=/data/mysql/data/

啟動 MySQL 服務

$ service mysqld start
$ ss -tnlp | grep 3306

配置 MySQL 環境變數

$ vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
$ source /etc/profile.d/mysql.sh

2.3 MySQL 用戶初始化

$ mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: # 輸入初始密碼,在錯誤日誌中

The existing password for the user account root has expired. Please set a new password.

New password: # 輸入新密碼

Re-enter new password: # 輸入新密碼

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: Y # 是否啟用密碼安全策略

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2 # 設置密碼複雜度
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : N # 是否修改 root 密碼,剛纔已經新設置了,輸入 N

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y # 是否移除匿名用戶
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y # 是否禁止 root 用戶遠程登錄
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y # 是否刪除 test 測試資料庫
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y # 是否刷新許可權表
Success.

All done!

驗證 MySQL 安裝

mysqladmin version -u root -p

三、源碼編譯方式安裝

3.1 安裝依賴包

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

安裝 Boost 庫,獲取程式包請訪問 Boost 官網

$ cd /usr/local/src/
$ wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
$ tar xf boost_1_59_0.tar.gz -C /usr/local/

3.2 獲取 MySQL 源代碼包

-w1043

$ cd /usr/local/src/
$ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.25.tar.gz

3.3 創建 MySQL 用戶組

$ groupadd mysql
$ useradd -r -g mysql -s /bin/false mysql

3.4 預編譯

$ tar xf mysql-5.7.25.tar.gz
$ cd mysql-5.7.25
$ cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql/data \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_BOOST=/usr/local/boost_1_59_0 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DWITH_CSV_STORAGE_ENGINE=1 \
-DWITH_HEAP_STORAGE_ENGINE=1 \
-DWITH_MYISAMMRG_STORAGE_ENGINE=1 \
-DWITH_ZLIB=system \
-DWITH_EMBEDDED_SERVER=1

更多 cmake 指令參考官方文檔

3.5 編譯安裝

$ make -j `grep processor /proc/cpuinfo | wc -l`
$ make install

3.6 配置文件

準備 MySQL 配置文件,我這裡用的是線上工具生成的 my.cnf 文件,工具鏈接

$ cat /etc/my.cnf

[client]
port = 3306
socket = /data/mysql/mysql.sock

[mysqld]
port = 3306
socket = /data/mysql/mysql.sock
pid_file = /var/run/mysql.pid
datadir = /data/mysql/data
basedir = /usr/local/mysql
default_storage_engine = InnoDB
max_allowed_packet = 128M
max_connections = 2048
open_files_limit = 65535
skip-name-resolve
lower_case_table_names=1
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
innodb_buffer_pool_size = 128M
innodb_log_file_size = 128M
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 0
key_buffer_size = 16M
log-error = /data/mysql/log/mysql_error.log
log-bin = /data/mysql/log/mysql_bin.log
slow_query_log = 1
slow_query_log_file = /data/mysql/log/mysql_slow_query.log
long_query_time = 5
tmp_table_size = 16M
max_heap_table_size = 16M
query_cache_type = 0
query_cache_size = 0
server-id=1

創建數據目錄

$ mkdir -pv /data/mysql/{log,data}
$ chown -R mysql.mysql /data/mysql/

3.7 初始化

$ cd /usr/local/mysql/
$ ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data/

3.8 設置啟動腳本配置環境變數

$ cp support-files/mysql.server /etc/init.d/mysqld
$ chmod +x /etc/init.d/mysqld
$ echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh

3.9 啟動資料庫

$ systemctl enable mysqld
$ systemctl start mysqld
$ systemctl status mysqld
$ ss -tnlp|grep 3306
$ ps aux|grep mysql

3.10 初始化用戶

與二進位方式一樣,初始密碼在錯誤日誌內。

$ mysql_secure_installation

3.11 驗證 MySQL

$ mysqladmin version -uroot -p

以上就是 MySQL 5.7 版本的三種安裝方式,歡迎大家多留言交流。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 作為一個以伺服器為主要市場的操作系統,主要就是對客戶端的請求進行響應,進行處理的。在經歷過系統鏡像安裝和本地配置好ssh功能後,接下來進行伺服器的安裝,這裡我以nginx為主,介紹一下如何安裝nginx和配置站點, 安裝要求 + linux centos7系統 + ssh軟體 nginx簡介 "ng ...
  • 前不久入職實習生,現在在幫著組裡面dalao們跑Case,時不時要上去收一下有木有Dump,每次敲命令太煩人於是逼著自己學寫Shell腳本。一開始真的是很痛苦啊,也沒能搞到書,只能憑網上半真半假的消息照葫蘆畫瓢!廢話少說,上正文! 我是分割線 就是這樣子,在想到什麼就再往上面丟點什麼吧! ...
  • 一、screen 命令不間斷會話 1、安裝screen(從系統鏡像作為yum倉庫安裝) 1.1、載入系統鏡像 1.2、mount /dev/cdrom /media/cdrom/ (掛在系統鏡像) vim /etc/fstab (添加開機啟動項) 1.3、yum倉庫配置 1.3.1、掛載系統鏡像 1 ...
  • 最近一直有安裝虛擬機的想法,今天剛剛知道win10有自帶的Linux子系統,就準備試一下: 首先要保證自己的電腦處於開發者選項: 然後就要在控制面板的程式和功能頁面點擊“啟用或者關閉WIndows功能‘ 然後愉快的等待重啟,對要很愉快! 重啟之後呢,按win+R,輸入cmd進入命令界面,輸入bash ...
  • 本文講述如何通過樹莓派的硬體PWM控制好盈電調來驅動RC車子的前進後退,以及如何驅動伺服電機來控制車子轉向。 1. 好盈電調簡介 車子上的電調型號為:WP-10BLS-A-RTR,在好盈官網並沒有搜到對應手冊,但找到一份通用RC競速車的電調使用說明,不過說明書中並沒有提及信號調製方式,繼續尋找,看到 ...
  • linux中刪除文件內空白行的幾種方法 有時你可能需要在 Linux 中刪除某個文件中的空行。如果是的,你可以使用下麵方法中的其中一個。有很多方法可以做到,但我在這裡只是列舉一些簡單的方法。 你可能已經知道 grep、awk 和 sed 命令是專門用來處理文本數據的工具。 下列 5 種方法可以做到。 ...
  • 1.linux 查看安裝了什麼擴展 2.查看文件位置 3. 查看php.ini位置 ...
  • 轉載自:http://www.cnblogs.com/wainiwann/p/3942203.html 在開發的一個基於rtmp聊天的程式時發現了一個很奇怪的現象。 在windows下當我們執行 closesocket的操作之後,阻塞的 recv會立即返回 1 。 而在linux下當我們執行clos ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...