AWS Elastic Beanstock. rails cannot connect to MYSQL - mysql

I cannot solve this issue, mysql will not connect. using eb deploy. And I am using Ruby v 2.3.1
+ su -s /bin/bash -c 'leader_only bundle exec rake db:migrate' webapp
rake aborted! Mysql2::Error: Can't connect to MySQL server on 'labr.cyp2t1256323.us-west-2.rds.amazonaws.com' (4)
/opt/rubies/ruby-2.3.1/bin/bundle:23:in `load'
/opt/rubies/ruby-2.3.1/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace) (ElasticBeanstalk::ExternalInvocationError)
caused by: ++ /opt/elasticbeanstalk/bin/get-config container -k script_dir
Database.yml
production:
adapter: mysql2
encoding: utf8
reconnect: false
pool: 20
database: ***
username: *****
password: *****
host: labr.cyp2t1256323.us-west-2.rds.amazonaws.com
port: 3306
3306 is enabled on Amazon EC2 console, rails-env is at health red

Add a new security group that accept all traffic. And add your db instance to this group.
http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_SecurityGroups.html

Related

Cannot connect to local MySQL server using socket (2) in Elastic beanstalk using RDS database

I am creating a CI\CD for my Ruby on Rails application running in Elastic Beanstalk using code pipeline. My Code build succeeds but deployment to EBS provider fails with the below error
+ cd /var/app/ondeck
+ su -s /bin/bash -c 'bundle exec /opt/elasticbeanstalk/support/scripts/check-for-rake-task.rb db:migrate' webapp
`/home/webapp` is not a directory.
Bundler will use `/tmp/bundler20211229-11836-pzl6dq11836' as your home directory temporarily.
+ '[' false = true ']'
+ su -s /bin/bash -c 'leader_only bundle exec rake db:migrate' webapp
`/home/webapp` is not a directory.
Bundler will use `/tmp/bundler20211229-11840-9hxmcz11840' as your home directory temporarily.
rake aborted!
Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
/opt/rubies/ruby-2.6.7/bin/bundle:23:in `load'
/opt/rubies/ruby-2.6.7/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace) (Executor::NonZeroExitStatus)
I have
mysql --version
mysql Ver 14.14 Distrib 5.5.62, for Linux (x86_64) using readline 5.1
and could connect to External RDS MySQL Aurora database.
my Buildspec.yml:
version: 0.2
phases:
install:
runtime-versions:
ruby: 2.6
commands:
- echo Installing Bundler...
- gem install bundler
- bundle install
pre_build:
commands:
- echo Preparing database to run tests...
- bundle exec rails db:create --trace RAILS_ENV=production
build:
commands:
- echo Running tests...
- bundle exec rails test/test_helper.rb
artifacts:
files:
- '**/*'
My database.yml:
default: &default
adapter: mysql2
encoding: utf8mb4
collation: utf8mb4_unicode_ci
pool: 5
timeout: 5000
username: <%= ENV['DB_USER'] %>
password: <%= ENV['DB_PASSWORD'] %>
host: <%= ENV['DB_HOST'] %>
port: <%= ENV['DB_PORT'] %>
development:
<<: *default
database: <%= ENV['DB'] %>
test:
<<: *default
database: <%= ENV['DB_TEST'] %>
production:
<<: *default
database: <%= ENV['DB'] %>`
I found an answer for a similar problem connecting to a mysql server from Rails EB SSH that might still help you. Basically, Elastic Beanstalk was not successfully using my environment's envars to run my bundle exec rails console command in the eb ssh instance access.
I solved my issue by prepending all of the required envars explicitly to any rails commands I ran from within the eb ssh instance access. So for example, in order to run your su -s /bin/bash -c 'leader_only bundle exec rake db:migrate' webapp command, you may have to change that to run something like the following:
su -s /bin/bash -c 'leader_only RAILS_MASTER_KEY=xxxxxxx RAILS_ENV=production DB_HOST=xxxxxxx DB_PASSWORD=xxxxxxx DB_USER=xxxxxxx DB=xxxxxx AWS_REGION=xxxxxxx AWS_BUCKET=xxxxxxx bundle exec rake db:migrate' webapp
Replace the xxxxxxxs above with the values from the corresponding variables in your EB > Configuration > Software tab, and you should be able to connect to the remote database that way. After that, you will be able to run migration commands, rake tasks and other database-reliant functions. I'm not familiar with CI/CD but I hope the principles of this solution are adaptable to your project.
Thanks for your answers. But I faced the issue because of the master branch in git not updated properly.
After updating the master branch with proper application code, the CI\CD initiated as expected but got stuck in between because of env and credential file not in git.
I create a s3 bucket to store the env files and referred those file while packaging it for EBS deployment.
Hope this helps someone in future :)

Transition from mysql to postgress error

I wanted to change from mysql to postgresql.
I don't care about data so I have changed database.yml:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.postgresql
test:
<<: *default
database: db/test.postgresql
production:
<<: *default
database: db/production.postgresql
And run
rails db:reset db:migrate
But I get error
rails aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I have restarted both IDE (using c9) and server, but it gave no effect.
Any idea how to solve that problem?
#Edit After running sudo service postgresql start servers starts, and then when I run rails db:create
I get error:
psql: FATAL: role "ubuntu" does not exist
Just run this in your terminal :
$ cd /usr/local/var/postgres/
$ rm postmaster.pid
It's because last time your computer was shut off, Postgresql did not exit gracefully (usually due to running out of battery).

How to configure circle.yml in order to call the database?

I am trying to configure the CircleCi setup.
When I try to run the tests in my own environment then everything works out, but when I try to run it in Circle CI, then I haave got this error.
PHPUnit 5.6.0 by Sebastian Bergmann and contributors.
IIIII.IIIIIIIIIIIIIIIIIIIIIIIIIIIIIFFFFFFFFFFFFIIII..IIIIIIIIIIII 65 / 89 ( 73%)
IIIIIIIIIIIIIIIIIIIIIII. 89 / 89 (100%)
Time: 1.27 seconds, Memory: 56.00MB
There were 12 failures:
1) App\Test\TestCase\Controller\TagsControllerTest::testStages
exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /home/ubuntu/cemcloud2/vendor/cakephp/cakephp/src/Database/Driver/PDODriverTrait.php:48
This is the first part of the error.
I am getting that I have an error with my database configuration.
We are using Vagrant with the script (not sure if this is viable).
Also we are using mariaDB 10.1.19 and I have built up a test database for PHPUnit to run the tests against.
I don't know how to add this database with the configuration of mariadb and specific database.
I know that I have to add the
database.yml.ci
to my config file so I have added it in there and it looks like this.
test:
adapter: mysql
database: cemcloudTest
username: root
password: ''
Also I have added the circle.yml to our project root and it looks like this.
machine:
php:
version: 7.0
mysql:
version: 10.1.19
database:
override:
- cp config/database.yml.ci config/database.yml
- bundle exec rake db:create db:schema:load
Could someone help me out ?
in your cicle.yml database have to be created with name you use in your test config.
database:
override:
- mysql -u ubuntu -e "create database circle_ruby_test"
So that, this is the db conf:
test: # connection name
adapter: mysql2
encoding: utf8
reconnect: false
host: localhost
database: circle_ruby_test #your created DB
username: ubuntu #default circle user
pool: 25
socket: /var/run/mysqld/mysqld.sock # default sock location for circle
HTH:)

Rails - Multiple development instances with a single source and database.yml file

Can we connect to multiple development instances with a single source and a single database.yml file? I mean, I need to have 4 development instances from same source which connects 4 different MySQL databases. Can I start the servers by using different PORT IDs?
example:
development:
adapter: mysql2
database: TEST1_development
host: localhost
username: root
password: password
encoding: utf8
pool: 30
development-2:
adapter: mysql2
database: TEST2_development
host: localhost
username: root
password: password
encoding: utf8
pool: 30
development-3:
adapter: mysql2
database: TEST3_development
host: localhost
username: root
password: password
encoding: utf8
pool: 30
development-4:
adapter: mysql2
database: TEST4_development
host: localhost
username: root
password: password
encoding: utf8
pool: 30
Then, then how we can start 4 different servers (at localhost) connecting these 4 databases simultaneously? (we normally use rails server for single instance in development.)
Thanks :)-
You are not limited to the default rails environments. You could just have development-2, development-3 environments. Read this post on the 37signals blog where DHH talks about that
database.yml can read environment variables.
development:
adapter: postgresql
host: localhost
database: <%= ENV['POSTGRES_DATABASE'] %>
username: <%= ENV['POSTGRES_USER'] %>
password: <%= ENV['POSTGRES_PASSWORD'] %>
then make sure these are set when you start your server.
It is super easy.
Just update your database.yml file as given in the question. Suppose our development instances are development, development-2, development-3 and development-4.
First you just create 3 copies of the environment file development.rb (can find insideconfig/environments/) and rename it to development-2.rb, development-3.rb and development-4.rb.
Then, just do run the below steps before starting the servers.
RAILS_ENV=development rake db:create (OR just rake db:create as it defaults to development)
RAILS_ENV=development-2 rake db:create
RAILS_ENV=development-3 rake db:create
RAILS_ENV=development-4 rake db:create
RAILS_ENV=development rake db:schema:load (OR just rake db:schema:load as it defaults to development)
RAILS_ENV=development-2 rake db:schema:load
RAILS_ENV=development-3 rake db:schema:load
RAILS_ENV=development-4 rake db:schema:load
RAILS_ENV=development rake db:seed (OR just rake db:seed as it defaults to development)
RAILS_ENV=development-2 rake db:seed
RAILS_ENV=development-3 rake db:seed
RAILS_ENV=development-4 rake db:seed
Then, start all instances with different server pid's as given below.
rails s -p 3000 -e development --pid tmp/pids/server.pid (OR rails s by default)
rails s -p 3002 -e development-2 --pid tmp/pids/server.2.pid
rails s -p 3003 -e development-3 --pid tmp/pids/server.3.pid
rails s -p 3004 -e development-4 --pid tmp/pids/server.4.pid
That will start all instances simultaneously from the same source and a single database.yml file. Thank you all for all your replies and help :)-

mysql error help... while running rake db:setup RAILS_ENV="production"

I am diligently trying to get mysql up and running for my first rails app ever.
I keep getting the following error when running rake db:setup RAILS_ENV="production":
rake aborted!
dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dyl Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2/mysql2.bundle
Reason: image not found - /Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2 /mysql2.bundle
/Users/chris/rails_projects/sienab/Rakefile:4
(See full trace by running task with --trace)
I am running snow leopard, mysql 5.5, gem mysql2, rails 3.
Any help is great. many thanks.
database.yml below
# SQLite version 3.x
# gem install sqlite3
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# 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: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: sienab_production
pool: 5
username: username
password: password
host: localhost
Looks like you need to install the mysql2 libs:
sudo apt-get install libmysqlclient-dev libmysqlclient16
Also, I suggest you use gem 'mysql2','0.2.7' in your Gemfile if you're using Rails 3.0.x.
Don't forget to run bundle install after making this change.