编译安装MySQL
前言:mysql编译安装可以根据需要设定参数,按照需求进行定制安装,并且安装的版本可以根据项目需要灵活选择,整体可配置弹性大。其中YUM二进制方式部署配置简单,可自动解决软件包之间依赖关系问题,但YUM不能自定义软件模块和功能,不能自定义软件部署路径,增加后期维护成本。此方案提供了一键安装部署脚本,使脚本安装省去繁琐步骤,只需输入变量即可完成安装任务,也可作为资源池机器上架初始化的参考。或者直接用
编译安装MySQL
前言:
mysql编译安装可以根据需要设定参数,按照需求进行定制安装,并且安装的版本可以根据项目需要灵活选择,整体可配置弹性大。其中YUM二进制方式部署配置简单,可自动解决软件包之间依赖关系问题,但YUM不能自定义软件模块和功能,不能自定义软件部署路径,增加后期维护成本。此方案提供了一键安装部署脚本,使脚本安装省去繁琐步骤,只需输入变量即可完成安装任务,也可作为资源池机器上架初始化的参考。
一、安装前准备
1.1上传安装介质
#使用win10自带sftp命令上传到服务器:
PSC:\Windows\system32> cd e:\
PSE:> sftp root@192.168.161.12
sftp>cd /mysql
sftp>put boost_1_59_0.tar.gz
注:本地目录(绝对路径)必须为非中文名称。
或者直接用终端工具将包上传
1.2 解包
[root@localhost opt]# tar zxvf boost_1_59_0.tar.gz -C /usr/local/
[root@localhost opt]# mv /usr/local/boost_1_59_0/ /usr/local/boost
[root@localhost opt]# tat zxvf mysql-5.7.17.tar.gz
1.3 安装依赖环境包
[root@localhost opt]# yum install -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 字符终端处理库
1.4 创建相关的目录以及用户和组
#创建MySQL用户和组,并且用户不能登陆(系统自带mysql软件,安装时会自动创建该用户,不需要单独手工创建)
[root@localhost opt]# groupadd -r mysql && useradd -r -g mysql -s /sbin/nologin -M mysql
[root@localhost opt]# mkdir -p /mysql/data && chown mysql:mysql /mysql/data ##创建数据目录
[root@localhost opt]# mkdir -p /var/lib/mysql && chown mysql:mysql /var/lib/mysql ## 创建UNIXSOCK目录
[root@localhost opt]# mkdir -p /mysql/log && chown mysql:mysql /mysql/log ##创建log目录
1.5 修改 /etc/hosts
编辑 /etc/hosts.添加本机的hosts 条目
[root@localhost opt]# vim /etc/hosts
192.168.161.13 host_name
1.6 关闭防火墙与增强
[root@localhost opt]# setenforce 0 ## 关闭增强
[root@localhost opt]# service iptables stop ##关闭iptables防火墙
[root@localhost opt]# service firewalld stop ##关闭firewalld防火墙
[root@localhost opt]# iptables -L ##查看状态
[root@localhost opt]# systemctl status firewalld
1.7 卸载自安装软件
配置yum源并卸载自安装软件
[root@localhost opt]# yum remove -y mysql.x86_64 mysql-connector-odbc.x86_64 mysql-libs.x86_64mysql-server.x86_64
1.8 编译安装
** 预编译 **
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DSYSCONFDIR=/etc -DSYSTEMD_PID_DIR=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/usr/local/mysql/data -DWITH_BOOST=/usr/local/boost -DWITH_SYSTEMD=1
详解
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:本地boost路径
编译安装
预编译完成后,执行下面的命令编译,安装:
#指定CPU数量编译
make-jgrep processor /proc/cpuinfo | wc -l
&& make install
注意:编译安装时对CPU资源消耗比较高。增加主机的CPU个数,可以减少编译的时间。
1.9 添加开机自启
对目录修改权限,添加service/systemd服务:
[root@localhost mysql-5.7.17]# chown -R mysql:mysql /usr/local/mysql
[root@localhost mysql-5.7.17]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql-5.7.17]# chmod +x /etc/init.d/mysqld
设置开机自启
[root@localhost mysql-5.7.17]# chkconfig --add mysqld
[root@localhost mysql-5.7.17]# chkconfig mysqld on
1.10 设置环境变量
将/usr/local/mysql/bin添加进入环境变量,或者直接使用软链接的方式链到/usr/local/bin下:
#添加到环境变量
[root@localhost mysql-5.7.17]# echo “export PATH=/usr/local/mysql/bin:$PATH” >>/etc/bashrc
[root@localhost mysql-5.7.17]# echo"" >> /etc/bashrc
[root@localhost mysql-5.7.17]# source ~/.bashrc
#使用软链接
[root@localhost mysql-5.7.17]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
1.11 数据库初始化
修改配置文件
默认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 -p’old_pass’ password ‘new_pass’
或者
mysql-uroot -p
alteruser root@localhost identified by ‘111111’;
脚本
#!/bin/bash
############################编译安装MYSQL数据库###################################
#安装依赖包
yum -y install ncurses ncurses-devel bison cmake gcc gcc-c++
#创建mysql用户
useradd -M -s /sbin/nologin mysql
#解压安装包
cd /opt
tar -xf mysql-5.7.17.tar.gz
tar -xf boost_1_59_0.tar.gz -C /usr/local/
mv /usr/local/boost_1_59_0 /usr/local/boost
#切换到解压目录
cd mysql-5.7.17/
#编译安装
cmake
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock
-DSYSCONFDIR=/etc
-DSYSTEMD_PID_DIR=/usr/local/mysql
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_EXTRA_CHARSETS=all
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1
-DMYSQL_DATADIR=/usr/local/mysql/data
-DWITH_BOOST=/usr/local/boost
-DWITH_SYSTEMD=1
make -j3 && make install
#修改配置文件
cat > /etc/my.cnf <<EOF
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
EOF
#更改安装目录属主属组为mysql
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf
#添加环境变量
#echo ‘export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH’ >> /etc/profile
#source /etc/profile
#创建软连接
ln -s /usr/local/mysql/bin/* /usr/local/bin
#初始化数据库
cd /usr/local/mysql/bin/
./mysqld
–initialize-insecure
–user=mysql
–basedir=/usr/local/mysql
–datadir=/usr/local/mysql/data
#添加MYSQL到系统服务管理
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld.service
if [ $? -eq 0 ];then
echo “MYSQL安装完成”
else
echo “服务安装异常,请检查”
fi
systemctl enable mysqld.service
总结
对刚接触Mysql的人来说,源码安装是很有必要的,因为明确知道程序每一步执行的命令,对软件的理解及日常维护都会起到融会贯通,举一反三的作用。软件安装过程中,会遇到很多很多的问题,遇到问题,解决问题,且多用脚本工具平台起到以逸待劳的作用,如此不断精进,方可不断成长,团队的工具产品也可不断叠加场景,成为时间的朋友
更多推荐
所有评论(0)