Skip to content

mysql_user or mysql_database can not connect to the database #674

@doganoo

Description

@doganoo

🗣️ Foreword

Thank you for providing this awesome cookbook.

👻 Brief Description

When trying to use the mysql_database and mysql_user resources, Chef throws error with "Can't connect to local MySQL server through socket '/var/run/mysql/mysqld.sock".

Please notice I am not trying to connect on the shell within the Vagrant Box.

The Cookbook:

credentials = {
'user': 'keestash',
'password': 'keestash',
'config_name': 'default'
}

mysql_config 'default' do
  source 'my_cnf.erb'
  instance 'default'
  notifies :restart, "mysql_service[default]"
  action :create
end

mysql_service 'default' do
  port '3306'
  version '8.0'
  initial_root_password credentials['password']
  action [:create, :start]
end

# Create a database
# mysql_database 'keestash' do
#   host 'localhost'
#   user 'root'
#   socket '/var/run/mysql/mysqld.sock'
#   password credentials['password']
#   action :create
# end

mysql_user 'keestash' do
  ctrl_password credentials['password']
  password credentials['password']
  host '%'
  action :create
end

The my_cnf.erb

# Chef generated my.cnf for instance mysql

[client]
socket                         = /var/run/mysql/mysqld.sock

The kitchen.yml:

---
driver:
  name: vagrant

## The forwarded_port port feature lets you connect to ports on the VM guest via
## localhost on the host.
## see also: https://www.vagrantup.com/docs/networking/forwarded_ports

#  network:
#    - ["forwarded_port", {guest: 80, host: 8080}]

provisioner:
  name: chef_zero

  ## product_name and product_version specifies a specific Chef product and version to install.
  ## see the Chef documentation for more details: https://docs.chef.io/workstation/config_yml_kitchen/
  #  product_name: chef
  #  product_version: 17

verifier:
  name: inspec

platforms:
  - name: ubuntu-20.04

max_retries: 3
multiple_converge: 3
wait_for_retry: 600

suites:
  - name: default
    verifier:
      inspec_tests:
        - test/integration/default
    attributes:

attributes/apparmor.rb:

default[:apparmor][:disable] = true

🥞

Cookbook Version: 11.0.0

Chef Workstation version: 21.11.679
Chef Infra Client version: 17.7.29
Chef InSpec version: 4.49.0
Chef CLI version: 5.4.2
Chef Habitat version: 1.6.420
Test Kitchen version: 3.1.1
Cookstyle version: 7.25.9

Steps To Reproduce

Steps to reproduce the behavior:

  1. Add mysql cookbook to policyfile
  2. create mysql cookbook
  3. add instructions (see above)
  4. run kitchen converge or kitchen converge ubuntu

🚓 Expected behavior

A running MySQL instance with the given user/database

Error:

      * mysql_user[keestash] action create[2021-12-17T09:15:46+00:00] FATAL: mysql failed executing this SQL statement:
       SELECT User,Host FROM mysql.user WHERE User='keestash' AND Host='%';
       [2021-12-17T09:15:46+00:00] FATAL: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysql/mysqld.sock' (2)
       
       
           
           ================================================================================
           Error executing action `create` on resource 'mysql_user[keestash]'
           ================================================================================
           
           RuntimeError
           ------------
           SQL ERROR
           
           Cookbook Trace: (most recent call first)
           ----------------------------------------
           /tmp/kitchen/cache/cookbooks/mysql/libraries/helpers.rb:373:in `execute_sql'
           /tmp/kitchen/cache/cookbooks/mysql/resources/mysql_user.rb:60:in `block in class_from_file'
           
           Resource Declaration:
           ---------------------
           # In /tmp/kitchen/cache/cookbooks/keestash/recipes/mysql.rb
           
            33: mysql_user 'keestash' do
            34:   ctrl_password credentials['password']
            35:   password credentials['password']
            36:   host '%'
            37:   action :create
            38: end
            39: 
           
           Compiled Resource:
           ------------------
           # Declared in /tmp/kitchen/cache/cookbooks/keestash/recipes/mysql.rb:33:in `from_file'
           
           mysql_user("keestash") do
             action [:create]
             default_guard_interpreter :default
             declared_type :mysql_user
             cookbook_name "keestash"
             recipe_name "mysql"
             password "*sensitive value suppressed*"
             host "%"
             privileges [:all]
           end
           
           System Info:
           ------------
           chef_version=17.8.25
           platform=ubuntu
           platform_version=20.04
           ruby=ruby 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x86_64-linux]
           program_name=/opt/chef/bin/chef-client
           executable=/opt/chef/bin/chef-client


Please let me know if you need additional information. Thank you in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions