This is a follow up question to "How to create a ssh tunnel in ruby and then connect to mysql server on the remote host" from 5 years ago.
I'm trying to create an SSH tunnel in Ruby and then connect to a remote MySQL database. I'm doing this because I need to move some legacy data from an older version of my app, so I don't need ActiveRecord, migrations, etc.
I'm using the NetSSHGateway gem and the MySQL2 gem.
Here's my Ruby code (run in rails console):
gateway = Net::SSH::Gateway.new('old_remote_server.com','server_username')
port = gateway.open('127.0.0.1', 3306, 3307)
client = Mysql2::Client.new(
host: "127.0.0.1",
username: 'database_username',
password: 'database_password',
database: 'database_name',
port: port
)
After the last line, the console hangs for about 2 minutes, and then gives me the error:
Mysql2::Error: Lost connection to MySQL server at 'reading initial communication packet', system error: 0
I am able to SSH into the remote server, and execute MySQL commands that way, so I'm not sure what the issue is here.
After some thought, this isn't likely the best way to migrate data, for a few reasons.
In the end, I simply dumped my legacy MySQL database, moved it to my new app's environment, and used the MySQL2 gem to build rake tasks to translate and move the data into my new app.
Related
I am having some difficulties for the past few days while trying to connect to a remote MYSQL DB from a Heroku hosted Rails App.
Locally I am able to connect to the remote database and retrieve/modify data.
My database.yml file:
remote:
adapter: mysql2
host: host.address
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
database: databasename
username: username
password: password
port: 3306
Model that I try to use this remote database for:
class Test < ApplicationRecord
establish_connection(:remote)
self.table_name = "tests"
end
The mysql database is hosted on server with Cpanel which has remote database service, where I configure to be accessible from every ip (0.0.0.0), (%), (www.mywebsite.com).
When I try connecting to the database with e.g.: running Test.new via heroku rails console, I get the following:
app/vendor/bundle/ruby/3.0.0/gems/activerecord-7.0.2.3/lib/active_record/connection_adapters/mysql2_adapter.rb:51:in `rescue in new_client': There is an issue connecting with your hostname: my.host.ip. (ActiveRecord::DatabaseConnectionError)
Please check your database configuration and ensure there is a valid connection to your database.
/app/vendor/bundle/ruby/3.0.0/gems/mysql2-0.5.4/lib/mysql2/client.rb:95:in `connect': Can't connect to MySQL server on 'my.host.ip:3306' (110) (Mysql2::Error::ConnectionError)
This is my secondary Database. My production one is postgresql used with the postgresql AddOn on heroku..
All the ENV variables I use in heroku are for the username, pass, database name, host.
I have read all the questions here regarding this and tried everything. Not sure what I am missing.
Thanks for any advice.
We previously had a successful connection from Rails to an AWS MySQL instance through ActiveRecord, but as of our latest push to Heroku, that connection was severed. We don't understand why, as we have all traffic enabled on the AWS RDS settings, and the credentials in our database.yml config file have not changed. The only big thing that changed in the push to Heroku was adding the dovenv gem and passing control over environment variables to that gem.
When booting up our rails server in any of our three environments (dev, test, production), we receive a NoDatabaseError from ActiveRecord:
ActiveRecord::NoDatabaseError - Unknown database '<database_name>.us-east-1.rds.amazonaws.com'
We can successfully connect to the database through MySQL CLI.
database.yml:
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: "<username>"
password: "<password>"
host: <database_name>.us-east-1.rds.amazonaws.com
port: <port>
development:
<<: *default
database: test
test:
<<: *default
database: test
production:
<<: *default
Relevant local dependencies:
activerecord (5.1.4)
mysql2 (0.4.10)
rails (5.1.4)
AWS security groups:
default (<group_id>) CIDR/IP - Inbound 0.0.0.0/0
default (<group_id>) CIDR/IP - Outbound 0.0.0.0/0
Publicly accessible: Yes
What we have tried:
Restarting MySQL server: $ brew services list $ brew services restart mysql
Viewing MySQL logs on AWS, which reveals the error: [Warning] IP address '<ip_address>' could not be resolved: Name or service not known
Testing in production, which when attempting to connect to the database, gives the error: ActiveRecord::StatementInvalid: Mysql2::Error: No database selected
Running rake db:create RAILS_ENV=development, which gives the error:
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.`<possibly_private_id>`.`us-east-1`.`rds`.`amazonaws`.`com` DEFAULT CHARACTER SET `utf8`' at line 1
Uninstalling mysql, /usr/local/var/mysql, and reinstalling mysql (through brew)
Checking ENV variables, of which there are none that relate to MySQL.
Confirmed that my mysql server is running by running mysql.server status
I have a theory that when installing the gem dotenv-rails, we may have lost some MySQL credentials that granted access to the server from our project's local directory... Again, we still can connect to the server through MySQL CLI so we know everything on AWS is fine and healthy. I also can't seem to log in to mysql as my local username, or root. Currently trying to figure that issue out.
Solved - turns out it was related to the modifications I made in my environment variable setup. In this section of the Rails Guides, it notes that specifically declaring "DATABASE_URL" as an environment variable will automatically set or override the database variable declared in your config/database.yml file.
In my case, the host variable (set in config/database.yml) was correctly set to MY_DATABASE_NAME.amazon.aws, but DATABASE_URL variable in my environment was silently setting the config file's database variable to the same MY_DATABASE_NAME.amazon.aws.
I do not have a database called 'MY_DATABASE_NAME.amazon.aws', which is why it was freaking out. Hope this helps someone else!
My Ruby app connects to 2 databases in production: a PG database on the heroku server(successful) and a remote mysql database (unsuccessful).
The database URLs are stored in environment variables (in application.yml locally, and in config vars on Heroku).
The app is successfully retrieving data from both of these databases in development mode, so I know the URLs and credentials are valid.
On Heroku, only the pg connection is successful;
the MySQL connection throws a generic error: Mysql2::Error (Can't connect to MySQL server on 'villageprint.mypresswise.com' (110)):.
So both URLs are definitely being read from the config vars but the MYSQL connection is still unsuccessful.
Is there any reason the MySQL database might not accept the connection from Heroku? Or is it more likely that the connection is failing due to some application error? How can I debug this?
Im currently working with ruby on rails, which is an area that I'm entirely new to. I'm using MAMP and have set up a local MySQL database called my_db with a table called employees https://localhost/phpmyadmin.
Is there a way to connect to this database using ruby on rails and write to this table in the database. I have tried many examples but all of them show you how to connect to a mySQL db installed natively. I can't seem to work out how to connect to a database that lives in MAMP. Any suggestions?
Rails connects to whatever's defined in config/database.yml, so you should be able to connect in much the same way any PHP code does:
development:
driver: mysql2
host: localhost
username: my_username
password: my_password
database: my_database_name
Generally you connect over host with an optional port parameter.
I have 2 ubuntu 11.04 servers on IPs 192.168.9.14 and 192.168.9.15. On .14 I have installed ruby + rails and on .15 I have installed mysql and the database for my site resides on .15
I now want to get rails 2.3.8 to connect from .14 to the database on .15. I have the following in my database.yml on .14.
production:
adapter: mysql
encoding: utf8
reconnect: false
database: gtt_production
pool: 5
username: root
password: admin
host: 192.168.9.15
port: 3306
I'm currently getting an error showing up in my rails log as follows:
Can't connect to MySQL server on 192.168.9.15.
I originally thought this was due to user privileges on the mysql server but I have now added that in and still getting the same problem.
So I then tried connecting via a terminal (command line) as follows:
mysql -h 192.168.9.15 -uroot -p
and I got a message saying :
The program 'mysql' can be found in the following packages:
* mysql-client-cor-5.1
Try: sudo apt-get install <selected package>
...And that's what makes me think I need a local copy of mysql even though the database completely resides on another server.
Please can someone confirm whether my 'guess' is correct or whether there is some 'clever' way of connecting to the mysql server on .15 without also installing mysql on .14.
you need the client - sudo apt-get install mysql The server is usually mysql-server
Did you install the mysql gem for ruby? Rails will need this in order to connect to the DB. Also make sure .15 can accept remote requests from .14.
Have you configured your mysql db to listen to external IP traffic? I dont think MySQL by default allows external connections, only localhost traffic. Check your my.cnf.