mysql - 使用ruby远程连接到mysql2

标签 mysql ruby rake mysql2

我正在尝试从另一个 VM2 连接到安装在 VM1 中的 mysql2。 vm2 使用 ssh 连接到 vm1 并且连接工作正常。 但是,当我尝试连接到 mysql 时,出现此错误:

No connection. Check your internet connection.

我的 config.yaml 文件包含这个:

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

我的 Rakefile 的一部分看起来像这样:

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

最佳答案

问题是我在我的默认 Ubuntu 上默认安装了 MySQL 我在安装后并没有真正更改任何设置。 在默认安装中,绑定(bind)地址设置为 127.0.0.1,因此我对其进行了注释。在那之后我得到另一个错误它说: Mysql2::Error: Host 'xxxxxx' is not allowed to connect to this MySQL server.所以从服务器(VM1)我使用这个授予了我的用户(bochra)权限:

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

关于mysql - 使用ruby远程连接到mysql2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44102430/

相关文章:

php - 将大数据从txt插入到mysql

ruby - 引发异常时如何中止 Ruby 脚本?

ruby - rake --tasks 完整描述(未删减)

ruby-on-rails - Rake 中的 'environment' 任务是什么?

java - 在java中显示数据库的表

javascript - Node.js Knex SQL 插入太慢

ruby-on-rails - 如何使用 if 在 ruby​​ 中构造 where 子句

ruby - 上传 MP3 文件 - UTF-8 字节序列无效

ruby-on-rails-3 - Rake db :seed working on local, heroku rake db:seed 试图将空值插入 id 列

php - 如何在Count()中指定条件?