mysql返回库_第一节:返回保持到数据库(mysql)

这篇博客详细介绍了如何配置Salt Minion将任务返回数据存储到MySQL数据库中,包括Python库安装、数据库创建、权限设置及实际操作演示,适合理解和实践SaltStack与MySQL集成的场景。
摘要由CSDN通过智能技术生成

官网:https://docs.saltstack.com/en/latest/ref/returners/all/salt.returners.mysql.html

minion返回数据到mysql数据库里

1)所有minion要安装python-mysql库

[admin@master ~ ]$ sudo salt '*' pkg.install name=MySQL-python

node3.51yuki.cn:

----------

MySQL-python:

----------

new:

1.2.5-1.el7

old:

node2.51yuki.cn:

----------

MySQL-python:

----------

new:

1.2.5-1.el7

old:

2)配置mysql

MariaDB [(none)]> CREATE DATABASE `salt`

-> DEFAULT CHARACTER SET utf8

-> DEFAULT COLLATE utf8_general_ci;

MariaDB [(none)]> use salt

Database changed

MariaDB [salt]> DROP TABLE IF EXISTS `jids`;

Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [salt]> CREATE TABLE `jids` (

-> `jid` varchar(255) NOT NULL,

-> `load` mediumtext NOT NULL,

-> UNIQUE KEY `jid` (`jid`)

-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

iumtext NOT NULL,

`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

KEY `id` (`id`),

KEY `jid` (`jid`),

KEY `fun` (`fun`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--

-- Table structure for table `salt_events`

--

DROP TABLE IF EXISTS `salt_events`;

CREATE TABLE `salt_events` (

`id` BIGINT NOT NULL AUTO_INCREMENT,

`tag` varchar(255) NOT NULL,

`data` mediumtext NOT NULL,

`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

`master_id` varchar(255) NOT NULL,

PRIMARY KEY (`id`),

KEY `tag` (`tag`)

) ENGINE=InQuery OK, 0 rows affected (0.01 sec)

MariaDB [salt]> CREATE INDEX jid ON jids(jid) USING BTREE;

ERROR 1061 (42000): Duplicate key name 'jid'

MariaDB [salt]>

MariaDB [salt]> --

MariaDB [salt]> -- Table structure for table `salt_returns`

MariaDB [salt]> --

MariaDB [salt]>

MariaDB [salt]> DROP TABLE IF EXISTS `salt_returns`;

noDB DEFAULT CHARSET=utf8;Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [salt]> CREATE TABLE `salt_returns` (

-> `fun` varchar(50) NOT NULL,

-> `jid` varchar(255) NOT NULL,

-> `return` mediumtext NOT NULL,

-> `id` varchar(255) NOT NULL,

-> `success` varchar(10) NOT NULL,

-> `full_ret` mediumtext NOT NULL,

-> `alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

-> KEY `id` (`id`),

-> KEY `jid` (`jid`),

-> KEY `fun` (`fun`)

-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Query OK, 0 rows affected (0.02 sec)

MariaDB [salt]>

MariaDB [salt]> --

MariaDB [salt]> -- Table structure for table `salt_events`

MariaDB [salt]> --

MariaDB [salt]>

MariaDB [salt]> DROP TABLE IF EXISTS `salt_events`;

Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [salt]> CREATE TABLE `salt_events` (

-> `id` BIGINT NOT NULL AUTO_INCREMENT,

-> `tag` varchar(255) NOT NULL,

-> `data` mediumtext NOT NULL,

-> `alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

-> `master_id` varchar(255) NOT NULL,

-> PRIMARY KEY (`id`),

-> KEY `tag` (`tag`)

-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Query OK, 0 rows affected (0.02 sec)

MariaDB [salt]> show tables;

+----------------+

| Tables_in_salt |

+----------------+

| jids |

| salt_events |

| salt_returns |

+----------------+

3 rows in set (0.00 sec)

配置授权用户salt

MariaDB [(none)]> grant all on salt.* to 'salt'@'10.2.11.%' identified by 'Aa123456';

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

测试能不能连上

[admin@master ~ ]$ sudo mysql -h 10.2.11.227 -u salt -p

3)配置/etc/salt/minion

mysql.host: '10.2.11.227'

mysql.user: 'salt'

mysql.pass: 'Aa123456'

mysql.db: 'salt'

mysql.port: 3306

4)重启minion

[admin@node2 tmp]$ sudo systemctl restart salt-minion

二)查看

[admin@master ~ ]$ sudo salt '*' test.ping --return=mysql

[admin@master ~ ]$ sudo salt '*' cmd.run 'df -h' --return=mysql

(--return是返回到指定的位置)

MariaDB [salt]> select * from salt_returns\G

*************************** 1. row ***************************

fun: test.ping

jid: 20180120192448883501

return: true

id: node3.51yuki.cn

success: 1

full_ret: {"fun_args": [], "jid": "20180120192448883501", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "node3.51yuki.cn"}

alter_time: 2018-01-20 19:24:49

*************************** 2. row ***************************

fun: test.ping

jid: 20180120192448883501

return: true

id: node2.51yuki.cn

success: 1

full_ret: {"fun_args": [], "jid": "20180120192448883501", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "node2.51yuki.cn"}

alter_time: 2018-01-20 19:24:49

*************************** 3. row ***************************

fun: cmd.run

jid: 20180120192640811889

return: "Filesystem Size Used Avail Use% Mounted on\n/dev/mapper/cl-root 13G 1.6G 11G 13% /\ndevtmpfs 486M 0 486M 0% /dev\ntmpfs 497M 12K 497M 1% /dev/shm\ntmpfs 497M 31M 466M 7% /run\ntmpfs 497M 0 497M 0% /sys/fs/cgroup\n/dev/vda1 1014M 121M 894M 12% /boot\ntmpfs 100M 0 100M 0% /run/user/1000\ntmpfs 100M 0 100M 0% /run/user/0"

id: node2.51yuki.cn

success: 1

full_ret: {"fun_args": ["df -h"], "jid": "20180120192640811889", "return": "Filesystem Size Used Avail Use% Mounted on\n/dev/mapper/cl-root 13G 1.6G 11G 13% /\ndevtmpfs 486M 0 486M 0% /dev\ntmpfs 497M 12K 497M 1% /dev/shm\ntmpfs 497M 31M 466M 7% /run\ntmpfs 497M 0 497M 0% /sys/fs/cgroup\n/dev/vda1 1014M 121M 894M 12% /boot\ntmpfs 100M 0 100M 0% /run/user/1000\ntmpfs 100M 0 100M 0% /run/user/0", "retcode": 0, "success": true, "fun": "cmd.run", "id": "node2.51yuki.cn"}

alter_time: 2018-01-20 19:26:40

*************************** 4. row ***************************

fun: cmd.run

jid: 20180120192640811889

return: "Filesystem Size Used Avail Use% Mounted on\n/dev/mapper/cl-root 13G 1.6G 11G 13% /\ndevtmpfs 486M 0 486M 0% /dev\ntmpfs 497M 12K 497M 1% /dev/shm\ntmpfs 497M 14M 483M 3% /run\ntmpfs 497M 0 497M 0% /sys/fs/cgroup\n/dev/vda1 1014M 121M 894M 12% /boot\ntmpfs 100M 0 100M 0% /run/user/0\ntmpfs 100M 0 100M 0% /run/user/1000"

id: node3.51yuki.cn

success: 1

full_ret: {"fun_args": ["df -h"], "jid": "20180120192640811889", "return": "Filesystem Size Used Avail Use% Mounted on\n/dev/mapper/cl-root 13G 1.6G 11G 13% /\ndevtmpfs 486M 0 486M 0% /dev\ntmpfs 497M 12K 497M 1% /dev/shm\ntmpfs 497M 14M 483M 3% /run\ntmpfs 497M 0 497M 0% /sys/fs/cgroup\n/dev/vda1 1014M 121M 894M 12% /boot\ntmpfs 100M 0 100M 0% /run/user/0\ntmpfs 100M 0 100M 0% /run/user/1000", "retcode": 0, "success": true, "fun": "cmd.run", "id": "node3.51yuki.cn"}

alter_time: 2018-01-20 19:26:40

4 rows in set (0.00 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值