Can't run rails "db: create" with remote ip on mysql - mysql

Here is the log
Can't connect to MySQL server on 'remote.ip.here.lol' (110)
Couldn't create 'solicit' database. Please check your configuration.
rails aborted!
Mysql2::Error: Can't connect to MySQL server on 'remote.ip.here.lol' (110)
bin/rails:4:in `<main>'
Tasks: TOP => db:create
(See full trace by running task with --trace)
my db.yml is like this:
default: &default
adapter: mysql2
encoding: utf8
database: db
host: remote.ip.here.lol
port: 3306
username: admin
password: supressed
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
test:
<<: *default
I don't have any clue of how to start, I recently got hired, my boss told about a gem dotenv-rails but I can't see how it fit to the current issue that I'm curently facing.
Edit: apparently using localhost and local database and user works fine, but not with the remote configuration.
Edit 2: I've created environment variables with dotenv and apparently I only need to fill a .env file with the needed information, it worked.

You might not have MySQL running on your machine? It seems you are trying to connect to your server but MySQL is not up.

Related

How to have a mysql database in my production environment when deployed to Heroku

I have a Rails application that I've deployed to heroku, and I don't know how to have a mysql database for the production environment. (The mysql database for local environment is already created without a problem.)
Question1:
Here are some parts of heroku config. You can see below that CLEARDB_DATABASE_URL and DATABASE_URL share the same host, but the other parts are different. Which should be included in the production part of database.yml?
$ heroku config
=== exampleapp Config Vars
CLEARDB_DATABASE_URL:mysql://<username1>:<password1>#<host1>/<database1>?reconnect=true
DATABASE_URL:mysql2://<username2>:<password2>#<host1>/<database2>?reconnect=true
Here's my database.yml. (I've included the username, host and password of CLEARDB_DATABASE_URL.)
default: &default
pool: 5
timeout: 5000
development:
<<: *default
adapter: mysql2
database: exampleapp
pool: 5
timeout: 5000
username: root
password: xxx
host: localhost
production:
<<: *default
adapter: mysql2
database: exampleapp
username: <username1>
host: <host1>
password: <password1>
Question2:
I've run "heroku run rails db:migrate", but the tables weren't created, even though all the necessary migration files are created under db/migrate directory in my repository.
When I checked the mysql for heroku production environment (is this the right way to check it?), this is what happened;
$ mysql -u <username1> -p -h <host1> //username and host of CLEARDB_DATABASE_URL
$ mysql> show tables;
Empty set (0.18sec)
If I create the tables from scratch here using SQL, do they get linked to the app? (CREATE TABLE db_name.tbl_name (col_name data_type,...) etc.)
I have a whole set of database for local environment, so it'd be great if I can move the tables with the data inside to the production environment. Are there any ways I can do so?
For production systems it is best practice to pull the database information from environment variables rather than hardcoding them in config/database.yml. That's why the DATABASE_URL environment variable is set in Heroku. You can just access it in config/database.yml like this:
production:
<<: *default
adapter: mysql2
url: <%= ENV['DATABASE_URL'] %>
Second, db:migrate runs migrations against an existing database, but it doesn't create it. If you are using a version of Rails earlier than 6.0, there are three tasks to set up a new database:
% heroku run rails db:create
% heroku run rails db:schema:load
% heroku run rails db:seed
The db:create tasks creates the database, db:schema:load initializes your app's tables from db/schema.rb, and db:seed seeds the database using whatever you've put in db/seeds.rb
Rails 6+ adds a db:prepare task that rolls the creation tasks into one:
% heroku run rails db:prepare
As mentioned above. We shouldn't hard code database details in code As this information get updated in case of database upgrades by ClearDB. This should be pulled from environment variables. As from ENV its complete URL, We can extract details like below
require 'uri'
database_url = URI.parse(ENV['DATABASE_URL'])
user_info = database_url.userinfo.split(':')
default: &default
pool: 5
timeout: 5000
development:
<<: *default
adapter: mysql2
database: database_url.path[1..-1]
pool: 5
timeout: 5000
username: user_info.first
password: user_info.last
host: database_url.host
For the second question, Can you check your schema.rb. is it having all the tables required on production

Ruby on Rails Installation, Ubuntu on Windows10 #<Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)>

I have installed Ruby on Rails on Ubuntu on Windows 10 following the instructions here: https://gorails.com/setup/windows/10
when I create the database (rake db:create) I receive the following error message twice:
User1#My-Macine:~/test1$ rake db:create
#<Mysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>"xxxxx", "host"=>"localhost", "database"=>"test1_development"}, {:charset=>"utf8", :collation=>"utf8_unicode_ci"}
(If you set the charset manually, make sure you have a matching collation)
My config/database.yml is the following:
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: xxxxx
host: localhost
development:
<<: *default
database: test1_development
test:
<<: *default
database: test1_test
production:
<<: *default
database: test1_production
username: test1
password: <%= ENV['TEST1_DATABASE_PASSWORD'] %>
I also checked for mysqld dir and mysqld.sock file and they did not exist.
So, according other answers, I tried to create mysqld dir and restart mysql
sudo mkdir /var/run/mysqld/
sudo service mysql restart
The restart failed and received the following error message
initctl: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused
I have checked solutions from similar cases/questions here but couldn't resolve it.
Can you please help me to resolve this?
Finally, I found it. I had also to create the mysqld.pid and mysqld.sock files inside the new created mysqld dir and restart mysql.
I found the answer here: https://superuser.com/questions/980841/why-is-mysqld-pid-and-mysqld-sock-missing-from-my-system-even-though-the-val

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: 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".

Redmine installation issues rake command issue, MySQL database issue?

I have somewhat of a complex issue involving several different programs including redmine, MySQL, Ruby, Ruby on Rails etc. This is on Windows XP.
I am following the redmine instructions here: http://www.redmine.org/projects/redmine/wiki/RedmineInstall
I am on step 5. where I am suppose to type RAILS_ENV=production rake db:migrate
When I type this in the command prompt and hit enter I get an error: "RAILS_ENV" is not a command blah blah.
So I reorder it to: rake db:migrate RAILS_ENV=production
This seems to work correct, but I get the following:
C:\redmine-1.2.1>rake db:migrate RAILS_ENV=production --trace
* Invoke db:migrate (first_time)
* Invoke environment (first_time)
* Execute environment
rake aborted!
Access denied for user 'redmine'#'localhost' (using password: YES)
C:/redmine-1.2.1/vendor/rails/activerecord/lib/active_record/connection_adapters
/mysql_adapter.rb:620:in `real_connect'
C:/redmine-1.2.1/vendor/rails/activerecord/lib/active_record/connection_adapters
/mysql_adapter.rb:620:in `connect'
C:/redmine-1.2.1/vendor/rails/activerecord/lib/active_record/connection_adapters
/mysql_adapter.rb:203:in `initialize'
C:/redmine-1.2.1/vendor/rails/activerecord/lib/active_record/connection_adapters
/mysql_adapter.rb:75:in `new'
Here is my database.yml file contents:
MySQL (default setup).
production:
adapter: mysql
database: redmine
host: localhost
username: redmine
password: ****
encoding: utf8
development:
adapter: mysql
database: redmine_development
host: localhost
username: root
password:
encoding: utf8
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql
database: redmine_test
host: localhost
username: root
password:
encoding: utf8
test_pgsql:
adapter: postgresql
database: redmine_test
host: localhost
username: postgres
password: "postgres"
test_sqlite3:
adapter: sqlite3
database: db/test.sqlite3
I really need some direction here. It's almost like there is a problem with my users/passwords. I have changed the passwords not to contain "!" or any other special characters. I do have capital and lower case letters.
Any help would be greatly appreciated.
DemiSheep
So, you have created the 'redmine' mysql user with the password specified in database.yml?
Can you connect to this user using mysql client? (e.g. mysql -uredmine -pyourmysqlpassword)
Does the user have all required privileges?