您好,登錄后才能下訂單哦!
本文主要給大家介紹安裝mysql 5.7.21 二進(jìn)制流程講析,希望可以給大家補(bǔ)充和更新些知識,如有其它問題需要了解的可以持續(xù)在億速云行業(yè)資訊里面關(guān)注我的更新文章的。
適用于CentOS 6.*和CentOS 7.* 系統(tǒng)版本:CentOS 6.8_x86-64 mysql版本:mysql-5.7.21-linux-glibc2.5-x86_64.tar.gz mysql程序安裝路徑:/data/mysql mysql數(shù)據(jù)存放路徑:/data/mysql/data socket存放路徑: /data/mysql/mysql.sock 安裝方式:二進(jìn)制文件安裝
[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 庫。如果這個(gè)庫沒有在本地安裝,數(shù)據(jù)目錄初始化和后續(xù)的云服務(wù)器啟動(dòng)步驟將會(huì)失敗。 2、此用戶僅用于運(yùn)行mysql服務(wù),而不是登錄,因此使用useradd -r和-s /bin/false命令選項(xiàng)來創(chuàng)建對服務(wù)器主機(jī)沒有登錄權(quán)限的用戶。
[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/
[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 # 驗(yàn)證
# 環(huán)境變量添加到全局中時(shí),兩個(gè)軟鏈接可以不用設(shè)置
[root@MYSQL mysql]# ln -s /data/mysql/bin/mysql /usr/bin [root@MYSQL mysql]# ln -s /data/mysql/bin/mysqld /usr/bin
[root@MYSQL mysql]# echo "/data/mysql/lib" > /etc/ld.so.conf.d/mysql.conf [root@MYSQL mysql]# ldconfig # 重新加載動(dòng)態(tài)鏈接庫
如果安裝在/usr/local/mysql/目錄下,則兩個(gè)sed不許執(zhí)行,因?yàn)槟J(rèn)安裝在/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
參考知數(shù)堂葉金榮老師的my.cnf生成器,根據(jù)需要選擇使用,附鏈接: 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:區(qū)分大小寫,1:不區(qū)分大小寫 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 #兩個(gè)table為預(yù)計(jì)建表個(gè)數(shù)的兩倍 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保留天數(shù) 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事務(wù)自動(dòng)執(zhí)行 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自定義在項(xiàng)目sql語句報(bào)錯(cuò)時(shí)開啟 transaction_isolation = REPEATABLE-READ #innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 10469M #innodb_buffer_pool_size物理內(nèi)存的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待優(yōu)化 innodb_flush_log_at_trx_commit = 2 #默認(rèn)值1是每一次事務(wù)提交或事務(wù)外的指令都需要把日志寫入(flush)硬盤 #設(shè)成2是不寫入硬盤而是寫入系統(tǒng)緩存。日志仍然會(huì)每秒flush到硬盤,處理效率會(huì)很高,設(shè)成2只會(huì)在整個(gè)操作系統(tǒng)掛了時(shí)才可能丟數(shù)據(jù)。 innodb_log_buffer_size = 32M innodb_log_file_size = 2G innodb_log_files_in_group = 2 innodb_max_undo_log_size = 4G # 根據(jù)您的服務(wù)器IOPS能力適當(dāng)調(diào)整 # 一般配普通SSD盤的話,可以調(diào)整到 10000 - 20000 # 配置高端PCIe SSD卡的話,則可以調(diào)整的更高,比如 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 后, 可能會(huì)導(dǎo)致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
--initialize-insecure參數(shù):使用空密碼創(chuàng)建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參數(shù):生成隨機(jī)密碼。
[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*#
注意:此時(shí)會(huì)生成一個(gè)臨時(shí)密碼,root@localhost:后面的是臨時(shí)密碼; 如果沒有輸出上面的信息,可以在error.log文件找,或把/etc/my.cnf文件移走;
[root@MYSQL mysql]# grep 'temporary password' /data/mysql/logs/error.log
參考: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
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加載配置服務(wù) [root@MYSQL mysql]# systemctl enable mysqld.service [root@MYSQL mysql]# systemctl is-enabled mysqld enabled
CentOS 6.*
[root@MYSQL mysql]# service mysqld start [root@MYSQL mysql]# service mysqld status # 查看是否運(yùn)行
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 &
[root@MYSQL ~]# cd /data/mysql [root@MYSQL mysql]# ./bin/mysql_secure_installation Securing the MySQL server deployment. Enter password for user root: #輸入初始化時(shí)的臨時(shí)密碼 The existing password for the user account root has expired. Please set a new password. New password: #設(shè)置新密碼 Re-enter new password: #重復(fù)新密碼 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 #是否設(shè)置密碼安全插件(不是DBA,不推薦設(shè)置) 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位;根據(jù)自己設(shè)置 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的現(xià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登錄遠(yuǎn)程 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 #刪除測試數(shù)據(jù)庫 - 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 #重新加載權(quán)限表 Success. All done! [root@MYSQL mysql]#
[root@MYSQL mysql]# mysql -uroot -p mysql> grant all on *.* to 'java'@'%' identified by 'jado@1301'; #授權(quán)并同時(shí)新建用戶 mysql> flush privileges; #刷新權(quán)限 附: create user java identified by '123456'; #新建普通用戶java(密碼:123456) grant all on ras.* to "java"@"%"; #授權(quán):把ras庫內(nèi)的所有權(quán)限授權(quán)給java用戶 revoke all on *.* from "java"@"%"; #取消授權(quán) drop user "java"@"%"; #直接刪除用戶 grant all on *.* to 'java'@'%' identified by 'jado@1301'; #授權(quán)并同時(shí)新建用戶 grant select,insert,update,delete on ras.* to java@”%” Identified by “123456”; #權(quán)限:增刪查改
在對MySQL數(shù)據(jù)庫進(jìn)行遷移的時(shí)候,有時(shí)候也需要遷移源數(shù)據(jù)庫內(nèi)的用戶與權(quán)限。 對于這個(gè)遷移我們可以從mysql.user表來獲取用戶的相關(guān)權(quán)限來生成相應(yīng)的SQL語句,然后在目標(biāo)服務(wù)器上來執(zhí)行生成的SQL語句即可。 說明:mysql中直接通過授權(quán)即可使用對應(yīng)用戶,不必使用創(chuàng)建用戶命令(如CREATE USER 'xxx'@'%' IDENTIFIED BY 'XXX';)先建用戶再授權(quán)。 該腳本可以將所有授權(quán)數(shù)據(jù)到當(dāng)前目錄下的sql腳本(grants.sql)中,使用grants.sql腳本刷到數(shù)據(jù)庫中即可完成授權(quán)數(shù)據(jù)遷移(注意:這里導(dǎo)出的數(shù)據(jù)包含root用戶的授權(quán)關(guān)系,而且導(dǎo)入之后會(huì)把目前已有的數(shù)據(jù)覆蓋掉,請確認(rèn)需要覆蓋再進(jìn)行導(dǎo)入?。。?/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
注意:上述代碼中,需要根據(jù)實(shí)際情況(mysql的root用戶的密碼)替換pwd的值。 將上述代碼拷貝后,新建并貼到shell腳本(如exp_grants.sh)中執(zhí)行該腳本即可完成數(shù)據(jù)庫用戶授權(quán)導(dǎo)出。 以上文件導(dǎo)出后的腳本(grants.sql)中內(nèi)容類似如下片段:
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.導(dǎo)入新數(shù)據(jù)庫:mysql -uroot -p mysql < grants.sql15. END
看了以上關(guān)于安裝mysql 5.7.21 二進(jìn)制流程講析,希望能給大家在實(shí)際運(yùn)用中帶來一定的幫助。本文由于篇幅有限,難免會(huì)有不足和需要補(bǔ)充的地方,如有需要更加專業(yè)的解答,可在官網(wǎng)聯(lián)系我們的24小時(shí)售前售后,隨時(shí)幫您解答問題的。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。