码迷,mamicode.com
首页 > 数据库 > 详细

使用playbook实现一键部署mysql数据库

时间:2020-06-14 01:09:54      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:数据库   present   创建   导出   use   utf8   -bash   sync   pre   

环境

主机名 安装服务 wan lan
db01 mariadb 10.0.0.51 172.16.1.51

流程分析

1.安装ansible
2.优化ansible
3.推送公钥
4.开启防火墙
5.开启80 443 873 nfs等端口和服务白名单
6.关闭selinux
7.创建同一的用户
	1.安装数据库
	2.启动数据库
	3.创建数据库用户
	4.创建数据库
	

主机清单

[root@m01 ~]# vim /etc/ansible/hosts
[db_group]
db01 ansible_ssh_host=172.16.1.51 asible_ssh_user=root ansible_ssh_port=22

导出数据库数据

1.备份db01上的数据库
mysqldump -uroot -p‘1‘ -A wp > /root/wp.sql

2.将web01上备份的数据库拷贝至m01服务器上
scp wp.sql  root@172.16.1.61:/root/ansible/lb/

yml

- hosts: all
  tasks:
    - name: install mariadb
      yum:
        name: mariadb-server
        state: present
      when: ansible_fqdn is match ‘lb*‘
        
    - name: start mariadb-server
      systemd:
        name: mariadb-server
        state: started
        enabled: yes
      when: ansible_fqdn is match ‘lb*‘

    - name: grant mysql user
      mysql_user:
      	#login_host: "localhost"
      	#login_user: "root"
     	#login_password: "123"
     	login_port: "3306"
      	name: "ty"
      	password: "123"
      	host: "%"
      	priv: "*.*:ALL,GRANT"
      	state: "present"
      when: ansible_fqdn is match ‘lb*‘

    - name: create a database
      mysql_db:
        #login_host: "127.0.0.1"
        #login_user: "root"
        #login_password: "123"
        login_port: "3306"
        name: "wp"
        encoding: "utf8"
        state: "present"   
      when: ansible_fqdn is match ‘lb*‘
        
    - name: copy wp.sql
      copy:
        src: "/root/ansible/lb/wp.sql"
        dest: "/root/"
      when: ansible_fqdn is match ‘lb*‘

    - name: import a database
      mysql_db:
        login_host: "127.0.0.1"
        login_user: "root"
        login_port: "3306"
        name: "wp"
        target: "/root/wp.sql"
        state: "import"
      when: ansible_fqdn is match ‘lb*‘

执行

1.执行base.yml
[root@m01 ~]# ansible-playbook ansible/base.yml 

2.执行rsync.yml
[root@m01 ~]# ansible-playbook ansible/nfs/nfs.yml -i /root/ansible/lb/hosts

使用playbook实现一键部署mysql数据库

标签:数据库   present   创建   导出   use   utf8   -bash   sync   pre   

原文地址:https://www.cnblogs.com/syy1757528181/p/13122283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!