macOS系统ruby连接mysql

安装

ruby中连接mysql推荐使用mysql2 这个库, 这个库依赖系统中的mysql-client, 所以需要先安装mysql-client

macOS使用brew直接安装即可

1
brew install mysql-client

安装完成后,控制台会输出mysql-client 的安装目录, 记录下这个目录, 安装mysql2 的时候需要使用, 我这里是/opt/homebrew/opt/mysql-client/

安装mysql2:

1
sudo gem install mysql2 -- --with-mysql-dir=/opt/homebrew/opt/mysql-client/

如果使用的不是mac系统自带ruby, 则安装Gem的时候,需要添加 --user-install 选项

1
gem install mysql2  --user-install -- --with-mysql-dir=/opt/homebrew/opt/mysql-client/

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/ruby -w
require 'mysql2'

client = Mysql2::Client.new(
:host => '127.0.0.1', # 主机
:username => 'root', # 用户名
:password => '123456', # 密码
:database => 'test', # 数据库
:encoding => 'utf8' # 编码
)
results = client.query("SELECT VERSION()")
results.each do |row|
puts row
end

错误

‘OpenSSH keys only supported if ED25519 is available’ 重启电脑后, 有时会出现此错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[18:08:04.625] ERROR: sql executor[Run Script] Ignoring debase-2.3.2 because its extensions are not built. Try: gem pristine debase --version 2.3.2
/Library/Ruby/Gems/2.6.0/gems/net-ssh-6.1.0/lib/net/ssh/authentication/ed25519_loader.rb:21:in `raiseUnlessLoaded': OpenSSH keys only supported if ED25519 is available (NotImplementedError)
net-ssh requires the following gems for ed25519 support:
* ed25519 (>= 1.2, < 2.0)
* bcrypt_pbkdf (>= 1.0, < 2.0)
See https://github.com/net-ssh/net-ssh/issues/565 for more information
Gem::MissingSpecError : "Could not find 'bcrypt_pbkdf' (~> 1.0) among 56 total gem(s)
Checked in 'GEM_PATH=/Users/huazhizui/.gem/ruby/2.6.0:/Library/Ruby/Gems/2.6.0:/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/gems/2.6.0', execute `gem env` for more information"
from /Library/Ruby/Gems/2.6.0/gems/net-ssh-6.1.0/lib/net/ssh/key_factory.rb:200:in `classify_key'
from /Library/Ruby/Gems/2.6.0/gems/net-ssh-6.1.0/lib/net/ssh/key_factory.rb:51:in `load_data_private_key'
from /Library/Ruby/Gems/2.6.0/gems/net-ssh-6.1.0/lib/net/ssh/key_factory.rb:42:in `load_private_key'
from /Library/Ruby/Gems/2.6.0/gems/net-ssh-6.1.0/lib/net/ssh/authentication/key_manager.rb:167:in `sign'
from /Library/Ruby/Gems/2.6.0/gems/net-ssh-6.1.0/lib/net/ssh/authentication/methods/publickey.rb:62:in `authenticate_with'
from /Library/Ruby/Gems/2.6.0/gems/net-ssh-6.1.0/lib/net/ssh/authentication/methods/publickey.rb:20:in `block in authenticate'
from /Library/Ruby/Gems/2.6.0/gems/net-ssh-6.1.0/lib/net/ssh/authentication/key_manager.rb:132:in `block in each_identity'
from /Library/Ruby/Gems/2.6.0/gems/net-ssh-6.1.0/lib/net/ssh/authentication/key_manager.rb:129:in `each'
from /Library/Ruby/Gems/2.6.0/gems/net-ssh-6.1.0/lib/net/ssh/authentication/key_manager.rb:129:in `each_identity'
from /Library/Ruby/Gems/2.6.0/gems/net-ssh-6.1.0/lib/net/ssh/authentication/methods/publickey.rb:19:in `authenticate'
from /Library/Ruby/Gems/2.6.0/gems/net-ssh-6.1.0/lib/net/ssh/authentication/session.rb:86:in `block in authenticate'
from /Library/Ruby/Gems/2.6.0/gems/net-ssh-6.1.0/lib/net/ssh/authentication/session.rb:72:in `each'
from /Library/Ruby/Gems/2.6.0/gems/net-ssh-6.1.0/lib/net/ssh/authentication/session.rb:72:in `authenticate'
from /Library/Ruby/Gems/2.6.0/gems/net-ssh-6.1.0/lib/net/ssh.rb:255:in `start'
from /Library/Ruby/Gems/2.6.0/gems/net-ssh-gateway-2.0.0/lib/net/ssh/gateway.rb:57:in `initialize'
from -e:21:in `new'
from -e:21:in `<main>'

解决办法:

简单的运行如下命令就好了:

1
ssh-add