天天看點

規模化部署mysql5.6

 Mysql有時需要大規模部署時,我們可以使用之前的資料庫做為模闆然後通過rpm打包工具傳遞到其它主機進行批量部署。

1、首先,下載下傳rpmbuild

yum install rpm-build -y

它是Red Hat用于打RPM包的工具。我們在編輯打包程式時,可以看到/root/rpmbuild目錄下有如下檔案夾。

BUILD  BUILDROOT  RPMS  SOURCES  SPECS  SRPMS

2、然後,下載下傳源碼包

http://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.20.tar.gz

并将其複制到指定的目錄/root/rpmbuild/SOURCES/mysql-5.6.20.tar.gz

3、安裝編譯MySQL需要的依賴包

yum -y install make gcc-c++ cmake bison-devel  ncurses-devel

4、編寫spec腳本

其實編寫spec檔案本質上是在寫一個回調函數,rpmbuild根據spec的回調函數,生成一個Shell腳本,打包的過程就是執行Shell腳本的過程。我們在腳本中使用CFLAGS将源碼包轉變為二進制安裝包。

5、生成rpm包

Tar zxvf mysql-5.6.20.tar.gz

Cd mysql_5.6.20

mkdir  5.6.20_mysql/rpm

cp mysql.spec  5.6.20_mysql/rpm

執行結束後生成的rpm包就在RPMS/x86_64下了

定義配置模闆,需要生成唯一的server_id

<a></a>

6、配置ftp

Yum install vsftp

Vi /etc/vsftpd/vsftpd.conf

userlist_enable=NO

将root從user_list中去掉

将root從/etc/vsftpdftpusers中去掉

Service vsftpd start

7、準備所需資料

需要的資料有rpm包、my.cnf和data檔案。

Tar cf data.tar ibdata1 mysql/ performance_schema/  test/   準備資料包

将mysql_5.6.20.tar.gz包、my.cnf包和data.tar包放在/var/ftp/pub目錄供其它server下載下傳。

8、配置自動化腳本

自動化腳本主要包括rpm包安裝,ftp下載下傳,mysql’使用者群組添加、檔案夾賦權、資料包解壓、rpm包安裝和服務配置。

9、拷貝到目标伺服器執行

Scp auto_mysql.txt ip:/root/

Chmod 755 auto.sh

./auto.sh &amp;

<a href="http://s3.51cto.com/wyfs02/M00/6D/CE/wKioL1VsQkDiHBKRAAHfwPVK7SE089.jpg" target="_blank"></a>

注腳本檔案:

mysql.spec

Name: mysql

Version:5.6.20

Release: %(echo $RELEASE)%{?dist}

License: GPL

Group: applications/database

BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root 

BuildRequires: cmake

Autoreq: no

#Source: %{name}-%{version}.tar.gz

prefix: /usr/local/mysql

Summary: MySQL 5.6.20

%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

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

my.cnf

[mysqld_safe]

pid-file=/usr/local/mysql/run/mysqld.pid

#malloc-lib=/usr/local/mysql/lib/libjemalloc.so

[mysql]

port=3306

default-character-set=gbk

no-auto-rehash

[client]

socket=/usr/local/mysql/run/mysql.sock

[mysqld]

#dir

basedir=/usr/local/mysql

datadir=/usr/local/mysql/data

tmpdir=/tmp

lc_messages_dir=/usr/local/mysql/share

log-error=/usr/local/mysql/log/alert.log

slow_query_log_file=/usr/local/mysql/log/slow.log

general_log_file=/usr/local/mysql/log/general.log

#innodb

innodb_data_home_dir=/usr/local/mysql/data

innodb_log_group_home_dir=/usr/local/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=2

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=1000

innodb_thread_concurrency=16

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=inserts

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=5

innodb_rollback_on_timeout=0

innodb_purge_threads=1

innodb_strict_mode=1

transaction-isolation=READ-COMMITTED

#myisam

key_buffer=64M

myisam_sort_buffer_size=64M

concurrent_insert=2

delayed_insert_timeout=300

#replication

master-info-file=/usr/local/mysql/log/master.info

relay-log=/usr/local/mysql/log/relaylog

relay_log_info_file=/usr/local/mysql/log/relay-log.info

relay-log-index=/usr/local/mysql/log/mysqld-relay-bin.index

slave_load_tmpdir=/usr/local/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=/usr/local/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

#sqlmod="STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE"

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

#table_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

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=1921681201

# vi mysql_install.sh

#!/bin/bash

# mysql auto-install script

yum install -y cmake gcc g++ bison ncurses-devel zlib

#Step 2: Create group &amp; user

group=$(more /etc/group|grep mysql)

if [ "$group" != "" ]; then

   echo "group already created!"

else

   groupadd mysql

fi

user=$(more /etc/passwd|grep mysql)

if [ "$user" != "" ]; then

   echo "user already created!"

   useradd -g mysql mysql

#Step 3: Get source

ftp -n&lt;&lt;EOF

open 10.243.29.65

user root test!2013

binary

cd /var/ftp/pub

mget *

EOF

#Step 3: Install

unique_id=`ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' | sed -e 's/\.//g'`

echo 'server_id='$unique_id &gt;&gt; my.cnf

rpm -ivh mysql-5.6.20-.el6.x86_64.rpm

cp my.cnf /usr/local/mysql

chown -R mysql:mysql /usr/local/mysql

tar xvf data.tar -C /usr/local/mysql/data/

mkdir -p /usr/local/mysql/log

chown -R mysql:mysql /usr/local/mysql/log

#step 4: Start MySQL

cp /usr/local/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

本文轉自zsaisai 51CTO部落格,原文連結:http://blog.51cto.com/3402313/1657276