プログラム
フォルダー構造
$ tree -a
.
├── .env
├── config_mariadb.rb
└── server_version.rb
server_version.rb
#! /usr/bin/ruby
#
# server_version.rb
#
# Feb/24/2023
#
require 'mysql2'
#
load 'config_mariadb.rb'
# ------------------------------------------------------------
STDERR.puts "*** 開始 ***"
#
host,user,password,data_base = config_mariadb_proc()
client = Mysql2::Client.new(host: host, username: user, password: password, database: data_base)
#
sql_str="SELECT VERSION ()"
result=client.query(sql_str)
#
result.each do |row|
print "#{row["VERSION ()"]}\n"
end
client.close
#
STDERR.puts "*** 終了 ***"
# ------------------------------------------------------------
config_mariadb.rb
#
# config_mariadb.rb
#
# Feb/24/2023
#
require 'dotenv'
#
# ------------------------------------------------------------
def config_mariadb_proc()
Dotenv.load
host = ENV['host']
user = ENV['user']
password = ENV['password']
data_base = ENV['data_base']
#
return host,user,password,data_base
end
# ------------------------------------------------------------
.env
host = 'localhost'
user = 'scott'
password = 'tiger123'
data_base = 'city'
実行結果
$ ./server_version.rb
*** 開始 ***
10.11.2-MariaDB
*** 終了 ***