mysql編譯安裝可以根據需要設定參數,按照需求進行定制安裝,并且安裝的版本可以根據項目需要靈活選擇,整體可配置彈性大。其中YUM二進制方式部署配置簡單,可自動解決軟件包之間依賴關系問題,但YUM不能自定義軟件模塊和功能,不能自定義軟件部署路徑,增加后期維護成本。此方案提供了一鍵安裝部署腳本,使腳本安裝省去繁瑣步驟,只需輸入變量即可完成安裝任務,也可作為資源池機器上架初始化的參考。
環境:
硬件為4C/4G/50G
系統版本為redhat6.9
#使用win10自帶sftp命令上傳到服務器:
PSC:Windowssystem32> cd e:
PSE:> sftp root@192.168.56.1
sftp>cd /mysql
sftp>put boost_1_59_0.tar.gz
注:本地目錄(絕對路徑)必須為非中文名稱。
#解壓
tar-xzvf mysql-5.7.21.tar.gz
tar-xzvf boost_1_59_0.tar.gz -C /usr/local/
yuminstall -y make cmake gcc gcc-c++ ncurses ncurses-devel bison
各個包功能的簡單介紹: make mysql源代碼是由C和C++語言編寫,在linux下使用make對源碼進行編譯和構建,要求必須安裝make 3.75或以上版本 gcc GCC是Linux下的C語言編譯工具,mysql源碼編譯完全由C和C++編寫,要求必須安裝GCC4.4.6或以上版本 cmake mysql使用cmake跨平臺工具預編譯源碼,用于設置mysql的編譯參數。如:安裝目錄、數據存放目錄、字符編碼、排序規則等。安裝最新版本即可。 bison Linux下C/C++語法分析器 ncurses 字符終端處理庫 |
mkdir-p /mysql/data && chown mysql.mysql /mysql/data #創建數據目錄
mkdir-p /mysql/log && chown mysql.mysql /mysql/log #創建log目錄
mkdir-p /var/lib/mysql && chown mysql.mysql /var/lib/mysql #創建UNIXSOCK目錄
#創建MySQL用戶和組,并且用戶不能登陸(系統自帶mysql軟件,安裝時會自動創建該用戶,不需要多帶帶手工創建)
groupadd-r mysql && useradd -r -g mysql -s /sbin/nologin -M mysql
#編輯/etc/hosts,添加本機的hosts條目
vi/etc/hosts
192.168.41.2host_name
#一定要關閉selinux
setenforce
vi/etc/security/selinux
#關閉防火墻
servicesiptables stop
iptables–L
#配置yum源并卸載自安裝軟件
yumremove -y mysql.x86_64 mysql-connector-odbc.x86_64 mysql-libs.x86_64mysql-server.x86_64
使用各種參數,預編譯源代碼.進入解壓的MySQL源碼目錄,執行以下命令:
cmake
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_DATADIR=/mysql/data
-DSYSCONFDIR=/etc
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysqld.sock
-DEXTRA_CHARSETS=all
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_MYISAM_STORAGE_ENGINE=1
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_PARTITION_STORAGE_ENGINE=1
-DENABLED_LOCAL_INFILE=1
-DENABLED_PROFILING=1
-DMYSQL_TCP_PORT=3306
-DWITH_DEBUG=0
-DDOWNLOAD_BOOST=1
-DWITH_BOOST=/usr/local/boost_1_59_0
DCMAKE_INSTALL_PREFIX=/usr/local/mysql :安裝路徑 DMYSQL_DATADIR=/data/mysql :數據文件存放位置 DSYSCONFDIR=/etc :my.cnf路徑 DMYSQL_UNIX_ADDR=/var/lib/mysql/mysqld.sock :連接數據庫socket路徑 DEXTRA_CHARSETS=all :安裝所有的字符集 DDEFAULT_CHARSET=utf8 :默認字符 DDEFAULT_COLLATION=utf8_general_ci :排序集 DWITH_MYISAM_STORAGE_ENGINE=1 :支持MyIASM引擎 DWITH_INNOBASE_STORAGE_ENGINE=1 :支持InnoDB引擎 DWITH_PARTITION_STORAGE_ENGINE=1 :安裝支持數據庫分區 DENABLED_LOCAL_INFILE=1 :允許從本地導入數據 DENABLED_PROFILING=1 : DMYSQL_TCP_PORT=3306 :端口 DWITH_DEBUG=0 : DDOWNLOAD_BOOST=1 :允許下載 DWITH_BOOST=/usr/local/boost_1_59_0 :本地boost路徑 |
預編譯完成后,執行下面的命令編譯,安裝:
#指定CPU數量編譯
make-j `grep processor /proc/cpuinfo | wc -l` && make install
注意:編譯安裝時對CPU資源消耗比較高。增加主機的CPU個數,可以減少編譯的時間。
對目錄修改權限,添加service/systemd服務:
chown-R mysql:mysql /usr/local/mysql
cp/usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod+x /etc/init.d/mysqld
#開機自啟
chkconfig--add mysqld
chkconfigmysqld on
將/usr/local/mysql/bin添加進入環境變量,或者直接使用軟鏈接的方式鏈到/usr/local/bin下:
#添加到環境變量
echo"export PATH=/usr/local/mysql/bin:$PATH" >>/etc/bashrc
echo"" >> /etc/bashrc
source~/.bashrc
#使用軟鏈接
ln-s /usr/local/mysql/bin/* /usr/local/bin/
默認MySQL不需要配置文件,編譯時已經配置好了,但是也可以使用配置文件,指定log的位置,編輯vim/etc/my.cnf, 將以下內容添加到文件中:
[mysql] auto-rehash socket=/var/lib/mysql/mysql.sock [mysqld] ####: for global user=mysql basedir=/usr/local/bin datadir=/mysql/data server_id=1517 port=3306 character_set_server=utf8 log_bin_trust_function_creators=ON max_prepared_stmt_count=1048576 log_timestamps=system socket=/var/lib/mysql/mysql.sock read_only=OFF skip_name_resolve=1 auto_increment_increment=1 auto_increment_offset=1 lower_case_table_names=1 secure_file_priv= open_files_limit=65536 max_connections=256 thread_cache_size=64 table_open_cache=4000 table_definition_cache=2000 table_open_cache_instances=32 ####: for binlog binlog_format=ROW log_bin=mysql-bin binlog_rows_query_log_events=ON log_slave_updates=ON expire_logs_days=7 binlog_cache_size=64k binlog_checksum=none sync_binlog=1 slave-preserve-commit-order=ON ####: for error-log log_error=/mysql/log/err.log ####: for general-log general_log=OFF general_log_file=general.log ####: for slow query log slow_query_log=ON slow_query_log_file=slow.log log_queries_not_using_indexes=OFF long_query_time=2.0 ####: for gtid gtid_executed_compression_period=1000 gtid_mode=ON enforce_gtid_consistency=ON ####: for replication skip_slave_start=0 master_info_repository=table relay_log_info_repository=table slave_parallel_type=logical_clock #database | LOGICAL_CLOCK slave_parallel_workers=4 rpl_semi_sync_master_enabled=1 rpl_semi_sync_slave_enabled=1 rpl_semi_sync_master_timeout=1000 plugin_load_add=semisync_master.so plugin_load_add=semisync_slave.so binlog_group_commit_sync_delay=4000 binlog_group_commit_sync_no_delay_count= 10 #binlog_transaction_dependency_tracking= WRITESET transaction_write_set_extraction= XXHASH64 ####: for innodb default_storage_engine=innodb default_tmp_storage_engine=innodb innodb_data_file_path=ibdata1:256M;ibdata2:256M:autoextend innodb_temp_data_file_path=ibtmp1:64M:autoextend innodb_buffer_pool_filename=ib_buffer_pool innodb_log_group_home_dir=./ innodb_log_files_in_group=8 innodb_log_file_size=128M innodb_file_per_table=ON innodb_online_alter_log_max_size=128M innodb_open_files=64000 innodb_page_size=16k innodb_thread_concurrency=0 innodb_read_io_threads=4 innodb_write_io_threads=4 innodb_purge_threads=4 innodb_page_cleaners=4 innodb_print_all_deadlocks=ON innodb_deadlock_detect=ON innodb_lock_wait_timeout=50 innodb_spin_wait_delay=6 innodb_autoinc_lock_mode=2 innodb_flush_sync=OFF innodb_io_capacity=400 innodb_io_capacity_max=2000 #--------Persistent Optimizer Statistics innodb_stats_auto_recalc=ON innodb_stats_persistent=ON innodb_stats_persistent_sample_pages=20 innodb_buffer_pool_instances=2 innodb_adaptive_hash_index=ON innodb_change_buffering=all innodb_change_buffer_max_size=25 innodb_flush_neighbors=1 innodb_flush_method=O_DIRECT innodb_doublewrite=ON innodb_log_buffer_size=128M innodb_flush_log_at_timeout=1 innodb_flush_log_at_trx_commit=1 innodb_buffer_pool_size=2G autocommit=ON #--------innodb scan resistant innodb_old_blocks_pct=37 innodb_old_blocks_time=1000 #--------innodb read ahead innodb_read_ahead_threshold=56 innodb_random_read_ahead=off #--------innodb buffer pool state innodb_buffer_pool_dump_pct=50 innodb_buffer_pool_dump_at_shutdown=ON innodb_buffer_pool_load_at_startup=ON ####for performance_schema performance_schema=on #on performance_schema_consumer_global_instrumentation=on performance_schema_consumer_thread_instrumentation=on performance_schema_consumer_events_stages_current=on performance_schema_consumer_events_stages_history=on performance_schema_consumer_events_stages_history_long=off performance_schema_consumer_statements_digest=on performance_schema_consumer_events_statements_current=on performance_schema_consumer_events_statements_history=on performance_schema_consumer_events_statements_history_long=off performance_schema_consumer_events_waits_current=on performance_schema_consumer_events_waits_history=on performance_schema_consumer_events_waits_history_long=off performance-schema-instrument=memory/%=COUNTED |
以上都完成后,還不能啟動MySQL,如果非要啟動,會報錯.
需要初始化數據庫:/usr/local/mysql/bin/mysqld --initialize --user=mysql--basedir=/usr/local/mysql --datadir=/mysql/data
或者/usr/local/mysql/bin/mysqld--initialize --user=mysql --defaults-file=/etc/my.cnf
--user:指定用戶
--basedir:mysql所在目錄
--datadir:mysql數據庫和表所在的目錄,以及PID文件
初始化后,會有一行提示,冒號后面的是初始密碼root@localhost:password:
Atemporary password is generated for root@localhost: RLbYehfa#60v
以上都配置完成,就可以啟動服務了:
#Redhat 6 使用service
servicemysqld start
#Redhat 7 使用systemd
systemctldaemon-reload
systemctlstart mysqld
將初始密碼修改成自己的密碼,直接在shell中輸入命令
:mysqladmin -uroot -pold_pass password new_pass
或者
mysql-uroot -p
alteruser root@localhost identified by 111111;
vi install_mysql.sh
#!/bin/bash
#需要的安裝介質:Mysql編譯包、boost安裝包
##################################一、環境準備########################################################
#1. 創建目錄
mkdir -p $1/data && chown mysql.mysql $1/data #創建數據目錄
mkdir -p $2/log && chown mysql.mysql $2/log #創建log目錄
mkdir -p /var/lib/mysql && chown mysql.mysql /var/lib/mysql #創建UNIX SOCK目錄
data_dir=$1/data
install_log=$2/install.log
#2. 解壓安裝介質
cd $3
tar -xzvf mysql-5.7.21.tar.gz
tar -xzvf boost_1_59_0.tar.gz -C /usr/local/
#3. 新建Mysql用戶
id mysql
if [ $? -ne 0 ]; then
echo "There are no MySQL users,Now start to create a Mysql user" >> ${install_log}
groupadd -r mysql && useradd -r -g mysql -s /sbin/nologin -M mysql
fi
#4. 卸載預裝軟件
`rpm -qa |grep -ie mysql -ie mariadb |awk {print "rpm -evh --nodeps " $1}` >> ${install_log}
#5. 安裝依賴包
yum clean all
yum list|grep tree
if [ $? -eq 0 ]; then
yum install -y cmake gcc gcc-c++ ncurses ncurses-devel bison >> ${install_log}
else
echo "Failed!!!There is no Yum here,please configure yum!" >> ${install_log}
exit;
fi
#6. 關閉防火墻和selinux
setenforce 0
perl -p -i -e "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
perl -p -i -e "s/SELINUX=permissive/SELINUX=disabled/g" /etc/selinux/config
service iptables stop
##################################二、編譯安裝########################################################################
#1. 預編譯
cmake
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_DATADIR=/mysql/data
-DSYSCONFDIR=/etc
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysqld.sock
-DEXTRA_CHARSETS=all
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_MYISAM_STORAGE_ENGINE=1
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_PARTITION_STORAGE_ENGINE=1
-DENABLED_LOCAL_INFILE=1
-DENABLED_PROFILING=1
-DMYSQL_TCP_PORT=3306
-DWITH_DEBUG=0
-DDOWNLOAD_BOOST=1
-DWITH_BOOST=/usr/local/boost_1_59_0 >> ${install_log}
if [ $? -ne 0 ]; then
exit;
fi
#2. 編譯安裝
make -j `grep processor /proc/cpuinfo | wc -l` && make install >> ${install_log}
if [ $? -ne 0 ]; then
exit;
fi
#3. 開機自啟
chown -R mysql:mysql /usr/local/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
#4. 添加到環境變量的路徑中
ln -s /usr/local/mysql/bin/* /usr/local/bin/
##################################三、數據庫初始化########################################################################
#1. 生成Mysql配置文件
mem_num=`free |grep Mem|awk {print $2}`
buffer_size=`echo ${mem_num}/2|bc`
cp /etc/my.cnf /etc/my.cnf.bak
echo "[mysql]
auto-rehash
socket=/var/lib/mysql/mysql.sock
[mysqld]
####: for global
user=mysql
basedir=/usr/local/bin
datadir=$1/data
server_id=1517
port=3306
character_set_server=utf8
log_bin_trust_function_creators=ON
max_prepared_stmt_count=1048576
log_timestamps=system
socket=/var/lib/mysql/mysql.sock
read_only=OFF
skip_name_resolve=1
auto_increment_increment=1
auto_increment_offset=1
lower_case_table_names=1
secure_file_priv=
open_files_limit=65536
max_connections=256
thread_cache_size=64
table_open_cache=4000
table_definition_cache=2000
table_open_cache_instances=32
####: for binlog
binlog_format=ROW
log_bin=mysql-bin
binlog_rows_query_log_events=ON
log_slave_updates=ON
expire_logs_days=7
binlog_cache_size=64k
binlog_checksum=none
sync_binlog=1
slave-preserve-commit-order=ON
####: for error-log
log_error=$2/log/err.log
####: for general-log
general_log=OFF
general_log_file=general.log
####: for slow query log
slow_query_log=ON
slow_query_log_file=slow.log
log_queries_not_using_indexes=OFF
long_query_time=2.0
####: for gtid
gtid_executed_compression_period=1000
gtid_mode=ON
enforce_gtid_consistency=ON
####: for replication
skip_slave_start=0
master_info_repository=table
relay_log_info_repository=table
slave_parallel_type=logical_clock
slave_parallel_workers=4
rpl_semi_sync_master_enabled=1
rpl_semi_sync_slave_enabled=1
rpl_semi_sync_master_timeout=1000
plugin_load_add=semisync_master.so
plugin_load_add=semisync_slave.so
binlog_group_commit_sync_delay=4000
binlog_group_commit_sync_no_delay_count= 10
#binlog_transaction_dependency_tracking= WRITESET
transaction_write_set_extraction= XXHASH64
####: for innodb
default_storage_engine=innodb
default_tmp_storage_engine=innodb
innodb_data_file_path=ibdata1:256M;ibdata2:256M:autoextend
innodb_temp_data_file_path=ibtmp1:64M:autoextend
innodb_buffer_pool_filename=ib_buffer_pool
innodb_log_group_home_dir=./
innodb_log_files_in_group=8
innodb_log_file_size=128M
innodb_file_per_table=ON
innodb_online_alter_log_max_size=128M
innodb_open_files=64000
innodb_page_size=16k
innodb_thread_concurrency=0
innodb_read_io_threads=4
innodb_write_io_threads=4
innodb_purge_threads=4
innodb_page_cleaners=4
innodb_print_all_deadlocks=ON
innodb_deadlock_detect=ON
innodb_lock_wait_timeout=50
innodb_spin_wait_delay=6
innodb_autoinc_lock_mode=2
innodb_flush_sync=OFF
innodb_io_capacity=400
innodb_io_capacity_max=2000
#--------Persistent Optimizer Statistics
innodb_stats_auto_recalc=ON
innodb_stats_persistent=ON
innodb_stats_persistent_sample_pages=20
innodb_buffer_pool_instances=2
innodb_adaptive_hash_index=ON
innodb_change_buffering=all
innodb_change_buffer_max_size=25
innodb_flush_neighbors=1
innodb_flush_method=O_DIRECT
innodb_doublewrite=ON
innodb_log_buffer_size=128M
innodb_flush_log_at_timeout=1
innodb_flush_log_at_trx_commit=1
innodb_buffer_pool_size=${buffer_size}
autocommit=ON
#--------innodb scan resistant
innodb_old_blocks_pct=37
innodb_old_blocks_time=1000
#--------innodb read ahead
innodb_read_ahead_threshold=56
innodb_random_read_ahead=off
#--------innodb buffer pool state
innodb_buffer_pool_dump_pct=50
innodb_buffer_pool_dump_at_shutdown=ON
innodb_buffer_pool_load_at_startup=ON
####for performance_schema
performance_schema=on
performance_schema_consumer_global_instrumentation=on
performance_schema_consumer_thread_instrumentation=on
performance_schema_consumer_events_stages_current=on
performance_schema_consumer_events_stages_history=on
performance_schema_consumer_events_stages_history_long=off
performance_schema_consumer_statements_digest=on
performance_schema_consumer_events_statements_current=on
performance_schema_consumer_events_statements_history=on
performance_schema_consumer_events_statements_history_long=off
performance_schema_consumer_events_waits_current=on
performance_schema_consumer_events_waits_history=on
performance_schema_consumer_events_waits_history_long=off
performance-schema-instrument=memory/%=COUNTED " > /etc/my.cnf
#2. 初始化
/usr/local/mysql/bin/mysqld --initialize --user=mysql --defaults-file=/etc/my.cnf >> ${install_log}
#3. 打印root臨時密碼
grep "temporary password" $2/log/err.log
#4. 啟動服務
v_release=`cat /etc/redhat-release |awk {print $7}`
if [ ${v_release} -gt 7 ];then
if [ `systemctl status mysqld|grep Active|awk {print $2}` != active ]; then
systemctl daemon-reload
systemctl enable mysqld
systemctl start mysqld
fi
echo "mysqld server is running"
else
if [ `service mysqld status|awk {print $4}` != OK ]; then
service mysqld start
fi
echo "mysqld server is running"
fi
總結
對剛接觸Mysql的人來說,源碼安裝是很有必要的,因為明確知道程序每一步執行的命令,對軟件的理解及日常維護都會起到融會貫通,舉一反三的作用。軟件安裝過程中,會遇到很多很多的問題,遇到問題,解決問題,且多用腳本工具平臺起到以逸待勞的作用,如此不斷精進,方可不斷成長,團隊的工具產品也可不斷疊加場景,成為時間的朋友。
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/130238.html
摘要:需要注意的是按照上的編譯方式,第一次可以安裝成功,卸載后第二次安裝就為提示沒有找到對應的路徑錯誤。要保證和配置的用戶名和用戶組是統一的。 帶圖片博文鏈接 為了避免每次安裝都要折騰好久,記錄一下編譯安裝的詳細教程 PHP環境下載 本文的PHP下載地址: `wget http://cn2.php.net/distributions/php-7.2.0.tar.gz` NGXIN環境下載 ...
摘要:需要注意的是按照上的編譯方式,第一次可以安裝成功,卸載后第二次安裝就為提示沒有找到對應的路徑錯誤。要保證和配置的用戶名和用戶組是統一的。 帶圖片博文鏈接 為了避免每次安裝都要折騰好久,記錄一下編譯安裝的詳細教程 PHP環境下載 本文的PHP下載地址: `wget http://cn2.php.net/distributions/php-7.2.0.tar.gz` NGXIN環境下載 ...
閱讀 1346·2023-01-11 13:20
閱讀 1684·2023-01-11 13:20
閱讀 1132·2023-01-11 13:20
閱讀 1858·2023-01-11 13:20
閱讀 4099·2023-01-11 13:20
閱讀 2704·2023-01-11 13:20
閱讀 1385·2023-01-11 13:20
閱讀 3594·2023-01-11 13:20