Ruby 操作 Mysql(2)

2021-10-19 16:08:40 浏览数 (1)

连接数据库

代码语言:javascript复制
[root@h102 mysql]# irb
2.3.0 :001 > require 'mysql2'
 => true 
2.3.0 :002 > client = Mysql2::Client.new(:host => "192.168.100.105", :username => "xxx", :password => "xxx")
 => #<Mysql2::Client:0x00000001ba9180 @read_timeout=nil, @query_options={:as=>:hash, :async=>false, :cast_booleans=>false, :symbolize_keys=>false, :database_timezone=>:local, :application_timezone=>nil, :cache_rows=>true, :connect_flags=>2147525125, :cast=>true, :default_file=>nil, :default_group=>nil, :host=>"192.168.100.105", :username=>"xxx", :password=>"xxx"}> 
2.3.0 :003 > client.class
 => Mysql2::Client 
2.3.0 :004 > 

查看连接是否可用

代码语言:javascript复制
2.3.0 :021 > client.ping
 => true 
2.3.0 :022 > 

查看客户端信息

代码语言:javascript复制
2.3.0 :022 > client.info
 => {:id=>50627, :version=>"5.6.27-76.0", :header_version=>"5.6.27-76.0"} 
2.3.0 :023 > 

查看服务端信息

代码语言:javascript复制
2.3.0 :023 > client.server_info
 => {:id=>50627, :version=>"5.6.27-76.0"} 
2.3.0 :024 > 

Tip: 可以使用的连接选项如下

代码语言:javascript复制
Mysql2::Client.new(
  :host,
  :username,
  :password,
  :port,
  :database,
  :socket = '/path/to/mysql.sock',
  :flags = REMEMBER_OPTIONS | LONG_PASSWORD | LONG_FLAG | TRANSACTIONS | PROTOCOL_41 | SECURE_CONNECTION | MULTI_STATEMENTS,
  :encoding = 'utf8',
  :read_timeout = seconds,
  :write_timeout = seconds,
  :connect_timeout = seconds,
  :reconnect = true/false,
  :local_infile = true/false,
  :secure_auth = true/false,
  :default_file = '/path/to/my.cfg',
  :default_group = 'my.cfg section',
  :init_command => sql
  )

在对安全要求更严格的环境下,可以使用 SSL 加密连接,前提是客户端和服务端都得编译对 SSL 的支持

代码语言:javascript复制
Mysql2::Client.new(
  # ...options as above...,
  :sslkey => '/path/to/client-key.pem',
  :sslcert => '/path/to/client-cert.pem',
  :sslca => '/path/to/ca-cert.pem',
  :sslcapath => '/path/to/cacerts',
  :sslcipher => 'DHE-RSA-AES256-SHA',
  :sslverify => true,
  )

创建数据库

代码语言:javascript复制
2.3.0 :024 > client.query("CREATE DATABASE `testxxx` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")
 => nil 
2.3.0 :025 >

在本地进行检查

代码语言:javascript复制
mysql> show databases;
 -------------------- 
| Database           |
 -------------------- 
| information_schema |
| Syslog             |
| mysql              |
| performance_schema |
| test               |
| testxxx            |
 -------------------- 
6 rows in set (0.03 sec)

mysql> show create database testxxx;
 ---------- ------------------------------------------------------------------------------------------------ 
| Database | Create Database                                                                                |
 ---------- ------------------------------------------------------------------------------------------------ 
| testxxx  | CREATE DATABASE `testxxx` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */ |
 ---------- ------------------------------------------------------------------------------------------------ 
1 row in set (0.00 sec)

mysql> 

0 人点赞