# File lib/tramp/evented_mysql.rb, line 199
  def self._connect opts
    opts = settings.merge(opts)

    conn = Mysql.init

    # set encoding _before_ connecting
    if charset = opts[:charset] || opts[:encoding]
      conn.options(Mysql::SET_CHARSET_NAME, charset)
    end

    conn.options(Mysql::OPT_LOCAL_INFILE, 'client')

    conn.real_connect(
      opts[:host] || 'localhost',
      opts[:user] || opts[:username] || 'root',
      opts[:password],
      opts[:database],
      opts[:port],
      opts[:socket],
      0 +
      # XXX multi results require multiple callbacks to parse
      # Mysql::CLIENT_MULTI_RESULTS +
      # Mysql::CLIENT_MULTI_STATEMENTS +
      (opts[:compress] == false ? 0 : Mysql::CLIENT_COMPRESS)
    )
    
    # increase timeout so mysql server doesn't disconnect us
    # this is especially bad if we're disconnected while EM.attach is
    # still in progress, because by the time it gets to EM, the FD is
    # no longer valid, and it throws a c++ 'bad file descriptor' error
    # (do not use a timeout of -1 for unlimited, it does not work on mysqld > 5.0.60)
    conn.query("set @@wait_timeout = #{opts[:timeout] || 2592000}")

    # we handle reconnecting (and reattaching the new fd to EM)
    conn.reconnect = false

    # By default, MySQL 'where id is null' selects the last inserted id
    # Turn this off. http://dev.rubyonrails.org/ticket/6778
    conn.query("set SQL_AUTO_IS_NULL=0")

    # get results for queries
    conn.query_with_result = true

    conn
  rescue Mysql::Error => e
    if cb = opts[:on_error]
      cb.call(e)
      nil
    else
      raise e
    end
  end