Rails 4 can not connect to mysql after deploy - mysql

I got some problems with deploying to staging, hopefully anyone could suggest a solution.
So I am deploying Rails 4 with Capistrano to DO droplet. I have production/staging branches on different droplets (actually I am deploying staging for the first time and can't get it working).
Deployment went well, I have my app in "current" folder ready. But what I have troubles with is creating database. When I run
RAILS_ENV=production bundle exec rake db:create
I get
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8",
"reconnect"=>false, "database"=>"blabladb", "pool"=>5,
"username"=>"root", "password"=>"pass", "host"=>"111.11.11.111"},
{:charset=>"utf8", :collation=>"utf8_unicode_ci"}
I go to production.log and it says something like
FATAL Mysql2 Error Can't connect to MySQL server on "111.11.11.111"
(111)
I try to connect to mysql from console - everything goes fine. I do not know what the problem could be here.
My database.yml from current folder:
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: blabladb
pool: 5
username: root
password: pass
host: 111.11.11.111

Is there a user corresponding to that IP address in your mysql server. This is the reason why it works with localhost and not with IP address. Open your mysql server at production and check if access is granted to root at IP level.

Related

ActiveRecord NoDatabaseError when attempting to connect to AWS MySQL instance

We previously had a successful connection from Rails to an AWS MySQL instance through ActiveRecord, but as of our latest push to Heroku, that connection was severed. We don't understand why, as we have all traffic enabled on the AWS RDS settings, and the credentials in our database.yml config file have not changed. The only big thing that changed in the push to Heroku was adding the dovenv gem and passing control over environment variables to that gem.
When booting up our rails server in any of our three environments (dev, test, production), we receive a NoDatabaseError from ActiveRecord:
ActiveRecord::NoDatabaseError - Unknown database '<database_name>.us-east-1.rds.amazonaws.com'
We can successfully connect to the database through MySQL CLI.
database.yml:
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: "<username>"
password: "<password>"
host: <database_name>.us-east-1.rds.amazonaws.com
port: <port>
development:
<<: *default
database: test
test:
<<: *default
database: test
production:
<<: *default
Relevant local dependencies:
activerecord (5.1.4)
mysql2 (0.4.10)
rails (5.1.4)
AWS security groups:
default (<group_id>) CIDR/IP - Inbound 0.0.0.0/0
default (<group_id>) CIDR/IP - Outbound 0.0.0.0/0
Publicly accessible: Yes
What we have tried:
Restarting MySQL server: $ brew services list $ brew services restart mysql
Viewing MySQL logs on AWS, which reveals the error: [Warning] IP address '<ip_address>' could not be resolved: Name or service not known
Testing in production, which when attempting to connect to the database, gives the error: ActiveRecord::StatementInvalid: Mysql2::Error: No database selected
Running rake db:create RAILS_ENV=development, which gives the error:
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.`<possibly_private_id>`.`us-east-1`.`rds`.`amazonaws`.`com` DEFAULT CHARACTER SET `utf8`' at line 1
Uninstalling mysql, /usr/local/var/mysql, and reinstalling mysql (through brew)
Checking ENV variables, of which there are none that relate to MySQL.
Confirmed that my mysql server is running by running mysql.server status
I have a theory that when installing the gem dotenv-rails, we may have lost some MySQL credentials that granted access to the server from our project's local directory... Again, we still can connect to the server through MySQL CLI so we know everything on AWS is fine and healthy. I also can't seem to log in to mysql as my local username, or root. Currently trying to figure that issue out.
Solved - turns out it was related to the modifications I made in my environment variable setup. In this section of the Rails Guides, it notes that specifically declaring "DATABASE_URL" as an environment variable will automatically set or override the database variable declared in your config/database.yml file.
In my case, the host variable (set in config/database.yml) was correctly set to MY_DATABASE_NAME.amazon.aws, but DATABASE_URL variable in my environment was silently setting the config file's database variable to the same MY_DATABASE_NAME.amazon.aws.
I do not have a database called 'MY_DATABASE_NAME.amazon.aws', which is why it was freaking out. Hope this helps someone else!

Ruby on Rails - Mysql2::Error: Can't connect to MySQL server on 'localhost' (10061)

