I am trying to connect to a different Database (php_db) from a rails app.
currently App has mysql Database(ror_db). Both Databases are on the same server.
I have some requirement ,where i have to insert some records from rails app to php_db.
I have used below code to do that.
Not sure how far it is secure.
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "xxxxxx",
:password => "xxxxxx",
:database => "php_db"
)
sql_update = "INSERT INTO cxv_journal(trans_no, reference,tran_date,event_date,doc_date) VALUES (2,2,'xxxx-xx-xx','xxxx-xx-xx','xxxx-xx-xx')"
ActiveRecord::Base.connection.execute(sql_update)
ActiveRecord::Base.remove_connection
config =YAML.load_file(File.join(Rails.root,"config","database.yml"))
ActiveRecord::Base.establish_connection(config['production'])
Appreciate any suggestion for more optimized solution.
Thanks in advance.
Related
I'm creating a website using 3 principle files.
One ruby file that I use to catch some data from the internet. (Nokogiri)
One database to save these data.
One php file, the principle page of the website, to display these data .
For the moment I have these 3 files working but separately. Now I'm trying with MAMP (I'm on MAC) to make this 3 files work together.
First I'm trying so save data in my MAMP database using my ruby code. I red that active record was the best way to do it, but the following code isn't working, can you help?
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "localhost",
:username => "root",
:password => "root",
:database => "my_database"
)
Base.connection.insert("INSERT INTO my_database(fields) VALUES('value')")
Try this:
connection = ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "localhost",
:username => "root",
:password => "root",
:database => "my_database"
).connection
connection.exec_query("INSERT INTO my_database(fields) VALUES('value')")
I'm trying to connect to a SQL server in a domain, but I don't know why I have an error.
My environnement is Ubuntu 14.04, and I use mysql2.
I think my problem is :server="myServer" but I have to specify what server.
My request is :
db = Mysql2::Client.new(:host => "xxx.xxx.xxx.xxx",:server="myServer" ,:username => "myname", :password => "SECRET", :port => 3306, :database => "test" )
which gives me this error: Mysql2::error (111)
This request is working:
db = Mysql2::Client.new(:host => "192.168.1.1", :username => "root", :password => "SECRET", :port => 3306, :database => "test" )
Take a look at this:
Can't connect to MySQL server error 111
Probably you are facing the same issue.
Can I check if a mysql connection can be established with the given credentials from the controller?(i.e. host, username, pwd etc.)
Please note that I am not talking about setting my rails database.
I have a create form which returns some database credentials and I need to check if such connection exists before storing it in my database.
Thanks in advance
You could use the mysql2 module, then trying to open a new connection:
require 'mysql2'
conn = Mysql2::Client.new(:host => 'localhost', :username => 'user', :password => 'password', :port => '1111', :database => 'database_name')
Read more documentation here: https://rubygems.org/gems/mysql2
I'm dealing with this a few days, and cannot connect to a simple mysql database on localhost.
require "mysql"
#db_host = "localhost"
#db_user = "myrubyapp"
#db_pass = "1234"
#db_name = "myrubyapp"
mysql = Mysql.new(:host => #db_host, :username => #db_user, :password => #db_pass, :database => #db_name)
The output I got is an error: can't convert Hash into String (TypeError) where Mysql.new is.
The second one, I tried to change the gem to mysql2
require "mysql2"
#db_host = "localhost"
#db_user = "myrubyapp"
#db_pass = "1234"
#db_name = "myrubyapp"
mysql = Mysql2.new(:host => #db_host, :username => #db_user, :password => #db_pass, :database => #db_name)
The output is an error too, but is different from the first: undefined method "new" for Mysql2:Module (NoMethodError).
Guys I'm sorry that I have to ask this kind of questions, but I'm really really confused, I have an experience of programming more than 3 years in JavaSE and EE, I am ashamed cause I can't deal with that. Please point me into right direction and don't judge me harshly. I am new to Ruby.
Try
mysql = Mysql2::Client.new(:host => #db_host, :username => #db_user, :password => #db_pass, :database => #db_name)
correct syntax is:
client = Mysql2::Client.new(:host => "localhost", :username => "root")
see mysql2 on github for more examples
I recomment you to take a look at Sequel as raw mysql2 lib provide a very limited functionality and Sequel can do a lot.
Never used the gem mysql, but have you tried to remove the hash :host => etc and pass directly a list of parameters?
Something like con = Mysql.new db_host, db_user, db_pass, db_name
http://zetcode.com/db/mysqlrubytutorial/ for a tutorial
I have a Rails 3.2 app which use PostgreSQL to store all the information.
But in one Rake task I need to make a connection with the MySQL server. I tried to do this:
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:database => "foo",
:user => "root",
:password => "",
)
But it just replace my default PostgreSQL connection with this temporary MySQL.
How to make the additional connection for the instance?
I found a very simple solution: to the the vanila mysql2 gem (https://github.com/brianmario/mysql2)
Now my code looks like:
client = Mysql2::Client.new(:host => "localhost", :username => "root", :database => "foobar", :password => "")
users = client.query("SELECT * FROM users")
After that I have an array of results.
Don't establish it on ActiveRecord::Base.
establish_connection connects to a database from a class, as you've discovered, so when you do it on AR:Base, every subclass of that (to whit, the entire database) has the connection established on it, replacing the current one.
Basically, you create a class for each of the tables you want to connect to, and call the establish connection method in those. If you want to do it in several tables, then create a module with it in and include it.
class MyCustomClass < ActiveRecord::Base
establish_connection(
:adapter => "mysql2",
:database => "foo",
:user => "root",
:password => "",
)
end
MyCustomClass.find(1)