Skip to:

Opened 8 years ago

Closed 7 years ago

#3022 closed defect (bug) (fixed)

BBpress import from remote database hostname:port config

Reported by: webp's profile webp Owned by: johnjamesjacoby's profile johnjamesjacoby
Milestone: 2.6 Priority: normal
Severity: minor Version: 2.0
Component: API - Importers Keywords: commit


In contrary to, bbPress can import from remote database. But must use Hostname:Port in the converter admin, instead of hostname and port in separate input form fields.

I'm able to use a remote MySql server as import source. Since I don't want to spin up another mysql server vm, I have another mysql instance running on the current database server using a different port from the first mysql instance at port 3306. So I must pass in the db port of the previous forum (Drupal db). The following is my config in the bbPress converter admin at http://.../wp-admin/tools.php?page=bbp-converter :

#add_settings_field#      #Input#   = #Form lable
_bbp_converter_platform   Platform  = previous forum (Drupal6 for me)
_bbp_converter_db_server  Hostname:Port = Db Server IP or hostname
_bbp_converter_db_port    [NOT USED]= Db Port  Use default 3306 if unsure
_bbp_converter_db_name    xxxxxx    = Db Name  old forum db name
_bbp_converter_db_user    yyyyyy    = Db User
_bbp_converter_db_pass    ******    = Db Pass
_bbp_converter_db_prefix  Prefix    = Table Prefix  (blank for me)

Note that Db Port is NOT USED above, and must pass to hostname as Hostname:Port.
According to bbpress/includes/admin/converter.php:

abstract class BBP_Converter_Base {
  public function __construct() {
  private function setup_globals() {
    $this->wpdb     = bbp_db();
    $this->max_rows = (int) $_POST['_bbp_converter_rows'];
    $this->opdb     = new wpdb( $_POST['_bbp_converter_db_user'],
                                $_POST['_bbp_converter_db_server'] );
    $this->opdb->prefix = $_POST['_bbp_converter_db_prefix'];

where, new wpdb constructor can take db_name as 'hostname:port'. See source of wp-includes/wp-db.php:

class wpdb {
    public function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {

    public function db_connect( $allow_bail = true ) {
        if ( $this->use_mysqli ) {
            $this->dbh = mysqli_init();
            // mysqli_real_connect doesn't support the host param including a port or socket
            // like mysql_connect does. This duplicates how mysql_connect detects a port and/or socket file.
            $port = null;
            $socket = null;
            $host = $this->dbhost;
            $port_or_socket = strstr( $host, ':' );
            if ( ! empty( $port_or_socket ) ) {
                $host = substr( $host, 0, strpos( $host, ':' ) );
                $port_or_socket = substr( $port_or_socket, 1 );
                if ( 0 !== strpos( $port_or_socket, '/' ) ) {
                    $port = intval( $port_or_socket );
                    $maybe_socket = strstr( $port_or_socket, ':' );
                    if ( ! empty( $maybe_socket ) ) {
                        $socket = substr( $maybe_socket, 1 );
                } else {
                    $socket = $port_or_socket;
            if ( WP_DEBUG ) {
                mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
            } else {
                @mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );

so wpdb class parses out hostname:port before passing hostname and port to:

Object oriented style
bool mysqli::real_connect ([ string $host [, string $username [, string $passwd [, string $dbname [, int $port [, string $socket [, int $flags ]]]]]]] )
Procedural style
bool mysqli_real_connect ( mysqli $link [, string $host [, string $username [, string $passwd [, string $dbname [, int $port [, string $socket [, int $flags ]]]]]]] )

However, I might be missing something, but bbpress/includes/admin/converter.php doesn't use _bbp_converter_db_port at all. Whatever users input port doesn't get passed to new wpdb.

So suggest to:

1) eliminate db port from the form and instruct users to use 'hostname:port', or
2) add db port to new wpdb in converter.php:

    $this->opdb     = new wpdb( $_POST['_bbp_converter_db_user'],
                                $_POST['_bbp_converter_db_server'] .':'.
                                $_POST['_bbp_converter_db_port'] );

Change History (3)

#1 @webp
8 years ago

FYI: More discussion regarding Using wpdb to connect to a separate database:

#2 @johnjamesjacoby
7 years ago

  • Keywords commit added
  • Milestone changed from Awaiting Review to 2.6
  • Owner set to johnjamesjacoby
  • Version changed from 2.5.11 to 2.0

Sorry about that. I'm able to duplicate this, and with recent improvements to the converter, I'll be able to fix this quickly.

#3 @johnjamesjacoby
7 years ago

  • Resolution set to fixed
  • Status changed from new to closed

In 6490:

Converter: Use the port number in the server address if available.

Fixes #3022.

Note: See TracTickets for help on using tickets.