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的人来说,源码安装是很有必要的,因为明确知道程序每一步执行的命令,对软件的理解及日常维护都会起到融会贯通,举一反三的作用。软件安装过程中,会遇到很多很多的问题,遇到问题,解决问题,且多用脚本工具平台起到以逸待劳的作用,如此不断精进,方可不断成长,团队的工具产品也可不断叠加场景,成为时间的朋友。
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/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环境下载 ...
阅读 1249·2023-01-11 13:20
阅读 1557·2023-01-11 13:20
阅读 1011·2023-01-11 13:20
阅读 1680·2023-01-11 13:20
阅读 3971·2023-01-11 13:20
阅读 2517·2023-01-11 13:20
阅读 1310·2023-01-11 13:20
阅读 3481·2023-01-11 13:20