2

I have to connect to mysql database specifing the protocol as in this command line:

mysql -P port_number -u user -ppassword --protocl=tcp -D database -h hostname

Well, port number I think is possible to pass adding ":port_number" to the "hostname" in tag. User name, database and password have their own tag as in the next example:

<connection>
    <host><![CDATA[hostname:port_number]]></host>
    <username><![CDATA[user]]></username>
    <password><![CDATA[password]]></password>
    <dbname><![CDATA[pierpape_reciele]]></dbname>
    <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
    <model><![CDATA[mysql4]]></model>
    <type><![CDATA[pdo_mysql]]></type>
    <pdoType><![CDATA[]]></pdoType>
    <active>1</active>
</connection>

What about some other parameter like "protocol"?

1

Adding

<protocol><![CDATA[tcp]]></protocol>

to the <connection> element should work, because in the end, any additional elements gets added to the PDO dsn string, as you can see in Zend_Db_Adapter_Pdo_Abstract::_dsn():

/**
 * Creates a PDO DSN for the adapter from $this->_config settings.
 *
 * @return string
 */
protected function _dsn()
{
    // baseline of DSN parts
    $dsn = $this->_config;

    // don't pass the username, password, charset, persistent and driver_options in the DSN
    unset($dsn['username']);
    unset($dsn['password']);
    unset($dsn['options']);
    unset($dsn['charset']);
    unset($dsn['persistent']);
    unset($dsn['driver_options']);

    // use all remaining parts in the DSN
    foreach ($dsn as $key => $val) {
        $dsn[$key] = "$key=$val";
    }

    return $this->_pdoType . ':' . implode(';', $dsn);
}

It's using a blacklist, to not add configurations that are passed differently but it doesn't restrict any other parameters. So the configuration will result in this dsn, which should work for you:

mysql:protocol=tcp;host=hostname:portnumber;dbname=pierpape_reciele
0

Eventually Magento calls Varien_Db_Adapter_Mysqli->_connect(), here's the code from CE 1.90.

protected function _connect()
{
    if ($this->_connection) {
        return;
    }
    if (!extension_loaded('mysqli')) {
        throw new Zend_Db_Adapter_Exception('mysqli extension is not installed');
    }
    // Suppress connection warnings here.
    // Throw an exception instead.
    @$conn = new mysqli();
    if (false===$conn || mysqli_connect_errno()) {
        throw new Zend_Db_Adapter_Mysqli_Exception(mysqli_connect_errno());
    }

    $conn->init();
    $conn->options(MYSQLI_OPT_LOCAL_INFILE, true);
    #$conn->options(MYSQLI_CLIENT_MULTI_QUERIES, true);

    $port = !empty($this->_config['port']) ? $this->_config['port'] : null;
    $socket = !empty($this->_config['unix_socket']) ? $this->_config['unix_socket'] : null;
    // socket specified in host config
    if (strpos($this->_config['host'], '/')!==false) {
        $socket = $this->_config['host'];
        $this->_config['host'] = null;
    } elseif (strpos($this->_config['host'], ':')!==false) {
        list($this->_config['host'], $port) = explode(':', $this->_config['host']);
    }

    #echo "<pre>".print_r($this->_config,1)."</pre>"; die;
    @$conn->real_connect(
        $this->_config['host'],
        $this->_config['username'],
        $this->_config['password'],
        $this->_config['dbname'],
        $port,
        $socket
    );
    if (mysqli_connect_errno()) {
        throw new Zend_Db_Adapter_Mysqli_Exception(mysqli_connect_error());
    }

    $this->_connection = $conn;

    /** @link http://bugs.mysql.com/bug.php?id=18551 */
    $this->_connection->query("SET SQL_MODE=''");
}

You can see here the config values that get passed through to mysqli_real_connect, the documentation for mysqli_real_connect says http://php.net/manual/en/mysqli.real-connect.php#socket

socket Specifies the socket or named pipe that should be used.

Note: Specifying the socket parameter will not explicitly determine the type of connection to be used when connecting to the MySQL server. How the connection is made to the MySQL database is determined by the host parameter.

and

host Can be either a host name or an IP address. Passing the NULL value or the string "localhost" to this parameter, the local host is assumed. When possible, pipes will be used instead of the TCP/IP protocol.

So if you want to use named pipes or a socket, you set the host to localhost and put the name in socket.

  • Thanks for your reply, but what about protocol? Ok socket, ok localhost, port etc. but in practice how can I pass the protocol option? – papesky Nov 5 '14 at 10:14
  • You can't set the protocol using php_mysqli. It's derived from the other parameters. See the documentation for mysql_real_connect I copied above. – Richard Nov 5 '14 at 10:23
  • OP has configured <type><![CDATA[pdo_mysql]]></type> (not mysqli), so Varien_Db_Adapter_Mysqli is not involved. – Fabian Schmengler Feb 20 '15 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.