您好,登錄后才能下訂單哦!
mysql tar 源碼編譯安裝操作
在下述的操作系統(tǒng)為CentOS 7.2,mysql版本為mysql-5.7.17
1.安裝前進(jìn)行依賴包安裝
yum -y install gcc gcc-c++ ncurses ncurses-devel cmake bison bison-devel openssl openssl-devel pcre pcre-devel
2.下載源碼包
2.1 建議到官方網(wǎng)站下載http://dev.mysql.com/downloads/mysql/,選擇“Source Code”。下載的過程,需要注冊(cè)一個(gè)Oracle賬戶,如果有直接登錄就好
2.2 提供一個(gè)下載地址:http://101.96.10.47/dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.17.tar.gz
或者h(yuǎn)ttps://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
新版本的源碼增加了boost庫,如果你下的源碼里面沒有需要下載這個(gè)庫,否則會(huì)報(bào)錯(cuò),故需進(jìn)行下載
3.新建MySQL用戶和用戶組
groupadd -r mysql
useradd -r -g mysql mysql
4.進(jìn)入安裝步驟
tar -zvxf mysql-boost-5.7.17.tar.gz
cd mysql-5.7.17
新建MySQL數(shù)據(jù)庫數(shù)據(jù)文件目錄
mkdir -p /www/mysql-data
mkdir -p /www/mysql-logs
mkdir -p /www/mysql-temp
進(jìn)行預(yù)編譯
cmake -DCMAKE_INSTALL_PREFIX=/www/mysql \
-DMYSQL_DATADIR=/www/mysql-data \
-DMYSQL_UNIX_ADDR=/www/mysql/mysql.sock \
-DWITH_BOOST=/www/boost_1_59_0 \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DTRACE=0 \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_EMBEDDED_SERVER=1
上述配置解釋:
-DCMAKE_INSTALL_PREFIX=/www/mysql //設(shè)置安裝目錄
-DMYSQL_DATADIR=/www/mysql/data //設(shè)置數(shù)據(jù)庫存放目錄
-DMYSQL_UNIX_ADDR=/www/mysql/mysql.sock //設(shè)置UNIX socket目錄
-DDEFAULT_CHARSET=utf8mb4 //設(shè)置默認(rèn)字符集
-DDEFAULT_COLLATION=utf8mb4_general_ci //設(shè)置默認(rèn)校對(duì)規(guī)則
-DWITH_INNOBASE_STORAGE_ENGINE=1 //添加InnoDB引擎支持
-DSYSCONFDIR=/etc //設(shè)置my.cnf配置文件的所在目錄,默認(rèn)為安裝目錄,沒有的話,後期可添加一份至此
進(jìn)行編譯安裝
make && make install
#安裝完成后清理一下零時(shí)文件
make clean
設(shè)置啟動(dòng)腳本,開機(jī)自啟動(dòng)(即將mysql做成服務(wù))
#拷貝可執(zhí)行文件到指定的目錄下,并修改名字為mysqld
cp /www/mysql/support-files/mysql.server /etc/init.d/mysqld
cp /www/mysql/bin/mysqld /usr/bin/mysqld
#授予可執(zhí)行的權(quán)限
chmod +x /etc/init.d/mysqld
#設(shè)置為開機(jī)啟動(dòng)
systemctl enable mysqld
修改mysql的可執(zhí)行目錄
chown -Rf mysql:mysql /www/mysql
chown -Rf mysql:mysql /www/mysql-data
chown -Rf mysql:mysql /www/mysql-logs
chown -Rf mysql:mysql /www/mysql-temp
配置文件即修改/etc/my.cnf
#參考,具體里面的參數(shù)說明,請(qǐng)自行網(wǎng)上搜索
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
skip-external-locking
skip-name-resolve
user = mysql
port = 3306
basedir = /www/mysql
datadir = /www/mysql-data
tmpdir = /www/mysql-temp
# server_id = .....
socket = /www/mysql/mysql.sock
log-error = /www/mysql-logs/mysql_error.log
pid-file = /www/mysql-data/mysql.pid
open_files_limit = 10240
back_log = 600
max_connections=500
max_connect_errors = 6000
wait_timeout=605800
#open_tables = 600
#table_cache = 650
#opened_tables = 630
max_allowed_packet = 32M
sort_buffer_size = 4M
join_buffer_size = 4M
thread_cache_size = 300
query_cache_type = 1
query_cache_size = 256M
query_cache_limit = 2M
query_cache_min_res_unit = 16k
tmp_table_size = 256M
max_heap_table_size = 256M
key_buffer_size = 256M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
lower_case_table_names=1(不區(qū)分大小寫設(shè)定)
default-storage-engine = INNODB
innodb_buffer_pool_size = 1G
innodb_log_buffer_size = 32M
innodb_log_file_size = 128M
innodb_flush_method = O_DIRECT
#####################
long_query_time= 2
slow-query-log = on
slow-query-log-file = /www/mysql-logs/mysql-slow.log
[mysqldump]
quick
max_allowed_packet = 32M
character_set_server = utf8
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character_set_server = utf8
備註:上述character_set_server = utf8屬於解決亂碼問題
添加環(huán)境變量 vi /etc/profile
#末尾添加以下內(nèi)容
#mysql env
export PATH=$PATH:/www/mysql/bin:/www/mysql/lib
使得設(shè)置文件生效 source /etc/profile
初始化數(shù)據(jù)庫
mysqld --initialize-insecure --user=mysql --basedir=/www/mysql --datadir=/www/mysql-data
備注:
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ù)文件
或者使用下列語句:
mysql_install_db --user=mysql --basedir=/www/mysql --datadir=/www/mysql-data
啟動(dòng)數(shù)據(jù)庫
systemctl start mysqld
查看數(shù)據(jù)庫狀態(tài)
systemctl status mysqld
查看mysql服務(wù)進(jìn)程和端口
ps -ef | grep mysql
netstat -tunpl | grep 3306
設(shè)置數(shù)據(jù)庫root用戶密碼
MySQL和Oracle數(shù)據(jù)庫一樣,數(shù)據(jù)庫也默認(rèn)自帶了一個(gè)root用戶(這個(gè)和當(dāng)前Linux主機(jī)上的
root用戶是完全不搭邊的),我們?cè)谠O(shè)置好MySQL數(shù)據(jù)庫的安全配置后初始化root用戶的密碼。
配制過程中,一路輸入 y 就行了。這里只說明下MySQL5.7.17版本中,用戶密碼策略分成低級(jí)
LOW 、中等 MEDIUM 和超強(qiáng) STRONG 三種,推薦使用中等 MEDIUM 級(jí)別!當(dāng)然也可以不進(jìn)行等
級(jí)設(shè)置
查閱官方文檔后發(fā)現(xiàn)有以下三種密碼策略:
政策測(cè)試執(zhí)行
0 ---LOW長(zhǎng)度
1 ---MEDIUM長(zhǎng)度; 數(shù)字,小寫/大寫和特殊字符
2 ---STRONG長(zhǎng)度; 數(shù)字,小寫/大寫和特殊字符; 字典文件
備註:後續(xù)的用戶相關(guān)密碼將需要按照此標(biāo)準(zhǔn)來執(zhí)行,但是也可以進(jìn)行相關(guān)修改
mysql_secure_installation
除了
Disallow root login remotely
Remove test database and accesss to it 可以為n,
其他都是y
進(jìn)入數(shù)據(jù)庫
mysql -uroot -p密碼
創(chuàng)建其他用戶
mysql> create user 'hua'@'localhost' identified by 'hlj123';(後續(xù)可以修改)
設(shè)置存儲(chǔ)引擎
mysql> set storage_engine=INNODB;
創(chuàng)建數(shù)據(jù)庫
mysql> create database zgz character set utf8;
查看是否建立成功
mysql> show databases;
賦予用戶相關(guān)數(shù)據(jù)庫權(quán)限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'10.134.100.174' IDENTIFIED BY 'FLM@88' WITH GRANT OPTION;
mysql> flush privileges;
檢驗(yàn)是否賦予遠(yuǎn)程連接數(shù)據(jù)庫權(quán)限已開通
mysql -h 10.134.100.174 -uroot -pFLM@88 zgz
如果進(jìn)入數(shù)據(jù)庫說明成功,否則重啟mysql服務(wù)即可再次嘗試
若是允許該用戶通過任意服務(wù)器可進(jìn)行連接相關(guān)數(shù)據(jù)庫
mysql> GRANT ALL PRIVILEGES ON databasename.tables TO 'root'@'%' IDENTIFIED BY 'Foxconn@88' WITH GRANT OPTION;
mysql> flush privileges;
導(dǎo)出你需要的數(shù)據(jù)庫
mysqldump -h 10.157.136.134 -uroot -proot --events --ignore-table=mysql.event --default-character-set=UTF8 jspxcms >zgz.sql
提前建立一個(gè)空數(shù)據(jù)庫來存放需要導(dǎo)入的數(shù)據(jù),後只需要執(zhí)行數(shù)據(jù)庫導(dǎo)入命令
mysql> use zgz;
mysql> source /root/zgz.sql(此處為你放置zgz.sql的存放位置)
確定數(shù)據(jù)表是否創(chuàng)建成功,即數(shù)據(jù)文件是否導(dǎo)入成功
mysql> show tables;
當(dāng)你需要查看當(dāng)前密碼策略,可進(jìn)行下列操作:
mysql> show VARIABLES like "%password%"
+---------------------------------------+---------+
| Variable_name | Value |
|---------------------------------------+---------|
| default_password_lifetime | 0 |
| disconnect_on_expired_password | ON |
| log_builtin_as_identified_by_password | OFF |
| mysql_native_password_proxy_users | OFF |
| old_passwords | 0 |
| report_password | |
| sha256_password_proxy_users | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+---------------------------------------+---------+
13 rows in set
Time: 0.030s
修改默認(rèn)密碼策略(當(dāng)然實(shí)際環(huán)境是不推薦修改為更低安全策略的)
mysql> set global validate_password_policy = 0;
validate_password_number_count指定了密碼中數(shù)據(jù)的長(zhǎng)度,
validate_password_special_char_count指定了密碼中特殊字符的長(zhǎng)度,
validate_password_mixed_case_count指定了密碼中大小字母的長(zhǎng)度。
這些參數(shù),默認(rèn)值均為1,所以validate_password_length最小值為4,
如果你顯示指定validate_password_length的值小于4,盡管不會(huì)報(bào)錯(cuò),
但validate_password_length的值將設(shè)為4。
修改validate_password_number_count,validate_password_special_char_count,
validate_password_mixed_case_count中任何一個(gè)值,則validate_password_length將進(jìn)行動(dòng)態(tài)修改
另,可通過my.cnf配置文件設(shè)置密碼策略的級(jí)別
vi /etc/my.cnf
[mysqld]
validate_password_policy=2
validate_password_policy設(shè)置mysql啟動(dòng)的時(shí)候,密碼策略級(jí)別如果設(shè)置為3,那么需要指定字典文件
當(dāng)然你也可以通過my.cnf配置文件關(guān)閉validate_password插件
只需要添加一行
validate_password = off
編輯完配置文件后,重啟mysqld服務(wù)即可生效。
mysql > show VARIABLES like "validate_password%"
+-----------------+---------+
| Variable_name | Value |
|-----------------+---------|
+-----------------+---------+
0 rows in set
Time: 0.008s
關(guān)閉validate_password插件后,就沒有了validate_password的一些參數(shù)變量。
MySQL新版本默認(rèn)監(jiān)聽在IPv6的地址族上。更改為監(jiān)聽I(yíng)Pv4地址族,
修改my.cnf添加一行配置:bind-address = 0.0.0.0
重啟mysqld即可
mysql主主複製配置
首先分別在兩臺(tái)服務(wù)器上安裝上述的安裝mysql的步驟,確保有兩臺(tái)服務(wù)器可使用
故數(shù)據(jù)庫服務(wù)器環(huán)境:
+-------------------+---------------------------------------------------
| |服務(wù)器節(jié)點(diǎn)1 |服務(wù)器節(jié)點(diǎn)2 |
+-------------------+------------------------+-------------------------+
|OS |CentOS 7.2 |CentOS 7.2 |
+-------------------+------------------------+-------------------------+
|DB Version |MySQL 5.7.17 |MySQL 5.7.17 |
+-------------------+------------------------+-------------------------+
|HostName |Linux01 |Linux02 |
+-------------------+------------------------+-------------------------+
|IPADDR |172.16.100.69 |172.16.100.70 |
+-------------------+------------------------+-------------------------+
創(chuàng)建復(fù)制需要的用戶,用戶需要slave權(quán)限(必要),file,select權(quán)限(可選)
mysql> grant replication slave, file, select on *.* to 'mysql_sync'@'172.16.100.69' identified by 'FLM88'
mysql> grant replication slave, file, select on *.* to 'mysql_sync'@'172.16.100.70' identified by 'FLM88'
備注:如果想要在Slave上有權(quán)限執(zhí)行 "LOAD TABLE FROM MASTER" 或
"LOAD DATA FROM MASTER" 語句的話,必須授予全局的 FILE 和 SELECT 權(quán)限。
上述的語句就是進(jìn)行了創(chuàng)建了同步數(shù)據(jù)庫用戶mysql_sync,密碼及相關(guān)權(quán)限的設(shè)定
分別修改兩臺(tái)服務(wù)器上面的配置文件vi /etc/my.cnf
172.16.100.69上的my.cnf:
log-bin=mysql-bin-db01
server-id=11
log-slave-updates
slave-skip-errors=all
auto_increment_increment=2
auto_increment_offset=1
172.16.100.70上的my.cnf:
log-bin=mysql-bin-db02
server-id=12
log-slave-updates
slave-skip-errors=all
auto_increment_increment=2
auto_increment_offset=2
備註:server-id兩個(gè)服務(wù)器必須配置不一樣,
auto_increment_increment配置每個(gè)節(jié)點(diǎn)中的自動(dòng)增長(zhǎng)字段每次增長(zhǎng)的步長(zhǎng)
auto_increment_offset配置各個(gè)節(jié)點(diǎn)自動(dòng)增長(zhǎng)字段的初始值從多少開始
如果我們不設(shè)置這兩個(gè)變量的話,含有自動(dòng)增長(zhǎng)字段的表將在多個(gè)服務(wù)器上面各自維護(hù),會(huì)出現(xiàn)重復(fù)的情況導(dǎo)致復(fù)制出錯(cuò)。
如上面的設(shè)置,A節(jié)點(diǎn)自增長(zhǎng)的字段產(chǎn)生的值將會(huì)是 1,3,5....
而B節(jié)點(diǎn)將是2,4,6......
重啟兩臺(tái)數(shù)據(jù)庫實(shí)例
systemctl restart mysqld
在兩個(gè)數(shù)據(jù)庫中執(zhí)行:show master status;查看主服務(wù)器狀態(tài):
172.16.100.69(節(jié)點(diǎn)1):
mysql> show master status;
+-----------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------------+----------+--------------+------------------+-------------------+
| mysql-bin-db01.000001 | 1096 | | | |
+-----------------------+----------+--------------+------------------+-------------------+
1 row in set (3.71 sec)
172.16.100.70(節(jié)點(diǎn)2):
mysql> show master status;
+-----------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------------+----------+--------------+------------------+-------------------+
| mysql-bin-db02.000001 | 625 | | | |
+-----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
配置從服務(wù)器Slave(兩個(gè)節(jié)點(diǎn)分別執(zhí)行)
節(jié)點(diǎn)1:
mysql> change master to master_host='172.16.100.70',master_user='mysql_sync',
master_password='Foxconn88',master_log_file='mysql-bin-db02.000001',master_log_pos=625;
mysql> start slave; //啟動(dòng)從服務(wù)器復(fù)制功能
節(jié)點(diǎn)2:
mysql> change master to master_host='172.16.100.69',master_user='mysql_sync',
master_password='Foxconn88',master_log_file='mysql-bin-db01.000001',master_log_pos=1096;
mysql> start slave; //啟動(dòng)從服務(wù)器復(fù)制功能
檢查狀態(tài):
mysql>show slave status\G
備註:不出問題,可以在各自的數(shù)據(jù)庫中查看到相關(guān)互備的master
因?yàn)槭窃创a安裝,所以需要將mysqldump添加進(jìn)入服務(wù),直接做個(gè)鏈接即可
ln /www/mysql/bin/mysqldump /usr/bin/mysqldump
設(shè)定定時(shí)備份數(shù)據(jù)庫,採用crontab執(zhí)行腳本進(jìn)行
#!/usr/bin/env bash
#a
# Author: Zhang Huangbin (zhb@iredmail.org)
# Date: 16/09/2007
KEEP_DAYS='90'
export BACKUP_ROOTDIR="/data0/db_bak/"
export MYSQL_USER="root"
export MYSQL_PASSWD="FLM@88"
export DATABASES="zgz"
export DB_CHARACTER_SET="utf8"
export PATH='/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin'
export CMD_DATE='/bin/date'
export CMD_DU='du -sh'
export CMD_COMPRESS='bzip2 -9'
export COMPRESS_SUFFIX='bz2'
export CMD_MYSQLDUMP='mysqldump'
export CMD_MYSQL='mysql'
export YEAR="$(${CMD_DATE} +%Y)"
export MONTH="$(${CMD_DATE} +%m)"
export DAY="$(${CMD_DATE} +%d)"
export TIME="$(${CMD_DATE} +%H:%M:%S)"
export TIMESTAMP="${YEAR}-${MONTH}-${DAY}-${TIME}"
export BACKUP_SUCCESS='YES'
export BACKUP_DIR="${BACKUP_ROOTDIR}/mysql/${YEAR}/${MONTH}/${DAY}"
mkdir -p $BACKUP_ROOTDIR
mysqldump -h 10.134.100.174 -uroot -pFLM@88
--events --ignore-table=mysql.event --default-character-set=UTF8 zgz>$BACKUP_ROOTDIR$TIMESTAMP.sql
上述只是相應(yīng)例子,可進(jìn)行適當(dāng)修改即可使用。
因?yàn)樽鲞@個(gè)MySQL集群的時(shí)間有點(diǎn)久遠(yuǎn)的,所以現(xiàn)在寫的可能存在一些錯(cuò)誤,如果大家發(fā)現(xiàn),歡迎指出,謝謝!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。