Hey I'm just getting started with Ruby on Rails and need some help. I installed Ruby on Rails and then created a project with MySQL but I am unable to connect to the MySQL server to create a database. Side note I am using Windows 10.
After running the command (rake db:create) I get this error.
#<Mysql2::Error: Can't connect to MySQL server on 'localhost' (10061)>
Here is my database.yml file
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: 1234
host: localhost
Assuming you're running Linux, try the following command to see if MySQL is running:
/etc/init.d/mysql status
Make sure you're specifying a database name to connect to as well, but that would likely give you a different error code than the one you're seeing.

Openshift RoR. Cant connect to db through mysql.sock

I have Openshift RoR app. When I trying to run some rake task which uses DB, it gives me error:
rake aborted!
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
When I pushed my app 1st time there was no socket value in database.yml, I added it later trying to fix this issue, set it to env variable of socket, which is:
"/var/lib/openshift/some_key/mysql//socket/mysql.sock
" In mysql.log I have this same socket too. So I don't understand where is this "/var/lib/mysql/mysql.sock" came from. I guess I need rebuild my app somehow, but it only restarts on "push" now. Or is this wrong idea?
EDIT: Here is my database.yml from .openshift/config. As i mentioned i added "soket: <%=ENV['OPENSHIFT_MYSQL_DB_SOCKET']%>" later, after first push. I just tried 'force_clean_build' marker, it didn't help though.
development:
adapter: mysql2
database: database
username: root
password: psswd
host: localhost
test:
adapter: mysql2
database: database
username: root
password: psswd
host: localhost
production:
adapter: mysql2
database: "<%=ENV['OPENSHIFT_APP_NAME']%>"
username: "<%=ENV['OPENSHIFT_MYSQL_DB_USERNAME']%>"
password: "<%=ENV['OPENSHIFT_MYSQL_DB_PASSWORD']%>"
host: <%=ENV['OPENSHIFT_MYSQL_DB_HOST']%>
port: <%=ENV['OPENSHIFT_MYSQL_DB_PORT']%>
soket: <%=ENV['OPENSHIFT_MYSQL_DB_SOCKET']%>
Turned out server didn't run all necessary rake:db tasks on deploy. That's why I was getting 'We are sorry...' error. And to run rake tasks through ssh, You need to add "RAILS_ENV=production" before it, like this:
RAILS_ENV=production rake db:setup
This 2 problems (not necessarily connected) made it look for me, like there is problem with DB config.

Rails Can't connect to MySQL server on 'localhost'

I'm having no end of trouble with my Rails 3 app's Mysql connection, though I have studied countless relevant threads. My error message:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2/client.rb:44:in `connect': Can't connect to MySQL server on 'localhost' (10061) (Mysql2::Error)
(Before you flag this as a duplicate question, consider whether you can find another thread with pertinent advice I have not followed.)
My efforts thus far:
I have copied the libmysql.dll file from <mysql installation>/bin to <ruby installation>/bin.
I have the mysql2 gem in the bundle, and it was installed with the connector (--with-mysql-dir=C:/mysql-connector-c-noinstall-6.0.2-win32):
> bundle show mysql2
C:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32
I believe my database.yml file is configured correctly:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: tq_development
pool: 5
username: root
password: pinney
host: localhost
try replacing localhost with 127.0.0.1 (in workbench and yml)
Have you ran rake db:create yet on the project in question?
I agree with the above....Make sure it works with workbench or some other GUI tool and you can connect using the info the database.yml file.
If not....come back and give us another holler.

Rails: error: database configuration does not specify adapter

The Rails application I'm developing is running fine on my local machine (MAC). When I upload it to the server (Centos 6.2 Linux, with Rails 3.2.3 and Passenger installed), and try to start the application (by entering the URL into my browser), I get the following error message:
Ruby (Rack) application could not be started There appears to be a
database problem.
Your application's database configuration file might be written
incorrectly. Please check it and fix any errors.
The database server may not be running. Please check whether it's
running, and start it if it isn't.
Error message:
database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified) Exception class:
ActiveRecord::AdapterNotSpecified
Note that when I ran rake db:create and rake db:migrate, both of these tasks ran fine and the databases are created and migrated properly.
Following is my database.yml file:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: acme_development
pool: 5
username: root
password: ***********
socket: /tmp/mysql.sock
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: acme_test
pool: 5
username: root
password: *****************
socket: /tmp/mysql.sock
Any ideas?
Phusion Passenger defaults to the "production" environment, as documented. It looks like you assume "development". In that case, set "RackEnv development".