Problem solving for MySQL module return value in Saltstack tool

Source: Internet
Author: User
Tags saltstack

One of the main features of the Saltstack integrated MySQL module in the development of MySQL management in the management system is Mysql.query

Two examples in the official documentation:


Salt.modules.mysql.query (database, query, **connection_args)

Run an arbitrary SQL query and return the results or the number of affected rows.

CLI Example:

Salt ' * ' mysql.query mydb "UPDATE mytable set myfield=1 limit 1"

Return Data:

{' Query time ': {' human ': ' 39.0ms ', ' raw ': ' 0.03899 '}, ' rows affected ': 1L}


CLI Example:

Salt ' * ' mysql.query mydb "select Id,name,cash from Users limit 3"

Return Data:

{' Columns ': (' id ', ' name ', ' cash '),

' Query time ': {' human ': ' 1.0ms ', ' raw ': ' 0.001 '},

' Results ': ((1L, ' User 1 ', Decimal (' 110.000000 ')),

(2L, ' User 2 ', Decimal (' 215.636756 ')),

(3L, ' User 3 ', Decimal (' 0.040000 ')),

' Rows returned ': 3L}


An example of my test:

Salt www.pyadmin.com mysql.query Salt "explain select * from Salt_events"

Www.pyadmin.com:

----------

Query time:

----------

Human

14.0ms

Raw

0.01402

Rows affected:

1


Look at the official and my test output shows that only the SELECT statement shows the desired result.

So I'm going to show the results of explain? Viewed many times the official document is not a clue, but from the source code to start

First, find this relevant module--mysql

Main position in/usr/local/lib/python2.7/dist-packages/salt/modules/mysql.py

Note: Some systems are under the Site-packages folder

Edit this file to find the following location:

ret = {}ret[' query time '] = {' human ': elapsed_h,  ' raw ':  str (Round ( elapsed, 5))}select_keywords = ["select",  "SHOW",  "DESC"]select_query =  Falsefor keyword in select_keywords:    if query.upper (). Strip (). StartsWith (keyword):        select_query = true         breakif select_query:    ret[' rows returned ' ] = affected    columns =  ()     for column  in cur.description:        columns +=  (Column[0], )     ret[' columns '] = columns    ret[' results '] =  results    return retelse:    ret[' rows affected '] =  affected  &nbSp; return ret 

Add an element such as "EXPLAIN" to the dictionary

Select_keywords = ["select", "SHOW", "DESC", "EXPLAIN"]


And then we're going to look at the output of the execution results:


Www.pyadmin.com:

----------

Columns

-ID

-Select_type

-Table

-Partitions

-Type

-Possible_keys

-Key

-Key_len

-Ref

-Rows

-Filtered

-Extra

Query time:

----------

Human

0.24s

Raw

0.23805

Results:

|_

-1

-Simple

-Salt_events

-None

-All

-None

-None

-None

-None

-3707

-100.00

-None

Rows returned:

1

Compared with the output before the modification, results this one.

This article is from "Shadow Knight" blog, please make sure to keep this source http://andylhz2009.blog.51cto.com/728703/1951339

Problem solving for MySQL module return value in Saltstack tool

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.