简体   繁体   中英

Specify port in mysql ruby connection

I'm learning ruby, and I'm trying to open a connection to MySQL using the following connection string:

    require 'mysql'

    begin
      # Create new database connection.
      db = Mysql.new('127.0.0.1','user','password!','myDb')
...

The issue is my database is running on a non-native port, how do a specify a port in this connection string, it seems only to take 4 parameters as show above...

For info, my database is running in a docker container with 3306 mapped to local port 50101.

If you use "standard" gem mysql you can check documentation there

#connect(host = nil, user = nil, passwd = nil, db = nil, port = nil, socket = nil, flag = 0) ⇒ Object (also: #real_connect) 

Also try to use gem like mysql2 there lot of documentation and examples:

    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
  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM