2016-09-22 12:43:57 +00:00
---
title: About the mysql_session Resource
2018-02-16 00:28:15 +00:00
platform: os
2016-09-22 12:43:57 +00:00
---
# mysql_session
Use the `mysql_session` InSpec audit resource to test SQL commands run against a MySQL database.
2017-10-03 21:35:10 +00:00
<br>
2016-09-27 19:03:23 +00:00
## Syntax
2016-09-22 12:43:57 +00:00
A `mysql_session` resource block declares the username and password to use for the session, and then the command to be run:
describe mysql_session('username', 'password').query('QUERY') do
2017-12-21 22:06:39 +00:00
its('stdout') { should match(/expected-result/) }
2016-09-22 12:43:57 +00:00
end
where
2017-12-21 22:06:39 +00:00
* `mysql_session` declares a username and password, connecting locally, with permission to run the query
2016-09-22 12:43:57 +00:00
* `query('QUERY')` contains the query to be run
2017-12-21 22:06:39 +00:00
* `its('stdout') { should eq(/expected-result/) }` compares the results of the query against the expected result in the test
2016-09-22 12:43:57 +00:00
2017-10-03 21:35:10 +00:00
<br>
2016-09-22 12:43:57 +00:00
2016-09-27 19:03:23 +00:00
## Examples
2016-09-22 12:43:57 +00:00
The following examples show how to use this InSpec audit resource.
2016-09-27 19:03:23 +00:00
### Test for matching databases
2016-09-22 12:43:57 +00:00
sql = mysql_session('my_user','password')
describe sql.query('show databases like \'test\';') do
its('stdout') { should_not match(/test/) }
end
2017-06-23 15:28:15 +00:00
### Alternate Connection: Different Host
2017-12-21 22:06:39 +00:00
sql = mysql_session('my_user','password','db.example.com')
2017-06-23 15:28:15 +00:00
### Alternate Connection: Different Port
2017-12-21 22:06:39 +00:00
sql = mysql_session('my_user','password','localhost',3307)
2017-06-23 15:28:15 +00:00
### Alternate Connection: Using a socket
2017-12-21 22:06:39 +00:00
sql = mysql_session('my_user','password', nil, nil, '/var/lib/mysql-default/mysqld.sock')
2017-10-03 21:35:10 +00:00
2017-12-21 22:06:39 +00:00
### Test for a successful query
2017-10-03 21:35:10 +00:00
2017-12-21 22:06:39 +00:00
describe mysql_session('my_user','password').query('show tables in existing_database;') do
its('exit_status') { should eq(0) }
end
### Test for a failing query
describe mysql_session('my_user','password').query('show tables in non_existent_database;') do
its('exit_status') { should_not eq(0) }
end
2017-10-03 21:35:10 +00:00
2017-12-21 22:06:39 +00:00
### Test for specific error message
2017-10-03 21:35:10 +00:00
2017-12-21 22:06:39 +00:00
describe mysql_session('my_user','password').query('show tables in non_existent_database;') do
its('stderr') { should match(/Unknown database/) }
end
2017-10-03 21:35:10 +00:00
2017-12-21 22:06:39 +00:00
<br>
## Matchers
2017-10-03 21:35:10 +00:00
2018-02-16 03:07:18 +00:00
This InSpec audit resource builds a [command](https://www.inspec.io/docs/reference/resources/command) object and returns the the result object. For a full list of available matchers, please visit our [matchers page](https://www.inspec.io/docs/reference/matchers/).