Ruby DBI: 连接到 MariaDB 数据库时出错

huangapple go评论69阅读模式
英文:

Ruby DBI: Error connecting to MariaDB database

问题

I see that you're encountering issues with your Ruby code while trying to connect to a MariaDB database. The error message suggests an issue with loading the MariaDB driver. This could be due to a compatibility or configuration problem.

To help you further, you can try the following steps:

  1. Ensure that you have the MariaDB driver for Ruby installed. You can typically install it using a command like:

    gem install mysql2
    
  2. Verify that the MariaDB gem version is compatible with your Ruby version.

  3. Double-check your database connection parameters, including the hostname, username, password, and database name.

  4. Ensure that you have the necessary MariaDB libraries installed on your system.

  5. Make sure your database server is up and running, as you mentioned that it's running well.

If you continue to face issues, it might be helpful to consult the documentation for the specific versions of Ruby, MariaDB, and the gems you are using, as there may be version-specific considerations or troubleshooting tips.

英文:

I wanted to follow the first steps of this guide (https://www.tutorialspoint.com/ruby/ruby_database_access.htm) and of course there was a problem.

This is what my code looks like:

#!/usr/bin/ruby -w

require "dbi"

begin
   # connect to the MySQL server
   dbh = DBI.connect("DBI:MariaDB:TESTDB:localhost", "testuser", "test123")
   # get server version string and display it
   row = dbh.select_one("SELECT VERSION()")
   puts "Server version: " + row[0]
rescue DBI::DatabaseError => e
   puts "An error occurred"
   puts "Error code:    #{e.err}"
   puts "Error message: #{e.errstr}"
ensure
   # disconnect from server
   dbh.disconnect if dbh
end

And this is what I receive

/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:74: warning: assigned but unused variable - meth
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:263: warning: assigned but unused variable - e2
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old initialize
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/utils/date.rb:42: warning: previous definition of initialize was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old initialize
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/utils/time.rb:16: warning: previous definition of initialize was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old initialize
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/utils/timestamp.rb:21: warning: previous definition of initialize was here
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:6: warning: assigned but unused variable - e
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old is_nullable?
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of is_nullable? was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old can_be_null?
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of can_be_null? was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old is_indexed?
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of is_indexed? was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old is_primary?
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of is_primary? was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old is_unique
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of is_unique was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old size
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of size was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old size=
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of size= was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old length
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of length was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old length=
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of length= was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old decimal_digits
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of decimal_digits was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old decimal_digits=
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of decimal_digits= was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old default_value
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of default_value was here
/var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old default_value=
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:83: warning: previous definition of default_value= was here
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/types.rb:110: warning: assigned but unused variable - civil
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/types.rb:111: warning: assigned but unused variable - time
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/handles.rb:12: warning: optional boolean argument is obsoleted
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/handles/database.rb:218: warning: parentheses after method name is interpreted as an argument list, not a decomposed argument
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/handles/database.rb:224: warning: parentheses after method name is interpreted as an argument list, not a decomposed argument
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/handles/statement.rb:307: warning: assigned but unused variable - cols
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/handles/statement.rb:324: warning: assigned but unused variable - cols
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/handles/statement.rb:358: warning: parentheses after method name is interpreted as an argument list, not a decomposed argument
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/handles/statement.rb:364: warning: parentheses after method name is interpreted as an argument list, not a decomposed argument
Traceback (most recent call last):
        5: from dziad.rb:7:in `<main>'
        4: from /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:145:in `connect'
        3: from /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:160:in `_get_full_driver'
        2: from /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:242:in `load_driver'
        1: from /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:242:in `synchronize'
/root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:300:in `block in load_driver': Unable to load driver 'MariaDB' (underlying error: uninitialized constant DBI::DBD::MariaDB) (DBI::InterfaceError)

I have been using the GPT chat to install MariaDB and verify the gem versions. However, despite spending a considerable amount of time on it, I'm unable to make the following code work.

My setup:
WSL version - 5.15.90.1-microsoft-standard-WSL2
Ruby version - ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-linux-gnu]
Debian version - 11.7
dbi (0.4.5, 0.4.0)
mysql2 (0.5.5)
My MariaDB after command sudo service mariadb status is running well.

答案1

得分: 1

  1. Gem dbi 于15年前被放弃。我不确定它是否与 Ruby 2.x 兼容。
  2. 使用 gem sequel 连接数据库。使用 gem mysql2 作为 Sequel 连接 MariaDB 数据库的驱动程序。
英文:
  1. Gem dbi is abondoned 15 years ago. I'm not sure it works with Ruby 2.x.
  2. Use gem sequel to connect a database. Use gem mysql2 as driver for Sequel to connect MariaDB database.
require 'bundler/inline'

gemfile(true) do
  source "https://rubygems.org"
  gem 'sequel'
  gem 'mysql2'
end

#                             username password         database
#                                ^        ^                 ^
DB = Sequel.connect('mysql2://testuser:test123@localhost/testdb')
row = DB.select(Sequel.lit('version()')).to_a
puts "Server version: #{row[0]}"

huangapple
  • 本文由 发表于 2023年7月6日 22:28:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629890.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定