我正在学习ruby,并且尝试使用以下连接字符串打开与MySQL的连接:
require 'mysql'
begin
# Create new database connection.
db = Mysql.new('127.0.0.1','user','password!','myDb')
...
问题是我的数据库正在非本地端口上运行,如何在此连接字符串中指定一个端口,似乎只需要4个参数,如上所示...
有关信息,我的数据库正在docker容器中运行,其中3306映射到本地端口50101。
答案 0 :(得分:1)
如果您使用“标准” gem mysql,则可以查看文档there
#connect(host = nil, user = nil, passwd = nil, db = nil, port = nil, socket = nil, flag = 0) ⇒ Object (also: #real_connect)
也尝试使用mysql2
之类的gem,那里有许多文档和示例:
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
)