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

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

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:

    1. 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:

  1. #!/usr/bin/ruby -w
  2. require "dbi"
  3. begin
  4. # connect to the MySQL server
  5. dbh = DBI.connect("DBI:MariaDB:TESTDB:localhost", "testuser", "test123")
  6. # get server version string and display it
  7. row = dbh.select_one("SELECT VERSION()")
  8. puts "Server version: " + row[0]
  9. rescue DBI::DatabaseError => e
  10. puts "An error occurred"
  11. puts "Error code: #{e.err}"
  12. puts "Error message: #{e.errstr}"
  13. ensure
  14. # disconnect from server
  15. dbh.disconnect if dbh
  16. end

And this is what I receive

  1. /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:74: warning: assigned but unused variable - meth
  2. /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:263: warning: assigned but unused variable - e2
  3. /var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old initialize
  4. /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
  5. /var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old initialize
  6. /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
  7. /var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old initialize
  8. /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
  9. /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/columninfo.rb:6: warning: assigned but unused variable - e
  10. /var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old is_nullable?
  11. /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
  12. /var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old can_be_null?
  13. /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
  14. /var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old is_indexed?
  15. /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
  16. /var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old is_primary?
  17. /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
  18. /var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old is_unique
  19. /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
  20. /var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old size
  21. /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
  22. /var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old size=
  23. /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
  24. /var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old length
  25. /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
  26. /var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old length=
  27. /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
  28. /var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old decimal_digits
  29. /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
  30. /var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old decimal_digits=
  31. /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
  32. /var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old default_value
  33. /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
  34. /var/lib/gems/2.7.0/gems/deprecated-2.0.1/lib/deprecated.rb:178: warning: method redefined; discarding old default_value=
  35. /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
  36. /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/types.rb:110: warning: assigned but unused variable - civil
  37. /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/types.rb:111: warning: assigned but unused variable - time
  38. /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi/handles.rb:12: warning: optional boolean argument is obsoleted
  39. /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
  40. /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
  41. /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
  42. /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
  43. /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
  44. /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
  45. Traceback (most recent call last):
  46. 5: from dziad.rb:7:in `<main>'
  47. 4: from /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:145:in `connect'
  48. 3: from /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:160:in `_get_full_driver'
  49. 2: from /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:242:in `load_driver'
  50. 1: from /root/.local/share/gem/ruby/2.7.0/gems/dbi-0.4.5/lib/dbi.rb:242:in `synchronize'
  51. /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.
  1. require 'bundler/inline'
  2. gemfile(true) do
  3. source "https://rubygems.org"
  4. gem 'sequel'
  5. gem 'mysql2'
  6. end
  7. # username password database
  8. # ^ ^ ^
  9. DB = Sequel.connect('mysql2://testuser:test123@localhost/testdb')
  10. row = DB.select(Sequel.lit('version()')).to_a
  11. 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:

确定