一涨颜、安裝準(zhǔn)備
安裝依賴包
# yum? -y? install? gcc gcc-c++ ncurses ncurses-devel cmake bison
相關(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 或以上版本
gcc:是 Linux 下的 C 語言編譯工具结胀,mysql 源碼編譯完全由 C 和 C++編寫,要求必須安裝GCC
bison:Linux 下 C/C++語法分析器
ncurses:字符終端處理庫
二责循、下載相應(yīng)源碼包
#? wget?https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
#? wget?https://cdn.mysql.com//archives/mysql-5.7/mysql-5.7.9.tar.gz
三糟港、新建MySQL用戶和用戶組
# groupadd? -r? mysql
#? useradd? -r -g? mysql? -s? /sbin/nologin? ? -M? mysql
四、預(yù)編譯
#? tar? -zxf? boost_1_59_0.tar.gz
#? tar? -zxf? mysql-5.7.11.tar.gz
#? mkdir -p /data/mysql
# cd mysql-5.7.11
# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql? \
-DSYSCONFDIR=/etc? \
-DMYSQL_USER=mysql \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/tem/mysql.sock? \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_DEBUG=0 \
-DMYSQL_MAINTAINER_MODE=0 \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=../boost_1_59_0 \
-DWITH_SSL:STRING=bundled \
-DWITH_ZLIB:STRING=bundled
#################################################################################
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ ? [MySQL安裝的根目錄]
-DMYSQL_DATADIR=/data/mysql \ ? ? ? ? ? ? ? ? [MySQL數(shù)據(jù)庫文件存放目錄]
-DSYSCONFDIR=/etc \ [MySQL配置文件所在目錄]
-DMYSQL_USER=mysql \ ? ? ? [MySQL用戶名]
-DWITH_MYISAM_STORAGE_ENGINE=1 \ [MySQL的數(shù)據(jù)庫引擎]
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ [MySQL的數(shù)據(jù)庫引擎]
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \ ? [MySQL的數(shù)據(jù)庫引擎]
-DWITH_MEMORY_STORAGE_ENGINE=1 \ [MySQL的數(shù)據(jù)庫引擎]
-DWITH_READLINE=1 \? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [MySQL的readline library]
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock\? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [MySQL的通訊目錄]
-DMYSQL_TCP_PORT=3306 \ ? ? ? ? [MySQL的監(jiān)聽端口]
-DENABLED_LOCAL_INFILE=1 \ ? ? ? ? [啟用加載本地?cái)?shù)據(jù)]
-DENABLE_DOWNLOADS=1 \ ? ? [編譯時(shí)允許自主下載相關(guān)文件]
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \ ? ? ? ? [使MySQL支持所有的擴(kuò)展字符]
-DDEFAULT_CHARSET=utf8 \ [設(shè)置默認(rèn)字符集為utf8]
-DDEFAULT_COLLATION=utf8_general_ci \ ? [設(shè)置默認(rèn)字符校對(duì)]
-DWITH_DEBUG=0 \ [禁用調(diào)試模式]
-DMYSQL_MAINTAINER_MODE=0 \
-DDOWNLOAD_BOOST=1 \ ? [允許在線更新boost庫]
-DWITH_BOOST=../boost_1_59_0 \? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [指定boost安裝路徑]
-DWITH_SSL:STRING=bundled \ [通訊時(shí)支持ssl協(xié)議]
-DWITH_ZLIB:STRING=bundled ? [允許使用zlib library]
#######################################################################################
五院仿、編譯安裝
#? make -j? `grep processor /proc/cpuinfo | wc -l`
//編譯很消耗系統(tǒng)資源秸抚,小內(nèi)存可能編譯通不過
# make install
// 花費(fèi)時(shí)間會(huì)較長
如果中途編譯失敗了,需要?jiǎng)h除cmake生成的預(yù)編譯配置參數(shù)的緩存文件和make編譯后生成的文件歹垫,再重新編譯剥汤。
#? make? clean
# rm -rf? CMakeCache.txt
六、更改目錄權(quán)限
# chown? -R? mysql:mysql? ? /usr/local/mysql
# chown? -R? mysql:mysq? ? l/data/mysql
七排惨、設(shè)置啟動(dòng)腳本吭敢,開機(jī)自啟動(dòng)
# cp? /usr/local/mysql/support-files/mysql.server? ? /etc/init.d/mysqld
# vim? /etc/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql
# chmod +x? /etc/init.d/mysqld
# chkconfig? ? --add? mysqld
# chkconfig mysqld? on
八,修改環(huán)境變量暮芭,添加庫文件路徑
# vim? /etc/profile
export? PATH=/usr/local/mysql/bin:$PATH
# source /etc/profile
# vim /etc/ld.so.conf
/usr/local/mysql/lib
# ldconfig
九鹿驼、自定義配置文件
/etc/my.cnf,僅供參考
cat > /etc/my.cnf << EOF
[client]
port = 3306
socket= /tmp/mysql.sock
default-character-set = utf8mb4
[mysqld]
port = 3306
socket= /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql
pid-file= /data/mysql/mysql.pid
user= mysql
bind-address = 0.0.0.0
server-id= 1
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
#skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30
log_error = /data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
EOF
十辕宏、初始化數(shù)據(jù)庫
# /usr/local/mysql/bin/mysqld? --initialize-insecure? --user=mysql? --basedir=/usr/local/mysql? --datadir=/data/mysq
注:
MySQL之前版本mysql_install_db是在mysql_basedir/script下
MySQL 5.7直接放在了mysql_install_db/bin目錄下畜晰。
"–initialize"已廢棄,生成一個(gè)隨機(jī)密碼(~/.mysql_secret)
"--initialize-insecure "不會(huì)生成密碼
"–datadir"目錄下不能有數(shù)據(jù)文件
十一、啟動(dòng)數(shù)據(jù)庫
# systemctl start mysqld
# systemctl status mysqld
# systemctl enable mysqld
十二瑞筐、查看MySQL服務(wù)進(jìn)程和端口
# netstat? -pantu | grep 3306
# ps? -elf? | grep? mysqld
十三凄鼻、設(shè)置數(shù)據(jù)庫root用戶密碼
MySQL和Oracle數(shù)據(jù)庫一樣,數(shù)據(jù)庫也默認(rèn)自帶了一個(gè) root 用戶(這個(gè)和當(dāng)前Linux主機(jī)上的root用戶是完全不搭邊的)聚假,我們在設(shè)置好MySQL數(shù)據(jù)庫的安全配置后初始化root用戶的密碼野宜。配制過程中,一路輸入 y 就行了魔策。
這里只說明下MySQL5.7版本中匈子,用戶密碼策略分成低級(jí) LOW 、中等 MEDIUM 和超強(qiáng) STRONG 三種闯袒,推薦使用中等 MEDIUM 級(jí)別虎敦!
# mysql_secure_installation
十四、登錄數(shù)據(jù)路
# mysql? -u root? -h localhost -p
十五政敢、selinux 防火請(qǐng)?jiān)O(shè)置
selinux:
# sed-i"s/SELINUX=enforcing/SELINUX=disabled/"/etc/selinux/config
#setenforce 0
iptables:
# iptables -A INPUT? -m state --state NEW -m tcp? -p tcp --dport 3306 -j ACCEPT
# iptables-save
firewalld:
#?firewall-cmd --zone=public --add-port=3306/tcp--permanent
#?firewall-cmd --reload