溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

安裝mysql 5.7.21 二進制流程講析

發布時間:2020-04-28 10:21:48 來源:億速云 閱讀:266 作者:三月 欄目:MySQL數據庫

本文主要給大家介紹安裝mysql 5.7.21 二進制流程講析,希望可以給大家補充和更新些知識,如有其它問題需要了解的可以持續在億速云行業資訊里面關注我的更新文章的。

1. 說明

適用于CentOS 6.*和CentOS 7.*
系統版本:CentOS 6.8_x86-64    mysql版本:mysql-5.7.21-linux-glibc2.5-x86_64.tar.gz
mysql程序安裝路徑:/data/mysql
mysql數據存放路徑:/data/mysql/data
socket存放路徑: /data/mysql/mysql.sock
安裝方式:二進制文件安裝

2. 新建用戶

[root@MYSQL ~]# yum install libaio numactl -y 
[root@MYSQL ~]# groupadd mysql
[root@MYSQL ~]# useradd -r -g mysql -M -s /bin/false mysql
1、MySQL依賴于libaio 庫。如果這個庫沒有在本地安裝,數據目錄初始化和后續的云服務器啟動步驟將會失敗。
2、此用戶僅用于運行mysql服務,而不是登錄,因此使用useradd -r和-s /bin/false命令選項來創建對服務器主機沒有登錄權限的用戶。

3. 下載解壓

[root@MYSQL ~]# cd /data/
[root@MYSQL data]# wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.21-linux-glibc2.5-x86_64.tar.gz
[root@MYSQL data]# tar -xvf  mysql-5.7.21-linux-glibc2.5-x86_64.tar.gz
[root@MYSQL data]# ln -s mysql-5.7.21-linux-glibc2.5-x86_64  /data/mysql
[root@MYSQL data]# mkdir -p/data/mysql/{logs,tmp}
[root@MYSQL data]# chown -R mysql:mysql /data/mysql/

4. 添加環境變量:

[root@MYSQL data]# cd /data/mysql/
[root@MYSQL mysql]# echo export PATH='${PATH}':/data/mysql/bin > /etc/profile.d/mysql.sh
[root@MYSQL mysql]# source /etc/profile.d/mysql.sh
[root@MYSQL mysql]# echo $PATH                # 驗證
# 環境變量添加到全局中時,兩個軟鏈接可以不用設置
[root@MYSQL mysql]# ln -s /data/mysql/bin/mysql /usr/bin
[root@MYSQL mysql]# ln -s /data/mysql/bin/mysqld /usr/bin

5. 添加MySQL類到系統

[root@MYSQL mysql]# echo "/data/mysql/lib" > /etc/ld.so.conf.d/mysql.conf
[root@MYSQL mysql]# ldconfig            # 重新加載動態鏈接庫

6. 修改啟動腳本

如果安裝在/usr/local/mysql/目錄下,則兩個sed不許執行,因為默認安裝在/usr/local/目錄下;
[root@MYSQL mysql]# sed -i 's#/usr/local/mysql#/data/mysql#g' /data/mysql/bin/mysqld_safe
[root@MYSQL mysql]# sed -i 's#/usr/local/mysql#/data/mysql#g' /data/mysql/support-files/mysql.server

7. 修改配置文件

安裝mysql 5.7.21 二進制流程講析

參考知數堂葉金榮老師的my.cnf生成器,根據需要選擇使用,附鏈接:
http://imysql.com/my-cnf-wizard.html
[root@MYSQL mysql]# vim /etc/my.cnf
[client]
port                               = 3306
socket                             = /data/mysql/mysql.sock
default-character-set               = utf8

