3

I am trying to access remote mysql database using rake task. Here i am posting my active record connection logic

client = ActiveRecord::Base.establish_connection(
:host => "server_ip",
:port => 22,
:adapter  => "mysql2",
:pool => 5,
:database => "database_name",
:username => "username",
:password => "password"
)

I am accessing it using this

client.connection.execute("SELECT * FROM users")

But every time it results with error as given below

rake aborted!
Lost connection to MySQL server at 'reading initial communication packet', system error: 0

Is there anything i am missing. Please suggest me right direction

Jeet
  • 1,350
  • 1
  • 15
  • 32

1 Answers1

1

Try the following,

Make sure MySQL connects from console with the same credentials

Make sure it connects in irb/rails console

If not, Add port number in the config you mentioned. MySQL should run in that port.

Check the access privilege for the username and IP in MySQL

Jon
  • 2,703
  • 3
  • 18
  • 14
  • It gives some active record object like # – Jeet Dec 27 '13 at 13:20
  • access privilege are perfect as through browser i am able to access remote database – Jeet Dec 27 '13 at 13:28
  • Adding port number in this it gives me error like this "Mysql2::Error: Lost connection to MySQL server at 'reading initial communication packet', system error: 0" – Jeet Dec 27 '13 at 13:31
  • Check the host.allow and host.deny http://stackoverflow.com/questions/961928/could-not-connect-lost-connection-to-mysql-server-at-reading-initial-communica – Jon Jan 02 '14 at 09:32