How to change docker compose for Rails and MySql2? - ruby-on-rails

I have an ancient (like 2012) Ruby on Rails server that uses MySql2 database. The server, of course, is not made by me so I now have to find out how even launch it. And I want to use Docker for that.
I found this guide to setup Docker container for Rails and PostgreSQL database. So, what do I have to change here to run it with MySql2?
Here is my RoR server's database setup:
adapter: mysql2
encoding: utf8
database: g_development
pool: 5
username: notactualusername
password: notactualpassword
This is a line from Dockerfile in the tutorial that installs and updates all the stuff
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
I need to change postgresql to mysql2, right?
This is from tutorial docker-compose
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: password
So, is MySql2 image just called mysql2? Is the password variable called MYSQL2_PASSWORD or something else?
This is also from tutorial (initialization):
docker compose run --no-deps web rails new . --force --database=postgresql
I guess changing here postgresql to mysql2? Do I actually need to run rails new . since I already have a server built?

If you want to use mysql, you need change database.yml
Don't forget run xampp
I use Ubuntu Linux
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: <%= ENV['USERNAME']%>
password: <%= ENV['PAASSWORD']%>
socket: /opt/lampp/var/mysql/mysql.sock // run xampp
host: <%= ENV["DB_HOST"]%>

Related

How do you connect to an AWS RDS postgres db from cloud foundry on PWS?

I am using PWS to run my rails app but we are having trouble connecting to our postgres db instance running on AWS RDS. We tried unbinding any ElephantSQL services we have and changing the DATABASE_URL ENV to the postgres login url, but that did not work. We also have tried updating our database.yml but nothing happens there either.
This seems like it SHOULD be easier but it appears that the database.yml is not read on PWS.
How can we connect to our RDS db from a PWS instance?
production:
adapter: postgresql
host: <%= ENV["DATABASE_URL"] %>
port: <%= ENV["DATABASE_PORT"] %>
database: <%= ENV["DATABASE_NAME"] %>
pool: 15
encoding: utf8
username: <%= ENV["DATABASE_USER_NAME"] %>
password: <%= ENV["DATABASE_PASSWORD"] %>
psql --host=xxxxx-prod.xxxxx.us-east-1.rds.amazonaws.com --port=5432 --username=multilang_prod_xxxx --password --dbname=multilang_prod_xxxx
try manually connecting
psql --host=multilang-prod.cnvr5o86qwm2.us-east-1.rds.amazonaws.com --port=5432 --username=multilang_prod --password --dbname=multilang_prod
sudo apt install postgresql-client-common
sudo apt install postgresql-client-9.5

How to set instance name database in ruby configuration

My app (ruby on rails) is in linux and I need connect in a Sql Server database.
Testing in command line I get:
$tsql -LH 10.10.10.10
ServerName MYCOMPSRV10
InstanceName SQL2008R2
IsClustered No
Version 10.50.4000.0
tcp 58891
And I can access database in command line using:
$tsql -S 10.10.10.10\\SQL2008R2 -U username
So, in my Ruby on Rails app I have database.yml with this code:
development:
adapter: sqlserver
host: 10.10.10.10
database: MyDatabase
username: username
password: password
port: 1433
pool: 5
timeout: 5000
But when I try run the app I get:
TinyTds::Error: Unable to connect: Adaptive Server is unavailable or does not exist
If I change config an try to add Database Instance Name:
development:
adapter: sqlserver
host: '10.10.10.10\SQL2008R2'
database: MyDatabase
username: username
password: password
port: 1433
pool: 5
timeout: 5000
And try run the App:
TinyTds::Error: Server name not found in configuration files
I did a research and don't found how to solve this problem. I even don't found how to set the instance name of database.
Can someone help me ?
I found a solution:
install:
unixodbc
unixodbc-bin
unixodbc-dev
sudo apt-get install unixodbc unixodbc-bin unixodbc-dev
uninstall
freetds-bin
freetds-dev
freetds-common
sudo apt-get purge freetds-bin freetds-dev freetds-common
download freetds current and install:
tar -xzf freetds-patched.tar.gz
cd freetds-1.00.15/
./configure
make
sudo make install
I discovered the dinamic port of my db instance trough this query:
SELECT local_tcp_port
FROM sys.dm_exec_connections
WHERE session_id = ##SPID
and change database.yml config adding this port instead default 1433
development:
<<: *default
host: 10.10.10.10
username: myuser
password: mypwd
port: 58891
after that I run:
rake db:migrate
Everything worked fine

Heroku pg:psql not working - PATH not correct?

I want to connect to the heroku pg:psql but it's not working anymore. Don't know what to do after searching the web and also Stackoverflow for hours.
The command heroku pg:psql gives me this error:
The local psql command could not be located
For help installing psql, see article about local-postgres
Working on a Mac with OS X (Version 10.9.1), Ruby 1.9.3. and Rails 4.0.1.
My database.yml is as following:
development:
adapter: postgresql
encoding: unicode
database: outspotly_v1_development
pool: 5
username: fabianwydler
host: localhost
port: 5432
test:
adapter: postgresql
encoding: unicode
database: outspotly_v1_test
pool: 5
username: fabianwydler
host: localhost
port: 5432
production:
adapter: postgresql
encoding: unicode
database: outspotly_v1_production
pool: 5
username: fabianwydler
host:
port:
Is there, perhaps, something wrong with my $PATH?
heroku run rails db
Running `rails db` attached to terminal... up, run.3540
Couldn't find database client: psql. Check your $PATH and try again.
Fabians-MacBook-Pro:outspotly_v1 fabianwydler$ echo $PATH
/Users/fabianwydler/.rvm/gems/ruby-1.9.3-p194/bin:/Users/fabianwydler/.rvm/gems/ruby- 1.9.3-p194#global/bin:/Users/fabianwydler/.rvm/rubies/ruby-1.9.3-p194/bin:/Users/fabianwydler/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
If the $PATH is wrong I don't know how to handle this because I'm not so familiar on Mac.
Thank you very much for your help.
Greetings, Fabian
You need to install a local copy of psql. By far the easiest way is to install homebrew and then postgresql:
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
brew install postgresql

