Search code examples
mysqlrubyrakemysql2

Remote connection to mysql2 using ruby


I m trying to connect to mysql2 that is installed in a VM1 from another VM2. vm2 connect to vm1 using ssh and the coonnection wroks. Howerver when i try to connect to mysql i get this error:

No connection. Check your internet connection.

my config.yaml file contains this:

# SQL connection
:adapter: mysql2
:database: project
:username: bochra
:password: 'password'
:host: ip_@_VM1

and a part of my Rakefile looks like this:

config = YAML.load(File.open('config.yaml'))
puts 'Clearing the database of all data.'
  begin
    client = Mysql2::Client.new(config)
    client.query('DROP TABLE IF EXISTS Employees')
    client.query('CREATE TABLE Employees(id INT, age INT, salary TEXT NOT NULL, PRIMARY KEY(id))')

  rescue
    ErrorHandler.new.show_error :no_connection
  end

Solution

  • The problem was that I have a default installation of MySQL on my default Ubuntu I haven't really changed any settings after install. In the default installation the bind address was set to 127.0.0.1 so i commented it. After that i got another errot it said: Mysql2::Error: Host 'xxxxxx' is not allowed to connect to this MySQL server. So from the server (VM1) i granted privelleges to my user (bochra) using this:

    CREATE USER 'bochra'@'%' IDENTIFIED BY 'root';
    GRANT ALL PRIVILEGES ON *.* TO 'bochra'@'%' WITH GRANT OPTION; and that worked fine for me