Linux之Ansible安装多实例mariadb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux之Ansible安装多实例mariadb相关的知识,希望对你有一定的参考价值。

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]le-centos7 /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的主要内容,如果未能解决你的问题,请参考以下文章

ansible实现mariadb在多台服务器的自动安装

编写二进制安装mariadb10.2的ansible-playbook剧本

Mariadb-多实例安装

mariadb多实例安装

剧本---Ansible实现自动化安装MariaDB

多实例:MySQL系列之二