Ruby on Rails mein MySQL ya PostgreSQL Connect Karne ka Tareeka

Ruby on Rails mein MySQL ya PostgreSQL Connect Karne ka Tareeka

Namaskar coders! Agar aap Rails ke saath khel rahe ho aur MySQL ya PostgreSQL database connect karna chahte ho, toh yeh guide aapke liye hai. Chaliye step-by-step samajhte hain:


1. Pehla Kadam: Gem Setup Karo

Database connect karne ke liye, pehle Gemfile mein sahi gem add karo.

MySQL Ke Liye:

# Gemfile
gem 'mysql2'  # MySQL gem

Terminal chalao:

bundle install

PostgreSQL Ke Liye:

# Gemfile
gem 'pg'  # PostgreSQL gem

Terminal chalao:

bundle install

2. Database Configuration: config/database.yml

config/database.yml file ko edit karo. Default SQLite settings hatao aur apne database ke hisaab se details daalo.

MySQL Configuration:

development:
  adapter: mysql2
  encoding: utf8mb4
  database: your_db_name   # Apna database naam daalo
  username: root          # Default MySQL user
  password: your_password # MySQL password
  host: localhost         # Local machine par
  port: 3306              # Default MySQL port

PostgreSQL Configuration:

development:
  adapter: postgresql
  encoding: unicode
  database: your_db_name   # Apna database naam daalo
  username: postgres       # Default PG user
  password: your_password  # PostgreSQL password
  host: localhost          # Local machine par
  port: 5432               # Default PostgreSQL port

💡 Tip: Agar password nahi hai toh password: ko comment kar do ya null rakho.


3. Database Create Karo & Tables Setup Karo

Rails commands se database aur tables banaye:

# Database create karo
rails db:create

# Migrations run karo (tables banane ke liye)
rails db:migrate

Check Karo:

  • MySQL: mysql -u root -pSHOW DATABASES;
  • PostgreSQL: psql -U postgres\l

4. Server Start Karo & Test Karo

Ab Rails server chala ke check karo connection:

rails server

Agar sab sahi hai toh localhost:3000 par “Yay! You’re on Rails!” dikhega.


5. Agar Error Aaye Toh? (Common Fixes)

Case 1: MySQL Password Issue

  • MySQL mein password set kiya hai? Agar nahi, toh password: "" rakho.
  • Password set karne ke liye:
  mysql -u root
  ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';

Case 2: PostgreSQL User Permissions

  • PostgreSQL mein user ko database create karne ka permission do:
  psql -U postgres
  CREATE USER your_user WITH CREATEDB;

Case 3: Gem Missing

  • bundle install phir se chalao aur server restart karo.

6. Production/Heroku Setup

Heroku par PostgreSQL use hota hai. Gemfile mein yeh ensure karo:

# Production mein PostgreSQL use karo
gem 'pg', group: :production
gem 'mysql2', group: :development # Dev mein MySQL

Heroku par deploy karte waqt:

heroku addons:create heroku-postgresql

7. Important Commands Cheat-Sheet

CommandDescription
rails db:createDatabase banata hai
rails db:migrateTables create/update karta hai
rails db:dropDatabase delete karta hai
rails dbconsoleDatabase CLI open karta hai

8. Final Tip: .env File for Security

Password directly database.yml mein na likho! .env file use karo:

# .env file
DB_USERNAME=root
DB_PASSWORD=super_secret

Aur database.yml mein:

password: <%= ENV['DB_PASSWORD'] %>

Gem add karo: gem 'dotenv-rails'.


Conclusion:
MySQL ho ya PostgreSQL, Rails ke saath connect karna ekdum easy hai! Bas gems aur configuration sahi rakho. Agar stuck ho toh rails dbconsole chala ke direct database se connect ho kar dekho. 😊

Happy Coding! ✨🚀
“Keep calm, rake db:migrate chalao!”

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *