use .my.cnf for ruby mysql connection - mysql

In a ruby script (not rails) Iam using mysql. Because my mysql are available in a .my.cnf file, I can use mysql on the terminal without password. But that does not work in a ruby script, how can I achieve that?

If you are using mysql2, you may need to read the .my.conf manually, as show in the doc
Reading a MySQL config file
You may read configuration options from a MySQL configuration file by
passing the :default_file and :default_group parameters. For example:
Mysql2::Client.new(:default_file => '/user/.my.cnf', :default_group =>
'client')

Related

Specify a particular database query from command line with MySQL Workbench

I would like to facilitate opening a database UI for development projects (usually docker containers, bound to arbitrary ports on the host machine) by a generic command.
I wonder if it is possible to open MySQL Workbench and let it connect automatically from the command line.
Similar to giving connection parameters with the mysql console:
mysql --host=127.0.0.1 --port=$port --user=db --password=db db
I haven't found that specifically in the supported arguments, so either it is hidden or maybe possible with any of the other options?
EDIT:
Probably the way is to generate a file to pass to --query dynamically?
Here's the format for the mysqlworkbench --query parameter:
--query="$user:$password#$host:$port"
This feature already exists as an example in ddev - look in the ~/.ddev/commands/host/mysqlworkbench.example file. (See on github).
For ddev, the query is set up as query="root:root#127.0.0.1:${DDEV_HOST_DB_PORT}", which uses the root/root credentials, accesses the 'db' container via localhost on the port provided by ddev at $DDEV_HOST_DB_PORT.

Does the MySQL Command Line Interface (CLI) have connection environment variables?

Does the MySQL Command Line Interface (CLI) or "shell" offer the ability to establish a connection using environment variables? The PostgreSQL CLI psql does via libpq with connection variables:
PGHOST
PGPORT
PGDATABASE
PGUSER
PGPASSWORD
I know I can connect via command-line switches and a ~/my.cnf file, but I'm wondering if there is an environment variable option analogous to the way psql works. Thanks!
Yes, for some of them: https://dev.mysql.com/doc/refman/8.0/en/environment-variables.html
MYSQL_HOST
MYSQL_TCP_PORT
There is no environment variable for the schema to use as the default schema. Client should specify that in the connection DSN, or else the USE schema statement after connecting.
There is no environment variable for the MySQL username.
MYSQL_PWD but this is deprecated and insecure and will be removed in a future version of MySQL. It's insecure for PostgreSQL for the same reason: another user can view your client's environment variables using ps.
It's more secure to use ~/.my.cnf, or you can specify a different option file to use. You can even store the username/password in an encrypted options file. See https://dev.mysql.com/doc/refman/8.0/en/option-files.html

How to work with emacs and mysql

I'm getting the message unable to locate SQL program 'mysql' when I attempt to enter sql mode in Emacs. However, I'm able to access mysql just fine via terminal.
For the record, I'm using user and password as root, server as localhost and a database of my choosing. I just don't know why this is happening.
Ideas?
Emacs uses the directories listed in the exec-path variable to look for programs. You can add to it like so:
(add-to-list 'exec-path "/usr/local/bin")
The following shell command should give you the correct value to use:
dirname `which mysql`

Rails with MySQL v.4.1

Is there any chance to make Rails application connect to old MySQL v4.1.20 server?
rails db
command works fine, but I cannot run the application.
When I generate new app with
rails new app_name -d mysql
I see the following in database.yml file:
MySQL. Versions 4.1 and 5.0 are recommended.
Which should mean I am able to use it.
However Rails Active Record uses MySQL v.5.0.2+ feature (https://github.com/rails/rails/blob/4-0-stable/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb):
# Make MySQL reject illegal values rather than truncating or blanking them, see
# http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_strict_all_tables
# If the user has provided another value for sql_mode, don't replace it.
if strict_mode? && !variables.has_key?(:sql_mode)
variables[:sql_mode] = 'STRICT_ALL_TABLES'
end
Unfortunately I have nothing to do with old MySQL server version. I have to use it.
Adding
strict: false
to the connector settings in database.yml solved the problem.

How to read in username/password from ".my.cnf" in Ruby MySQL driver?

Is there a way to connect to the MySQL server using the mysql gem without passing a username/pwd combo and instead loading it from the .my.cnf file?
Easiest way to load them is using inifile gem.
require 'inifile'
client_options_hash = IniFile.new('~/.my.cnf')['client']
Then you can just use the values from client_options_hash to set up your connection.