I’m on a Mac and used brew to install an updated MySQL. In an effort to streamline my local database creation, I took the password off my local root user (I know, I know), and after creating a new Rails app (rails new myapp -d mysql), should just be able to run rake db:create, right?
However, despite having /etc/my.cnf set to define the standard mysql.sock location…
[mysqld]
socket=/usr/local/var/mysql/mysql.sock
[client]
socket=/usr/local/var/mysql/mysql.sock
… Rails still won’t use the newly defined mysql.sock file on a rake db:create:
rake db:create
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "reconnect"=>false, "database"=>"delayed_jobs_development", "pool"=>5, "username"=>"root", "password"=>nil, "host"=>"localhost"}, charset: , collation:
Running mysqladmin variables lists the correct socket file location, as does mysql --help. I’ve restarted the MySQL server and even recreated the Rails app.
So my question: What else could be defaulting the socket file to /tmp/mysql.sock, or is there an alternate, preferably global, config file where I could specify its location that the rake task will honor? Could the rake task be calling a different commandline MySQL tool that uses a different config file?
Obviously, I could either create a symlink from the /tmp location to my real location, or edit my database.yml file to refer to it. I understand how to get Rails to talk to the DB server correctly, but the point is to have the proper defaults set up once, so any future Rails app I create locally is good to go without extra edits (/tmp/mysql.sock gets cleared on reboot).
In fact, I’m not even sure why it’s trying to connect via the socket file at all, since my database.yml file tells it to use the hostname:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: myapp_development
pool: 5
username: root
password:
host: localhost
As the MySQL documentation says,
/tmp/mysql.sockis the default location for the socket file. This is also what rails defaults to (more below).As you mentioned, you can always set the socket (the one you configured and that is returned by
mysqladmin variables) explicitly in your database.yml file:I think the connection is made via the socket file as you set
host: localhost. If you change that to127.0.0.1, the connection should by made over TCP/IP.I found rather old information (a really old version of the mysql adapter and a blog entry from 2007) which implies that the
MYSQL_UNIX_ADDRenvironment variable is used in resolving the socket. Also see this comment on GitHub by the developer of Ruby/MySQL, in which he says to set theMYSQL_UNIX_PORTvariable instead. A third env variable isMYSQL_SOCKETmentioned in the source of the Ruby/MySQL connector.I do not know if these variables get evaluated somewhere along the way. Still, you could try them out.
Right now, rails
mysql_adapter.rbsays that the configuredsocketis used and that it defaults to/tmp/mysql.sock(set inapp_generator.rb). No other locations seem to get checked. I did not found anything else in the defaultRuby/MySQLadapter used by rails.So basically – without being an expert on this topic – I don’t think there is a way to set a global default location for the MySQL socket file that gets evaluated by rails or the used default adapter.