[mysqld]
server-id                           = 1
user                               = mysql
port                               = 3306
basedir                            = /data/mysql
datadir                            = /data/mysql/data
socket                             = /data/mysql/mysql.sock
tmpdir                             = /data/mysql/tmp
character-set-server               = utf8mb4
innodb_file_per_table              = 1
lower_case_table_names             = 1
#0:區分大小寫,1:不區分大小寫
skip_name_resolve                  = 1
open_files_limit                   = 65535
back_log                           = 1024
max_connections                    = 512
max_connect_errors                 = 1000000
table_open_cache                   = 60000
table_definition_cache             = 60000
#兩個table為預計建表個數的兩倍
table_open_cache_instances         = 64
thread_stack                       = 512K
external-locking                   = FALSE
max_allowed_packet                 = 32M
sort_buffer_size                    = 16M
join_buffer_size                    = 16M
thread_cache_size                   = 768
query_cache_size                   = 0
query_cache_type                   = 0
interactive_timeout                 = 600
wait_timeout                       = 600
tmp_table_size                     = 96M
max_heap_table_size                = 96M
log_error                          = /data/mysql/logs/error.log
slow_query_log                     = 1
slow_query_log_file                = /data/mysql/logs/slow.log
long_query_time                    = 2
binlog-ignore-db                   = mysql
log-bin                            = /data/mysql/logs/mysql-bin
sync_binlog                        = 0
binlog_cache_size                  = 4M
max_binlog_cache_size              = 2G
max_binlog_size                    = 1G
expire_logs_days                   = 60
#bin-log保留天數
master_info_repository             = TABLE
relay_log_info_repository          = TABLE
gtid_mode                          = on
enforce_gtid_consistency           = 1
log_slave_updates
binlog_format                      = row
relay_log_recovery                 = 1
relay-log-purge                    = 1
key_buffer_size                    = 32M
read_buffer_size                   = 8M
read_rnd_buffer_size               = 16M
bulk_insert_buffer_size            = 64M
myisam_sort_buffer_size            = 128M
myisam_max_sort_file_size          = 10G
myisam_repair_threads              = 1
lock_wait_timeout                  = 3600
explicit_defaults_for_timestamp    = 1
#autocommit                        = 1
#autocommit=1事務自動執行
innodb_thread_concurrency          = 0
innodb_sync_spin_loops             = 100
innodb_spin_wait_delay             = 30
sql_mode                           = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
#sql_mode自定義在項目sql語句報錯時開啟

transaction_isolation              = REPEATABLE-READ
#innodb_additional_mem_pool_size   = 16M
innodb_buffer_pool_size            = 10469M
#innodb_buffer_pool_size物理內存的70%
innodb_buffer_pool_instances       = 8
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
#innodb_data_file_path = ibdata1:1G:autoextend
#innodb_data_file_path待優化
innodb_flush_log_at_trx_commit     = 2
#默認值1是每一次事務提交或事務外的指令都需要把日志寫入(flush)硬盤
#設成2是不寫入硬盤而是寫入系統緩存。日志仍然會每秒flush到硬盤,處理效率會很高,設成2只會在整個操作系統掛了時才可能丟數據。
innodb_log_buffer_size             = 32M
innodb_log_file_size               = 2G
innodb_log_files_in_group          = 2
innodb_max_undo_log_size           = 4G

# 根據您的服務器IOPS能力適當調整
# 一般配普通SSD盤的話,可以調整到 10000 - 20000
# 配置高端PCIe SSD卡的話,則可以調整的更高,比如 50000 - 80000
innodb_io_capacity                 = 4000
innodb_io_capacity_max             = 8000
innodb_flush_neighbors             = 0
innodb_write_io_threads            = 8
innodb_read_io_threads             = 8
innodb_purge_threads               = 4
innodb_page_cleaners               = 4
innodb_open_files                  = 65535
innodb_max_dirty_pages_pct         = 50
innodb_flush_method                = O_DIRECT
innodb_lru_scan_depth              = 4000
innodb_checksum_algorithm          = crc32
#innodb_file_format                = Barracuda
#innodb_file_format_max            = Barracuda
innodb_lock_wait_timeout           = 10
innodb_rollback_on_timeout         = 1
innodb_print_all_deadlocks         = 1
innodb_online_alter_log_max_size   = 4G
internal_tmp_disk_storage_engine   = InnoDB
innodb_stats_on_metadata           = 0


innodb_status_file                 = 1
# 注意: 開啟 innodb_status_output & innodb_status_output_locks 后, 可能會導致log-error文件增長較快
innodb_status_output               = 0
innodb_status_output_locks         = 0

#performance_schema
performance_schema                 = 1
performance_schema_instrument      = '%=on'

#innodb monitor
innodb_monitor_enable="module_innodb"
innodb_monitor_enable="module_server"
innodb_monitor_enable="module_dml"
innodb_monitor_enable="module_ddl"
innodb_monitor_enable="module_trx"
innodb_monitor_enable="module_os"
innodb_monitor_enable="module_purge"
innodb_monitor_enable="module_log"
innodb_monitor_enable="module_lock"
innodb_monitor_enable="module_buffer"
innodb_monitor_enable="module_index"
innodb_monitor_enable="module_ibuf_system"
innodb_monitor_enable="module_buffer_page"
innodb_monitor_enable="module_adaptive_hash"

[mysqldump]
quick
max_allowed_packet                  = 32M

8. 初始化數據庫

--initialize-insecure參數:使用空密碼創建root@localhost。
[root@MYSQL mysql]# cd /data/mysql/
[root@MYSQL mysql]# ./bin/mysqld --initialize-insecure --defaults-file=/etc/my.cnf --basedir=/data/mysql --datadir=/data/mysql/data --user=mysql
或者
--initialize參數:生成隨機密碼。
[root@MYSQL mysql]# cd /data/mysql/
[root@MYSQL mysql]# ./bin/mysqld --initialize --defaults-file=/etc/my.cnf --basedir=/data/mysql --datadir=/data/mysql/data --user=mysql
2017-07-11T07:34:36.210764Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-07-11T07:34:37.826785Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-07-11T07:34:38.275547Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-07-11T07:34:38.487524Z 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: 65189e9f-660b-11e7-912f-b0518e005cf6.
2017-07-11T07:34:38.544417Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-07-11T07:34:38.545337Z 1 [Note] A temporary password is generated for root@localhost: chpta=hXj4*#
注意:此時會生成一個臨時密碼,root@localhost:后面的是臨時密碼;
如果沒有輸出上面的信息,可以在error.log文件找,或把/etc/my.cnf文件移走;
[root@MYSQL mysql]# grep 'temporary password' /data/mysql/logs/error.log

9. 開啟SSL連接(性能降低20%)

參考:https://blog.51cto.com/moerjinrong/2367282

[root@MYSQL mysql]# cd /data/mysql/
[root@MYSQL mysql]# yum install openssl -y
[root@MYSQL mysql]# bin/mysql_ssl_rsa_setup --basedir=/data/mysql --datadir=/data/mysql/data --user=mysql
[root@MYSQL mysql]# cd data
[root@MYSQL mysql]# chown mysql:mysql *.pem
[root@MYSQL mysql]# ll *.pem
-rw------- 1 mysql mysql 1675 3月   8 18:34 ca-key.pem
-rw-r--r-- 1 mysql mysql 1107 3月   8 18:34 ca.pem
-rw-r--r-- 1 mysql mysql 1107 3月   8 18:34 client-cert.pem
-rw------- 1 mysql mysql 1675 3月   8 18:34 client-key.pem
-rw------- 1 mysql mysql 1679 3月   8 18:34 private_key.pem
-rw-r--r-- 1 mysql mysql  451 3月   8 18:34 public_key.pem
-rw-r--r-- 1 mysql mysql 1107 3月   8 18:34 server-cert.pem
-rw------- 1 mysql mysql 1675 3月   8 18:34 server-key.pem

10. 添加到系統服務

CentOS 6.*:
[root@MYSQL ~]# cd /data/mysql
[root@MYSQL mysql]# chmod 755 support-files/mysql.server
[root@MYSQL mysql]# cp support-files/mysql.server  /etc/init.d/mysqld
[root@MYSQL mysql]# chkconfig --add mysqld
[root@MYSQL mysql]# chkconfig mysqld on
[root@MYSQL mysql]# chkconfig --list mysqld
CentOS 7.*:
[root@MYSQL mysql]# vim /usr/lib/systemd/system/mysqld.service
# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# systemd service file for MySQL forking server
#
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql
ExecStart=/data/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 65535

讓systemctl加載配置服務
[root@MYSQL mysql]# systemctl enable mysqld.service
[root@MYSQL mysql]# systemctl is-enabled mysqld
enabled

11. 啟動mysql

CentOS 6.*

[root@MYSQL mysql]# service mysqld start
[root@MYSQL mysql]# service mysqld status             # 查看是否運行

CentOS 7.*

[root@MYSQL mysql]# systemctl start mysqld.service
[root@MYSQL mysql]# systemctl status mysqld.service

或者
[root@MYSQL mysql]#  /data/mysql/bin/mysqld_safe --user=mysql &

