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

环境

主机名 安装服务 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数据库

上一篇:基于Canal和Kafka实现MySQL的Binlog近实时同步


下一篇:Centos7 安装mysql后登录提示:Access denied for user 'root'@'localhost'