欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

分享MySQL的自动化安装部署的方法

程序员文章站 2024-02-28 20:48:22
有过mysql运维的人应该都清楚,线上的mysql一般都采用源码编译,因为这样才可以根据企业的各自需要选择要编译的功能,虽然mysql的源码编译挺简单的,但是试想一下,如果...

有过mysql运维的人应该都清楚,线上的mysql一般都采用源码编译,因为这样才可以根据企业的各自需要选择要编译的功能,虽然mysql的源码编译挺简单的,但是试想一下,如果你有几百台服务器同时要安装mysql,难道你还一台台去手动编译、编写配置文件吗?这显然太低效了,本文讨论mysql的自动化安装部署。

1、制作符合自己需求的rpm包

我们要根据mysql的源码编译符合企业需求的rpm包,源码获取命令如下:

wget
tar -zxvf mysql-5.6.16.tar.gz
cd mysql-5.6.16
mkdir rpm
cd rpm

在上面我们获取了源码,并在源码主目录下创建rpm目录,接着我们在该目录下创建mysql.spec文件:

name: mysql
version:5.6.16
release: guahao
license: gpl
url:
group: applications/database
buildroot: %{_tmppath}/%{name}-%{version}-%{release}-root
buildrequires: cmake
packager:
autoreq: no
prefix: /opt/mysql
summary: mysql 5.6.16

%description
the mysql(tm) software delivers a very fast, multi-threaded, multi-user,
and robust sql (structured query language) database server. mysql server
is intended for mission-critical, heavy-load production systems as well
as for embedding into mass-deployed software.

%define mysql_user mysql
%define mysql_group mysql
%define __os_install_post %{nil}

%build
cd $oldpwd/../
cflags="-o3 -g -fno-exceptions -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing"
cxx=g++
cxxflags="-o3 -g -fno-exceptions -fno-rtti -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing"
export cflags cxx cxxflags

cmake .                                                  \
  -dsysconfdir:path=%{prefix}                            \
  -dcmake_install_prefix:path=%{prefix}                  \
  -dcmake_build_type:string=release                      \
  -denable_profiling:bool=on                             \
  -dwith_debug:bool=off                                  \
  -dwith_valgrind:bool=off                               \
  -denable_debug_sync:bool=off                           \
  -dwith_extra_charsets:string=all                       \
  -dwith_ssl:string=bundled                              \
  -dwith_unit_tests:bool=off                             \
  -dwith_zlib:string=bundled                             \
  -dwith_partition_storage_engine:bool=on                \
  -dwith_innobase_storage_engine:bool=on                 \
  -dwith_archive_storage_engine:bool=on                  \
  -dwith_blackhole_storage_engine:bool=on                \
  -dwith_perfschema_storage_engine:bool=on               \
  -ddefault_charset=utf8                                 \
  -ddefault_collation=utf8_general_ci                    \
  -dwith_extra_charsets=all                              \
  -denabled_local_infile:bool=on                         \
  -dwith_embedded_server=0                               \
  -dinstall_layout:string=standalone                     \
  -dcommunity_build:bool=on                              \
  -dmysql_server_suffix='-r5436';

make -j `cat /proc/cpuinfo | grep processor| wc -l`

%install
cd $oldpwd/../
make destdir=$rpm_build_root install

%clean
rm -rf $rpm_build_root

%files
%defattr(-, %{mysql_user}, %{mysql_group})
%attr(755, %{mysql_user}, %{mysql_group}) %{prefix}/*

%pre

%post
ln -s %{prefix}/lib %{prefix}/lib64

%preun

%changelog
有了这个spec文件之后,就可以执行如下命令生成我们自己的rpm包:
rpmbuild -bb ./mysql.spec

2、编写my.cnf模板

my.cnf模板如下:

[mysqld_safe]
pid-file=/opt/mysql/run/mysqld.pid

[mysql]
prompt=\\u@\\d >
default-character-set=gbk
no-auto-rehash

[client]
socket=/opt/mysql/run/mysql.sock

[mysqld]
#dir
basedir=/opt/mysql
datadir=/data/mysql/data
tmpdir=/data/mysql/tmp
log-error=/data/mysql/log/alert.log
slow_query_log_file=/data/mysql/log/slow.log
general_log_file=/data/mysql/log/general.log
socket=/opt/mysql/run/mysql.sock

#innodb
innodb_data_home_dir=/data/mysql/data
innodb_log_group_home_dir=/data/mysql/data
innodb_data_file_path=ibdata1:2g;ibdata2:16m:autoextend
innodb_buffer_pool_size=10g
innodb_buffer_pool_instances=4
innodb_log_files_in_group=4
innodb_log_file_size=1g
innodb_log_buffer_size=200m
innodb_flush_log_at_trx_commit=1
innodb_additional_mem_pool_size=20m
innodb_max_dirty_pages_pct=60
innodb_io_capacity=200
innodb_thread_concurrency=32
innodb_read_io_threads=8
innodb_write_io_threads=8
innodb_open_files=60000
innodb_file_format=barracuda
innodb_file_per_table=1
innodb_flush_method=o_direct
innodb_change_buffering=all
innodb_adaptive_flushing=1
innodb_old_blocks_time=1000
innodb_stats_on_metadata=0
innodb_read_ahead=0
innodb_use_native_aio=0
innodb_lock_wait_timeout=50
innodb_rollback_on_timeout=0
innodb_purge_threads=1
innodb_strict_mode=1
transaction-isolation=read-committed

#myisam
key_buffer_size=100m
myisam_sort_buffer_size=64m
concurrent_insert=2
delayed_insert_timeout=300

#replication
master-info-file=/data/mysql/log/master.info
relay-log=/data/mysql/log/mysql-relay
relay_log_info_file=/data/mysql/log/mysql-relay.info
relay-log-index=/data/mysql/log/mysql-relay.index
slave_load_tmpdir=/data/mysql/tmp
slave_type_conversions="all_non_lossy"
slave_net_timeout=4
skip-slave-start
sync_master_info=1000
sync_relay_log_info=1000

#binlog
log-bin=/data/mysql/log/mysql-bin
server_id=2552763370
binlog_cache_size=32k
max_binlog_cache_size=2g
max_binlog_size=500m
binlog_format=row
sync_binlog=1000
log-slave-updates=1
expire_logs_days=0

#server
default-storage-engine=innodb
character-set-server=gbk
lower_case_table_names=1
skip-external-locking
open_files_limit=65536
safe-user-create
local-infile=1
performance_schema=0

log_slow_admin_statements=1
log_warnings=1
long_query_time=1
slow_query_log=1
general_log=0

query_cache_type=0
query_cache_limit=1m
query_cache_min_res_unit=1k

table_definition_cache=65536

thread_stack=512k
thread_cache_size=256
read_rnd_buffer_size=128k
sort_buffer_size=256k
join_buffer_size=128k
read_buffer_size=128k

port=3306
skip-name-resolve
skip-ssl
max_connections=4500
max_user_connections=4000
max_connect_errors=65536
max_allowed_packet=128m
connect_timeout=8
net_read_timeout=30
net_write_timeout=60
back_log=1024

#server id

细心的读者应该会注意在,在my.cnf的末尾在server id上留了空白,在后面的shell脚本会动态加上,这是因为在一个企业内部的所有mysql的server id必须保持全局一致性,这样在主备复制时才不会导致混乱。
其实如果想把这个脚本写的更通用,完全可以把更多的参数留白,如port、datadir、内存相关参数等,这里我只是以server id为例,抛砖引玉。

3、准备mysql数据目录模板

你得事先准备一台mysql,可以根据自己的需求,把通用性的东西放在上面(如账户等),下面是一个最简单的已安装好的mysql的数据目录结构:

[root@lx25 mysql]# ls -l
total 12
drwxr-xr-x 5 mysql mysql 4096 jul  2 09:26 data
drwxr-xr-x 2 mysql mysql 4096 jul  1 18:21 log
drwxr-xr-x 2 mysql mysql 4096 jul  2 09:26 tmp
[root@lx25 mysql]# cd data
[root@lx25 data]# ls -l
total 6314044
drwx------ 2 mysql mysql       4096 jul  1 17:17 mysql
drwx------ 2 mysql mysql       4096 jul  1 17:17 performance_schema
drwx------ 2 mysql mysql       4096 jul  1 17:17 test

把该目录用tar打包(命名为data.tar),然后以这个为模板解压至新装mysql实例的数据目录下即可。
4、编写自动化安装部署脚本

在运行这个脚本之前,我们必须得把前面几部制作的rpm包、my.cnf模板和数据目录模板放到一个固定的地方,本例中是放在企业内部的ftp上。

mysql自动化安装部署脚本(命名为:mysql_install.sh)如下:

#!/bin/sh

#step 1: prepare
yum install cmake gcc g++ bison ncurses-devel zlib

groupadd mysql
useradd -g mysql mysql

#step 2: get source
ftp -n<<eof
open 10.10.100.254
user zhuxianjie zxj321
binary
cd mysql
prompt
mget *
eof

#step 3: install
unique_id=`date "+%y%m%d%m%s"`
echo 'server_id='$unique_id >> my.cnf
rpm -ivh mysql-5.6.16-guahao.x86_64.rpm
cp my.cnf /opt/mysql
chown -r mysql:mysql /opt/mysql

tar xvf data.tar -c /data
chown -r mysql:mysql /data/mysql

#step 4: start mysql
cp /opt/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig mysqld on

/etc/init.d/mysqld start