12. 配置安全策略:

[root@MYSQL ~]# cd /data/mysql 
[root@MYSQL mysql]# ./bin/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        #是否設置密碼安全插件(不是DBA,不推薦設置)

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: 0       #選擇0,長度大于8位;根據自己設置
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的現有密碼

 ... 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   #刪除測試數據庫
 - 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! 
[root@MYSQL mysql]#

13. 登錄并添加用戶及授權

[root@MYSQL mysql]# mysql -uroot -p
mysql> grant all on *.* to 'java'@'%' identified by 'jado@1301';   #授權并同時新建用戶
mysql> flush privileges;                                   #刷新權限

附:
create user java identified by '123456'; #新建普通用戶java(密碼:123456)
grant all on  ras.* to "java"@"%";      #授權:把ras庫內的所有權限授權給java用戶

revoke all on  *.* from "java"@"%";             #取消授權
drop user "java"@"%";                           #直接刪除用戶                         

grant all on *.* to 'java'@'%' identified by 'jado@1301';   #授權并同時新建用戶
grant select,insert,update,delete on ras.* to java@”%” Identified by “123456”;   #權限:增刪查改

14. 拓展:導出用戶及權限

    在對MySQL數據庫進行遷移的時候,有時候也需要遷移源數據庫內的用戶與權限。
對于這個遷移我們可以從mysql.user表來獲取用戶的相關權限來生成相應的SQL語句,然后在目標服務器上來執行生成的SQL語句即可。
    說明:mysql中直接通過授權即可使用對應用戶,不必使用創建用戶命令(如CREATE USER 'xxx'@'%' IDENTIFIED BY 'XXX';)先建用戶再授權。
    該腳本可以將所有授權數據到當前目錄下的sql腳本(grants.sql)中,使用grants.sql腳本刷到數據庫中即可完成授權數據遷移(注意:這里導出的數據包含root用戶的授權關系,而且導入之后會把目前已有的數據覆蓋掉,請確認需要覆蓋再進行導入?。。?/pre>
vim mysql_user_ migrate.sh
#!/bin/bash
#Function export user privileges
pwd=rootroot
expgrants()
{ 
  mysql -B -u'root' -p${pwd} -N $@ -e "SELECT CONCAT('SHOW GRANTS FOR ''', user, '''@''', host, ''';') AS query FROM mysql.user" | mysql -u'root' -p${pwd} $@ | sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/-- \1 /;/--/{x;p;x;}'
} 
expgrants > ./grants.sql

注意:上述代碼中,需要根據實際情況(mysql的root用戶的密碼)替換pwd的值。
將上述代碼拷貝后,新建并貼到shell腳本(如exp_grants.sh)中執行該腳本即可完成數據庫用戶授權導出。
以上文件導出后的腳本(grants.sql)中內容類似如下片段:


1.  -- Grants for root@%   
2.  GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY PASSWORD '*FAAFFE644E901CFAFAEC7562415E5FAEC243B8B2' WITH GRANT OPTION;  
3.    
4.  -- Grants for skyleo@%   
5.  GRANT ALL PRIVILEGES ON *.* TO 'aaa'@'%' IDENTIFIED BY PASSWORD '*E6A7BF712C9294EEF165FC1CD0AD04FABC5E1136' WITH GRANT OPTION;  
6.    
7.  -- Grants for skyleo1@%   
8.  GRANT ALL PRIVILEGES ON *.* TO 'aaa1'@'%' IDENTIFIED BY PASSWORD '*1E9DC9809EBE1D5089616868F2DE14B375DACF64' WITH GRANT OPTION;  
9.    
10. -- Grants for skyleo2@%   
11. GRANT ALL PRIVILEGES ON *.* TO 'aaa2'@'%' IDENTIFIED BY PASSWORD '*A601FAAA6AB2D539501BA7FE6E816D499207BA76' WITH GRANT OPTION;
12.
導入新數據庫:
mysql -uroot -p mysql < grants.sql

15. END

看了以上關于安裝mysql 5.7.21 二進制流程講析,希望能給大家在實際運用中帶來一定的幫助。本文由于篇幅有限,難免會有不足和需要補充的地方,如有需要更加專業的解答,可在官網聯系我們的24小時售前售后,隨時幫您解答問題的。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女