在前面的博客中已經演示,經過腳本實現MySQL通用二進制的安裝,下面經過演示用腳本實現源碼編譯MariaDB實現自動化,在大規模部署多臺MariaDB時,這裏使用ansible來實現其自動化部署,能夠大大簡化運維工程師的時間。node
先講解腳本在本地自動化安裝MariaDB,結合上一篇博客和前面自動化實現的MySQL通用二進制格式的安裝。mysql
環境:Centos 6.6 mariadb-10.0.13.tar.gz node5(hostname) 2塊硬盤:1塊是系統用、1塊是做爲MySQL數據盤
sql
查看卷組信息
[root@node5 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg_lvm 1 2 0 wz--n- 98.30g 68.30g
[root@node5 ~]# vgs | awk '{if(NR==2) {print $1}}' 把卷組名切分出來
vg_lvm
擴容卷組
[root@node5 ~]# vgextend vg_lvm /dev/sdb
Volume group "vg_lvm" successfully extended
[root@node5 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg_lvm 2 2 0 wz--n- 118.30g 88.30g
[root@node5 ~]# vgreduce $(vgs | awk '{if(NR==2) {print $1}}') /dev/sdb
Removed "/dev/sdb" from volume group "vg_lvm"
[root@node5 ~]# lvcreate -L 18G -n data vg_lvm /dev/sdb
Logical volume "data" createdshell
[root@node5 ~]# chmod +x mysql.sh 給定其權限
[root@node5 ~]# vim mysql.sh 腳本內容以下:
#!/bin/bash
useradd -r -s /sbin/nologin mysql > /dev/null
vgextend $(vgs|awk '{if(NR==2) {print $1}}') /dev/sdb > /dev/null
lvcreate -L 18G -n data vg_lvm /dev/sdb > /dev/null
mkdir /mysql
mkfs.ext4 /dev/vg_lvm/data > /dev/null
mount /dev/vg_lvm/data /mysql
mkdir /mysql/data
chown -R mysql.mysql /mysql/data
tar -xf /usr/local/src/mariadb-10.0.13.tar.gz -C /usr/local/
yum groupinstall -y "Development tools" "Server Platform Development" > /dev/null
echo -e "\033[42mGroupinstall is OK.\033[0m"
yum install -y libxml2-devel cmake > /dev/null
echo -e "\033[42mInstall is OK.\033[0m"
cd /usr/local/mariadb-10.0.13/
cmake . -DMYSQL_DATADIR=/mysql/data -DWITH_SSL=system -DWITH_SPHINX_STORAGE_ENGINE=1 > /dev/null
echo -e "\033[42mCmake is OK.\033[0m"
make && make install > /dev/null
echo -e "\033[42mMake and Make install is OK.\033[0m"
cd /usr/local/mysql
echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
cp -f support-files/my-large.cnf /etc/my.cnf
sed -i '/^\[mysqld\]/a datadir=/mysql/data' /etc/my.cnf
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
chown -R root.mysql /usr/local/mysql/*
/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mysql/data > /dev/null
echo -e "033[42mMysql initial is ok.\033[0m"
service mysqld start
ss -tnlp|grep 3306
[root@node5 ~]# ./mysql.sh 執行腳本
.....
150410 5:52:01 [Note] InnoDB: Starting shutdown...
150410 5:52:03 [Note] InnoDB: Shutdown completed; log sequence number 1616707
033[42mMysql initial is ok. 初始化完成
Starting MySQL. SUCCESS! 啓動ok
LISTEN 0 128 :::3306 :::* users: (("mysqld",39585,21)) 發現監聽在3306端口了,說明自動化一切都ok
vim
MariaDB源碼編譯進行配置時,能夠根據業務需求功能,定製其功能,經過命令行給其傳遞一個配置參數或者是經過在mysql.sh文件包含配置參數所在的文件,下次要使用另外功能時,直接修改另一個文件的參數便可。在執行mysql.sh時,爲了執行一切ok,也能夠先在主機上安裝好screen軟件,經過screen命令來安全執行制動化腳本。安全
下面是使用ansible實現自動化編譯安裝MariaDB
bash
hosts /etc/ansible/hosts文件內容
[dbserver]
IP 或者是 HOSTNAME
IP 或者是 HOSTNAME
IP 或者是 HOSTNAME
。。。
args.sh 做爲MariaDB 參數的文件,能夠根據需求改動便可
#!/bin/bash
mariadb="-DMYSQL_DATADIR=/mysql/data -DWITH_SSL=system -DWITH_SPHINX_STORAGE_ENGINE=1"
mysql.sh
#!/bin/bash
source /root/args.sh
useradd -r -s /sbin/nologin mysql > /dev/null
vgextend $(vgs|awk '{if(NR==2) {print $1}}') /dev/sdb > /dev/null
lvcreate -L 18G -n data vg_lvm /dev/sdb > /dev/null
mkdir /mysql
mkfs.ext4 /dev/vg_lvm/data > /dev/null
mount /dev/vg_lvm/data /mysql
mkdir /mysql/data
chown -R mysql.mysql /mysql/data
yum groupinstall -y "Development tools" "Server Platform Development" > /dev/null
echo -e "\033[42mGroupinstall is OK.\033[0m"
yum install -y libxml2-devel cmake > /dev/null
echo -e "\033[42mInstall is OK.\033[0m"
cd /usr/local/mariadb-10.0.13/
cmake . $(mariadbarg) > /dev/null
echo -e "\033[42mCmake is OK.\033[0m"
make && make install > /dev/null
echo -e "\033[42mMake and Make install is OK.\033[0m"
cd /usr/local/mysql
echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
cp -f support-files/my-large.cnf /etc/my.cnf
sed -i '/^\[mysqld\]/a datadir=/mysql/data' /etc/my.cnf
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
chown -R root.mysql /usr/local/mysql/*
/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mysql/data > /dev/null
echo -e "033[42mMysql initial is ok.\033[0m"
service mysqld start
ss -tnlp|grep 3306
mysql.yml playbook文件內容以下: unarchive模塊是複製和解壓文件的,script是把當前shell腳本到遠程主機上執行
---
#file: mysql.yml
- hosts: dbserver
remote_user: root
tasks:
- name: remote copy decompress
unarchive: src=/usr/local/src/mariadb-10.0.13.tar.gz dest dest=/usr/local/ copy=yes
- name: execute mariadb install script
script: /root/mysql.sql
運維
unarchive: src=/usr/local/src/mariadb-10.0.13.tar.gz dest dest=/usr/local/ copy=yes 意思是:複製本地文件/usr/local/src/mariadb-10.0.13.tar.gz到遠程解壓縮到/usr/local目錄下。ide
ok,到這裏就已經所有ok了,若是實驗中有說什麼疑問,歡迎一塊兒討論
命令行