How to set up Postgres database for local Rails project?

I recently got a new machine and would now like to work on my projects from Github. I'm curious as to how to properly set up the Postgres database on my local machine. I have postgresql, pgadmin3 and libpq-dev installed on Ubuntu (12.04).
I pull down the project:
git clone https://github.com/thebenedict/cowsnhills.git
and run:
bundle.
When I run:
rake db:create && rake db:schema:load
I get this error:
rake db:create && rake db:schema:load
FATAL: password authentication failed for user "cnh"
FATAL: password authentication failed for user "cnh"
....
The config/database.yml file looks like this:
development:
adapter: postgresql
encoding: unicode
host: localhost
database: cnh_development
pool: 5
username: cnh
password: cnh
test:
adapter: postgresql
encoding: unicode
host: localhost
database: cnh_test
pool: 5
username: cnh
password: cnh
production:
adapter: postgresql
encoding: unicode
host: localhost
database: cnh_production
pool: 5
username: cnh
password: cnh
What's the proper way to set up the Postgres database so I can run this project on my local machine?
Right now when I start the Rails server I get:
I came across your question when looking for the same answer. I attempted to follow the instructions #prasad.surase gave you. The problem I found is the ppa repository is going to depreciate soon on 12.04 LTS. Instead I found this link and it really helped.
PostgreSQL setup for Rails development in Ubuntu 12.04
Install postgresql and admin tools through the package manager
sudo apt-get install postgresql libpq-dev phppgadmin pgadmin3
Login to postgresql prompt as the postgres user
sudo su postgres -c psql
Create a postgresql user for your project
create user username with password 'password';
Setup your postgres user with the same name and password as your Ubuntu user and make him a postgres superuser
alter user username superuser;
Create the development and test databases
create database projectname_development;
create database projectname_test;
Give permissions to the user on the databases
grant all privileges on database projectname_development to username;
grant all privileges on database projectname_test to username;
To end the postgresql session type \q
Update password for the user
alter user username with password ‘new password’;
firstly, install postgresql
sudo add-apt-repository ppa:pitti/postgresql
sudo apt-get update
#now install postgresql
sudo apt-get install postgresql-9.1 libpq-dev
create a new user in psql
sudo su postgres
createuser user_name #Shall the new role be a superuser? (y/n) y
Gemfile
gem 'pg'
bundle install
development.yml
development:
adapter: postgresql
database: app_development
pool: 5
username: user_name
password:
You follow this link http://www.cyberciti.biz/faq/howto-add-postgresql-user-account/
to create a postgres user and replace the credentials in database.yml

Rails Connect with Postgresql database in application ubuntu

I am using ubuntu 12.04 and rails 3.2. I am creating a rails application in which I'm using PostgreSQL databse. I installed postgresql using the following command:
sudo apt-get install postgresql
for reference i checked out https://help.ubuntu.com/community/PostgreSQL. Later I created the user postgres and set the password postgres using the following command
sudo -u postgres psql postgres
\password postgres
Next I created the database using:
sudo -u postgres createdb mydb
I tried to connect with Postgresql with the username postgres and password postgres and got successfully connected with the following command:
psql -U postgres -h localhost
Password for user postgres:
psql (9.1.4)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
postgres=#
In my rails app my database.yml has the following code:
development:
adapter: postgresql
encoding: unicode
database: mydb_development
pool: 5
username: postgres
password: postgres
test:
adapter: postgresql
encoding: unicode
database: mydb_test
pool: 5
username: postgres
password: postgres
production:
adapter: postgresql
encoding: unicode
database: mydb_production
pool: 5
username: postgres
password: postgres
Now when I run the command rake db:migrate i get the following error:
rake aborted!
FATAL: Peer authentication failed for user "postgres"
I tried adding host: localhost to database.yml for each environment and i get the following error:
rake aborted!
couldn't parse YAML at line 28 column 0
The line 28 is
development:
adapter: postgresql
encoding: unicode
database: hackathonio_development
pool: 5
username: postgres
password: testing
host: localhost {This is line 28}
Please help me figure out a solution for this..
I think you may have 2 problems. First, the host not being set as Shreyas pointed out. But I believe the second problem is that when you set the hostname Rails is trying to connect to PostgreSQL via a tcp socket and not a local ruby socket. My guess is you need to modify your pg_hba.conf file to allow postgres to login via localhost. Below are a few SO questions with answers that may help.
Rails can't login to postgresql - PG::Error - password - Correct info
What's the difference between "local" and "localhost" connection types in pg_hba.conf?
Can't use postgres user in new database for rails 3 on ubuntu 10.04 server
Please add host to your yml as localhost
My recommendation:
Step 1
Create a new, different user by running
$ createuser <username>
on the command line. You'll be prompted for password, etc. (Examples/docs: http://www.postgresql.org/docs/8.1/static/app-createuser.html)
Step 2
Update database.yml with the new users's username/password. Forget the first user you created, at least for now.
Step 2
$ rake db:create
Step 3
$ rake db:migrate
I think those steps are more likely to work that what you're trying.

Resources