mysql 5.7.21 二進制安裝
阿新 • • 發佈:2018-03-29
MySQL1. 說明
適用於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 -y # 安裝依賴包 [root@MYSQL ~]# groupadd mysql [root@MYSQL ~]# useradd -r -g mysql -M -s /bin/false mysql
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]# mv 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. 修改配置文件
參考知數堂葉金榮老師的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 = 0.1 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@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:後面的是臨時密碼,如果沒有輸出上面的信息,請把/etc/my.cnf文件移走;
9. 開啟SSL連接(可選)
[root@MYSQL mysql]# cd /data/mysql/ [root@MYSQL mysql]# bin/mysql_ssl_rsa_setup --basedir=/data/mysql --datadir=/data/mysql/data --user=mysql
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 [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=${BASEDIR}/bin/mysqld --defaults-file=/etc/my.cnf LimitNOFILE = 5000 [root@MYSQL mysql]# systemctl enable mysqld.service [root@MYSQL mysql]# systemctl list-unit-files | grep mysql
11. 啟動mysql
[root@MYSQL mysql]# /data/mysql/bin/mysqld_safe & # 守護進程能在崩潰後自動重啟,推薦
或者
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
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用戶的授權關系,而且導入之後會把目前已有的數據覆蓋掉,請確認需要覆蓋再進行導入!!):
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 二進制安裝