In my current application i am using PostgreSQL Data base,
but I want to change the PostgreSQL database into MYSQL DB.
if it's impossible ?
Step 1
Make a backup copy of your data
For Rails 3, install the YAML DB gem: https://github.com/ludicast/yaml_db
For Rails 2.x install the YAML DB plugin:
script/plugin install git://github.com/adamwiggins/yaml_db.git
Run the dump task
rake db:dump
Step 2
Update your config/database.yml file.
Step 3 :
gem install mysql
Have rake create your database
rake db:create
rake db:schema:load
Step 4
Use YamlDb to reload your data into MySql
rake db:load
this is a duplicate
Migrate database from Postgres to MySQL
dont forget to change the gems and your database config file to something like this:
development:
adapter: mysql2
encoding: utf8
database: my_db_name
username: root
password: my_password
host: 127.0.0.1
port: 3306
Related
In my team, we are developing a system that is accessed by different platforms such as a Ruby on Rails website, a desktop java application and Android as well as iOS apps.
Our central MySQL database is running remotely on a server and can be accessed through PHPMyAdmin (and ControlPanel).
While the connection between the other platforms works well, I struggle with connecting my Rails app to the database. I would like to copy the database structure (tables, indexes, columns) so that I can access the database within Rails just as I would access data from a model that has been created locally (e.g. Customer.find(name: "Florian") ).
Is there a way to accomplish this? I tried several things such as altering my database.yml file, but when I then run something, like for example rails c it shows:
/Users/florianpfisterer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/mysql2-0.4.2/lib/mysql2.rb:31:in `require': dlopen(/Users/florianpfisterer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-15/2.2.0-static/mysql2-0.4.2/mysql2/mysql2.bundle, 9): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.18.dylib
Referenced from: /Users/florianpfisterer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-15/2.2.0-static/mysql2-0.4.2/mysql2/mysql2.bundle
Reason: image not found - /Users/florianpfisterer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-15/2.2.0-static/mysql2-0.4.2/mysql2/mysql2.bundle (LoadError)
...
My `database.yml' file:
development:
adapter: mysql2
encoding: utf8
host: <host-IP>
username: <username>
password: <password>
port: <port>
database: <database>
pool: 5
timeout: 5000
The same block is under test and production as well. In my Gemfile I have included:
gem 'mysql2'
I'm running Mac OS X 10.11.2 El Capitan and the server is a linux system. My Rails version is Rails 4.2.4 and Ruby ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin15].
Thank you!
EDIT:
Thank you all, finally fixed my database.yml file and running rake db:schema:dump did it. But how do I convert my schema.rb to locally usable ActiveRecord::Models ?
Use this in your Gemfile
gem 'mysql2', '~> 0.3.18'
config/database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
socket: /var/run/mysqld/mysqld.sock
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
insert into your .bashrc
export DATABASE_USER='root'
export DATABASE_PASSWORD='123'
Add library path to your .bash_profile file:
MYSQL=/usr/local/mysql/bin
export PATH=$PATH:$MYSQL
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
The problem not in connection but with loading mysql itself.
Trying to run the database in RoR i have this error
Couldn't create database for {"adapter"=>"sqlite3", "pool"=>5, "timeout"=>5000, "database"=>"db/test.sqlite3"}
rake aborted!
Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded. Add gem 'mysql2' to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
but when i do gem list i get that i have mysql2 (0.4.1)
How can i fix this? and also, why does this happens?
Check /config/database.yml file
Probably your file contains something like this:
development:
adapter: sqlite3
pool: 5
timeout: 5000
database: db/development.sqlite3
Change sqlite to mysql and add login settings, also check environment (development, production or test)
you need to run bundle install as you have already added the gem in the Gemflle.Also you need to setup mysql and other libs before installing it.
You should include the mysql2 gem into your gemfile and run 'bundle install'.
Also your config/database.yml should look something like the below
development:
adapter: mysql2
encoding: utf8
database: my_db_name
username: root
password: my_password
Here the username and password will be the one you gave at the time of configuring the mysql inyour system
We are currently using SQLite3 as our database and want to switch it to MySQL before we send our Rails app live via Passenger and Nginx to our Linode Ubuntu 10.04 Lucid box.
we set up the msql server on our linode ubuntu 10.04 box
and set a password for the root user. how do we configure database.yml ? what are the steps to set up this MySQL database? We have the mysql2 gem installed and in our Gemfile.
What does database.yml need to look like in order to deploy with MySQL ?
Does the database need any specific name as in database name?
Do we go about running rake db:create on our machine or run it on our Ubuntu box?
current database.yml
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 2000
# 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: 2000
production:
adapter: mysql2
encoding: utf8
database: production
pool: 5
username: root
password: "mysql password"
Did you try a simple connection test? If not, here is a script to test the connection to your db.
#! /usr/bin/ruby
# getting required gems
require 'rubygems'
require 'mysql'
## ----- test the connection -----
# http://rubydoc.info/gems/mysql/2.8.1/frames
# real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil)
mysql = Mysql.connect('localhost', 'root', 'password', 'db_name', 3006, '', '')
## ----- if connected, will list databases -----
puts mysql.list_dbs().to_s
In case the 'mysql' did not install properly, you will have to custom configure the install properties. On my Mac (different that you linux machine), I referenced this blog post to get something like this,
sudo env ARCHFLAGS="-arch x86_64" gem install --no-rdoc --no-ri mysql -- --with mysqlconfig=/usr/local/mysql/bin/mysql_config
UPDATE - Had to reinstall mysql gem for a different ruby version, and removing the architecture notation allowed me to install the gem.
sudo gem install --no-rdoc --no-ri mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
On your production server, go to your app's root directory and type nano config/database.yml and paste this code in. Change the names.
production:
adapter: mysql
database: something_production
username: somethingotherthanroot
password: passwordnostring
host: 127.0.0.1
Now, either create the database on your own, on the server, or you can run rake RAILS_ENV=production db:create:allhell i've never tried but i'm sure you can do rake db:create:production too.
once done, just do rake RAILS_ENV=production db:migrate
Looks like your database.yml is fine, except for the fact that you should have your production name database named like this: projectname_production
Answering your question of how to create the database once on the server, ssh into your application server and do a rake db:create RAILS_ENV=production
This will create the production database on the server. Same thing with your migration. It should be rake db:migrate RAILS_ENV=production. This will start the migration for your production database.
I really need help installing Sphinx and getting it going.
I'm on rails 3 and I'm following these set of directions: http://freelancing-god.github.com/ts/en/quickstart.html to install Sphinx.
Thinking Sphinx was easy to install since it's a gem. However, the guide says I need to have both Sphinx and Thinking Sphinx installed to get started (Is this true?). I checked Ryan Bates' railscast for Thinking Sphinx and he only installed the plugin and got started immediately.
Anyways, I installed Sphinx via MacPorts and here's proof:
...
---> Configuring mysql5
---> Building mysql5
---> Staging mysql5 into destroot
---> Installing mysql5 #5.1.57_0
The MySQL client has been installed.
...
---> Installing sphinx #0.9.9_0+mysql5
---> Activating sphinx #0.9.9_0+mysql5
---> Cleaning sphinx
It automatically configured Sphinx with mysql5. I have both mySQL and POSTgreSQL.
Now the next step was to rake thinking_sphinx:index after setting up an index in my user model which looks like this:
define_index do
indexes year
indexes major
indexes books_buy
indexes books_sell
indexes facebook
indexes restaurants
indexes interests
end
However, rake thinking_sphinx:index gave me this:
rake aborted!
Don't know how to build task 'thinking_sphnix:index'
and then
rake aborted!
no such file to load -- sqlite3
So I figured my database was configured on sqlite3 still. So I switched the gem to mySQL and edited my database.yml file to look like this:
# Switched over to mysql
# 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.
development:
adapter: mysql5
encoding: utf8
database: sphinx_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
test:
adapter: mysql5
encoding: utf8
database: sphinx_test
pool: 5
username: root
password:
socket: /tmp/mysql.sock
production:
adapter: mysql5
encoding: utf8
database: sphinx_production
pool: 5
username: root
password:
socket: /tmp/mysql.sock
and I got these errors:
rake aborted!
Please install the mysql5 adapter: `gem install activerecord-mysql5-adapter` (no such file to load -- active_record/connection_adapters/mysql5_adapter)
and
ERROR: Could not find a valid gem 'activerecord-mysql5-adapter' (>= 0) in any repository
ERROR: Possible alternatives: activerecord-jdbcmysql-adapter, activerecord-nulldb-adapter, activerecord-odbc-adapter, activerecord-jdbc-adapter, activerecord-postgis-adapter
Tony-Ngs-MacBook-Air:sample_app TonyNg$ gem install activerecord-nulldb-adapter
Any tips would be generous and helpful. Would also be willing to switch to PostgreSQL if help lead provided. Thanks!
You need to put either mysql or mysql2 as your adapter - and include either gem (same names as the adapters) in your Gemfile. I recommend mysql2, as it's actively maintained - but keep in mind if you're using Rails 3.0.x, then you must use a 0.2.x release of mysql2. If you're on Rails 3.1, then use mysql2 0.3.x.
I want to create my Rails application with MySQL, because I like it so much. How can I do that in the latest version of Rails instead of the default SQLite?
Normally, you would create a new Rails app using
rails ProjectName
To use MySQL, use
rails new ProjectName -d mysql
If you already have a rails project, change the adapter in the config/database.yml file to mysql and make sure you specify a valid username and password, and optionally, a socket:
development:
adapter: mysql2
database: db_name_dev
username: koploper
password:
host: localhost
socket: /tmp/mysql.sock
Next, make sure you edit your Gemfile to include the mysql2 or activerecord-jdbcmysql-adapter (if using jruby).
For Rails 3 you can use this command to create a new project using mysql:
$ rails new projectname -d mysql
Go to the terminal and write:
rails new <project_name> -d mysql
If you have not created your app yet, just go to cmd(for windows) or terminal(for linux/unix) and type the following command to create a rails application with mysql database:
$rails new <your_app_name> -d mysql
It works for anything above rails version 3. If you have already created your app, then you can do one of the 2 following things:
Create a another_name app with mysql database, go to cd another_name/config/ and copy the database.yml file from this new app. Paste it into the database.yml of your_app_name app. But ensure to change the database names and set username/password of your database accordingly in the database.yml file after doing so.
OR
Go to cd your_app_name/config/ and open database.yml. Rename as following:
development:
adapter: mysql2
database: db_name_name
username: root
password:
host: localhost
socket: /tmp/mysql.sock
Moreover, remove gem 'sqlite3' from your Gemfile and add the gem 'mysql2'
If you are using rails 3 or greater version
rails new your_project_name -d mysql
if you have earlier version
rails new -d mysql your_project_name
So before you create your project you need to find the rails version. that you can find by
rails -v
rails -d mysql ProjectName
rails new <project_name> -d mysql
OR
rails new projectname
Changes in config/database.yml
development:
adapter: mysql2
database: db_name_name
username: root
password:
host: localhost
socket: /tmp/mysql.sock
Create application with -d option
rails new AppName -d mysql
$ rails --help
is always your best friend
usage:
$ rails new APP_PATH[options]
also note that options should be given after the application name
rails and mysql
$ rails new project_name -d mysql
rails and postgresql
$ rails new project_name -d postgresql
You should use the switch -D instead of -d because it will generate two apps and mysql with no documentation folders.
rails -D mysql project_name (less than version 3)
rails new project_name -D mysql (version 3 and up)
Alternatively you just use the --database option.
In Rails 3, you could do
$rails new projectname --database=mysql
If you are creating a new rails application you can set the database using the -d switch like this:
rails -d mysql myapp
Its always easy to switch your database later though, and using sqlite really is easier if you are developing on a Mac.
Just go to rails console and type:
rails new YOURAPPNAME -d mysql
On new project, easy peasy:
rails new your_new_project_name -d mysql
On existing project, definitely trickier. This has given me a number of issues on existing rails projects. This kind of works with me:
# On Gemfile:
gem 'mysql2', '>= 0.3.18', '< 0.5' # copied from a new project for rails 5.1 :)
gem 'activerecord-mysql-adapter' # needed for mysql..
# On Dockerfile or on CLI:
sudo apt-get install -y mysql-client libmysqlclient-dev
First make sure that mysql gem is installed, if not? than type following command in your console
gem install mysql2
Than create new rails app and set mysql database as default database by typing following command in your console
rails new app-name -d mysql
Use following command to create new app for API with mysql database
rails new <appname> --api -d mysql
adapter: mysql2
encoding: utf8
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
database.yml
# MySQL. Versions 5.1.10 and up are supported.
#
# Install the MySQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html
#
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: localhost
database: database_name
username: username
password: secret
development:
<<: *default
# 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:
<<: *default
# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
# DATABASE_URL="mysql2://myuser:mypass#localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
production:
<<: *default
Gemfile:
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.4.4', '< 0.6.0'
you first should make sure that MySQL driver is on your system if not run this on your terminal if you are using Ubuntu or any Debian distro
sudo apt-get install mysql-client libmysqlclient-dev
and add this to your Gemfile
gem 'mysql2', '~> 0.3.16'
then run in your root directory of the project
bundle install
after that you can add the mysql config to config/database.yml as the previous answers