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

前端之家收集整理的这篇文章主要介绍了Mysql入门分享MySQL的自动化安装部署的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

MysqL入门分享MysqL自动化安装部署的方法》要点:
本文介绍了MysqL入门分享MysqL自动化安装部署的方法,希望对您有用。如果有疑问,可以联系我们。

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

MysqL数据库1、制作符合自己需求的RPM包

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

MysqL数据库wget http://downloads.MysqL.com/archives/get/file/MysqL-5.6.16.tar.gz
tar -zxvf MysqL-5.6.16.tar.gz
cd MysqL-5.6.16
mkdir rpm
cd rpm

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

MysqL数据库Name: MysqL
Version:5.6.16
Release: guahao
License: GPL
URL: http://downloads.MysqL.com/archives/get/file/MysqL-5.6.16.tar.gz
Group: applications/database
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
BuildRequires: cmake
Packager: zhuxj@guahao.com
Autoreq: no
prefix: /opt/MysqL
Summary: MysqL 5.6.16

MysqL数据库%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.

MysqL数据库%define MysqL_USER MysqL
%define MysqL_GROUP MysqL
%define __os_install_post %{nil}

MysqL数据库%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

MysqL数据库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';

MysqL数据库make -j `cat /proc/cpuinfo | grep processor| wc -l`

MysqL数据库%install
cd $OLDPWD/../
make DESTDIR=$RPM_BUILD_ROOT install

MysqL数据库%clean
rm -rf $RPM_BUILD_ROOT

MysqL数据库%files
%defattr(-,%{MysqL_USER},%{MysqL_GROUP})
%attr(755,%{MysqL_GROUP}) %{prefix}/*

MysqL数据库%pre

MysqL数据库%post
ln -s %{prefix}/lib %{prefix}/lib64

MysqL数据库%preun

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

MysqL数据库2、编写my.cnf模板

MysqL数据库my.cnf模板如下:

MysqL数据库[MysqLd_safe]
pid-file=/opt/MysqL/run/MysqLd.pid

MysqL数据库[MysqL]
prompt=\\u@\\d \\r:\\m:\\s>
default-character-set=gbk
no-auto-rehash

MysqL数据库[client]
socket=/opt/MysqL/run/MysqL.sock

MysqL数据库[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

MysqL数据库#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

MysqL数据库#myisam
key_buffer_size=100M
myisam_sort_buffer_size=64M
concurrent_insert=2
delayed_insert_timeout=300

MysqL数据库#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

MysqL数据库#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

MysqL数据库#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

MysqL数据库log_slow_admin_statements=1
log_warnings=1
long_query_time=1
slow_query_log=1
general_log=0

MysqL数据库query_cache_type=0
query_cache_limit=1M
query_cache_min_res_unit=1K

MysqL数据库table_definition_cache=65536

MysqL数据库thread_stack=512K
thread_cache_size=256
read_rnd_buffer_size=128K
sort_buffer_size=256K
join_buffer_size=128K
read_buffer_size=128K

MysqL数据库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

MysqL数据库#server id

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

MysqL数据库3、准备MysqL数据目录模板

MysqL数据库你得事先准备一台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

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

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

MysqL数据库MysqL自动化安装部署脚本(命名为:MysqL_install.sh)如下:

MysqL数据库#!/bin/sh

MysqL数据库#Step 1: Prepare
yum install cmake gcc g++ bison ncurses-devel zlib

MysqL数据库groupadd MysqL
useradd -g MysqL MysqL

MysqL数据库#Step 2: Get Source
ftp -n<<EOF
open 10.10.100.254
user zhuxianjie zxj321
binary
cd MysqL
prompt
mget *
EOF

MysqL数据库#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

MysqL数据库tar xvf data.tar -C /data
chown -R MysqL:MysqL /data/MysqL

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

MysqL数据库/etc/init.d/MysqLd start

猜你在找的MySQL相关文章