Linux之Ansible安裝多實例mariadb
阿新 • • 發佈:2019-04-30
The exit ansible安裝 uid ansi UNC etc mas entos Ansible安裝多實例mariadb10.2.23
堡壘機配置
root家目錄下放置mariadb二進制包
[Mon Apr 29 19:52
[email protected] ~]# ls
-rw-r--r-- 1 root root 456950538 Apr 26 08:16 mariadb-10.2.23-linux-x86_64.tar.gz
在/data目錄下創建ansible目錄,內部創建roles文件夾
[Mon Apr 29 20:05 [email protected] /data/ansible] # ll drwxr-xr-x 5 root root 4096 Apr 29 09:40 roles
在roles目錄下創建需要用到的目錄
[Mon Apr 29 19:54
[email protected] /data/ansible/roles]# mkdir mariadb/{tasks,templates,vars} -pv
查看當前目錄結構
[Mon Apr 29 20:05
[email protected] /data/ansible/roles]# tree mariadb/
mariadb/
|-- tasks
|-- templates
|-- vars
創建數據庫管理賬戶組.yml:
[Mon Apr 29 20:16 [email protected] /data/ansible/roles]# vim mariadb/tasks/group.yml - name: create group group: name=mysql system=yes gid=336
創建數據庫管理賬戶.yml:
[Mon Apr 29 20:06 [email protected] /data/ansible/roles]# vim mariadb/tasks/user.yml - name: create user user: name=mysql uid=336 system=yes group=mysql home=/data/mysql create_home=no
創建多實例所需存儲目錄.yml:
[Mon Apr 29 20:17
[email protected] /data/ansible/roles]# vim mariadb/tasks/mkdir.yml
- name: mkdir /data/mysql/...
shell: mkdir -pv /data/mysql/{3306,3307,3308}/{data,etc,socket,log,bin,pid}
修改存儲目錄權限.yml:
[Mon Apr 29 20:20
[email protected] /data/ansible/roles]# vim mariadb/tasks/chown.yml
- name: change /data/mysql owner
shell: chown -R mysql.mysql /data/mysql
解壓mariadb包到被控端主機/usr/local/下.yml:
[Mon Apr 29 20:20
[email protected] /data/ansible/roles]# vim mariadb/tasks/unarchive.yml
- name: unarchive mariadb.tar
unarchive: src=/root/{{package}} dest=/usr/local/
- name: create link
file: src=/usr/local/{{package1}} path=/usr/local/mysql state=link
生成數據庫內容.yml:
[Mon Apr 29 20:27
[email protected] /data/ansible/roles]# vim mariadb/tasks/scp.yml
- name: ctreate the database
shell: cd /usr/local/mysql/; ./scripts/mysql_install_db --datadir=/data/mysql/{{item}}/data --user=mysql
with_items:
- 3306
- 3307
- 3308
拷貝my.cnf.j2生成my.cnf配置文件.yml:
[Mon Apr 29 20:30
[email protected] /data/ansible/roles]# vim mariadb/tasks/config.yml
- name: copy config1
template: src=my.cnf.j2 dest=/data/mysql/{{port1}}/etc/my.cnf
- name: copy config2
template: src=my1.cnf.j2 dest=/data/mysql/{{port2}}/etc/my.cnf
- name: copy config3
template: src=my2.cnf.j2 dest=/data/mysql/{{port3}}/etc/my.cnf
拷貝模板文件生成啟動服務文件.yml:
[Mon Apr 29 20:31
[email protected] /data/ansible/roles]# vim mariadb/tasks/mysqld.yml
- name: copy service1
template: src=mysqld.j2 dest=/data/mysql/{{port1}}/bin/mysqld mode=754
- name: copy service2
template: src=mysqld1.j2 dest=/data/mysql/{{port2}}/bin/mysqld1 mode=754
- name: copy service3
template: src=mysqld2.j2 dest=/data/mysql/{{port3}}/bin/mysqld2 mode=754
寫主文件.yml:
[Mon Apr 29 20:31
[email protected] /data/ansible/roles]# vim mariadb/tasks/main.yml
- include: group.yml
- include: user.yml
- include: mkdir.yml
- include: unarchive.yml
- include: scp.yml
- include: chown.yml
- include: config.yml
- include: mysqld.yml
拷貝需要用到的模板文件
[Mon Apr 29 20:37
[email protected] /data/ansible/roles]# cp /etc/my.cnf mariadb/templates/my.cnf.j2
修改my.cnf.j2模板文件,此文件需要拷貝復制三份在當前目錄,命名為my1.cnf.j2,my2,cnf.j2,分別對應變量{{port1}},{{port2}},{{port3}}
my.cnf.j2
[Mon Apr 29 20:38
[email protected] /data/ansible/roles]# vim mariadb/templates/my.cnf.j2
[mysqld]
port={{port1}}
datadir=/data/mysql/{{port1}}/data
socket=/data/mysql/{{port1}}/socket/mysql.sock
[mysqld_safe]
log-error=/data/mysql/{{port1}}/log/mariadb.log
pid-file=/data/mysql/{{port1}}/pid/mariadb.pid
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
my1.cnf.j2
[Mon Apr 29 20:43
[email protected] /data/ansible/roles]# vim mariadb/templates/my1.cnf.j2
[mysqld]
port={{port2}}
datadir=/data/mysql/{{port2}}/data
socket=/data/mysql/{{port2}}/socket/mysql.sock
[mysqld_safe]
log-error=/data/mysql/{{port2}}/log/mariadb.log
pid-file=/data/mysql/{{port2}}/pid/mariadb.pid
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
my2.cnf.j2
[Mon Apr 29 20:41
[email protected] /data/ansible/roles]# vim mariadb/templates/my2.cnf.j2
[mysqld]
port={{port3}}
datadir=/data/mysql/{{port3}}/data
socket=/data/mysql/{{port3}}/socket/mysql.sock
[mysqld_safe]
log-error=/data/mysql/{{port3}}/log/mariadb.log
pid-file=/data/mysql/{{port3}}/pid/mariadb.pid
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
服務啟動腳本,此腳本也要復制3份放到templates目錄內,命名為mysqld.j2,mysqld1.j2,mysqld2.j2:內部需要更改的有端口port,變量自動生成
[Mon Apr 29 20:47
[email protected] /data/ansible/roles]# vim mariadb/templates/mysqld.j2
#!/bin/bash
port={{port1}}
mysql_user="root"
mysql_pwd=""
cmd_path="/usr/local/mysql/bin"
mysql_basedir="/data/mysql"
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"
function_start_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
${cmd_path}/mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf &> /dev/null &
else
printf "MySQL is running...\n"
exit
fi
}
function_stop_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
printf "Stoping MySQL...\n"
${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
fi
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 2
function_start_mysql
}
case $1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
printf "Usage: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"
esac
上述用到的變量在vars目錄內定義
[Mon Apr 29 20:43
[email protected] /data/ansible/roles]# vim mariadb/vars/main.yml
package: mariadb-10.2.23-linux-x86_64.tar.gz
package1: mariadb-10.2.23-linux-x86_64
port1: 3306
port2: 3307
port3: 3308
在anslible目錄下創建主腳本mariadb.yml
[Mon Apr 29 20:52
[email protected] /data/ansible]# vim mariadb.yml
---
- hosts: 172.16.36.112
remote_user: root
roles:
- role: mariadb
最後的目錄結構:
|-- mariadb
| |-- tasks
| | |-- chown.yml
| | |-- config.yml
| | |-- group.yml
| | |-- main.yml
| | |-- mkdir.yml
| | |-- mysqld.yml
| | |-- scp.yml
| | |-- unarchive.yml
| | `-- user.yml
| |-- templates
| | |-- my1.cnf.j2
| | |-- my2.cnf.j2
| | |-- my.cnf.j2
| | |-- mysqld1.j2
| | |-- mysqld2.j2
| | `-- mysqld.j2
| `-- vars
| `-- main.yml
腳本跑完之後
ansbile 172.16.36.112 -m shell -a ‘echo ‘PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mariadb.sh;source /etc/profile.d/mariadb.sh‘
由於是使用多實例生成的數據庫,啟動可能稍有不便
在172.16.36.112主機上使用下列命令啟動,ss -tnlp 可以看到三個端口啟動成功和對應的mysqld服務
# bash /data/mysql/3306/bin/mysqld start
# bash /data/mysql/3307/bin/mysqld1 start
# bash /data/mysql/3308/bin/mysqld2 start
[[email protected] ~]# ss -tnlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:* users:(("rpcbind",pid=6100,fd=4),("systemd",pid=1,fd=81))
LISTEN 0 128 *:22 *:* users:(("sshd",pid=6456,fd=3))
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=6618,fd=13))
LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=10667,fd=20))
LISTEN 0 80 :::3307 :::* users:(("mysqld",pid=10792,fd=20))
LISTEN 0 80 :::3308 :::* users:(("mysqld",pid=10907,fd=20))
LISTEN 0 128 :::111 :::* users:(("rpcbind",pid=6100,fd=6),("systemd",pid=1,fd=83))
LISTEN 0 128 :::22 :::* users:(("sshd",pid=6456,fd=4))
LISTEN 0 100 ::1:25 :::* users:(("master",pid=6618,fd=14))
Linux之Ansible安裝多實例mariadb