PDO not working with Port

I am trying to connect to an external database using PDO.

$dbh = new PDO('mysql:host=hotsname;port=3309;dbname=dbname', 'root', 'root');

However this is not working on one particular server I have.

I thought maybe only a certain host was allowed, but I checked the mysql rules and also tried this code on a server I have personally and the connection worked.

So I know the code works and that their is no block on my side and that the firewall is accepting requests to port 3309 and passing it to the correct server on port 3306.

So the issue is solely on one server. As a test I thought I’d open port 3306 to test the code without specifying a port.

$dbh = new PDO('mysql:host=hotsname;dbname=dbname', 'root', 'root');

This worked straight away.

So my question is, why when adding the port does this cause it not to be able to connect to the database. It is only this particular server that has this issue.

Many Thanks for your time.

Update

The error given is simply “Cannot connect to mysql server on xxx.xxx.xxx.xxx”.

I have gotten slightly further now. The code is now working providing I have a firewall rule to allow incoming on 3306 to the server using 3306. So even though I have specified a port, it seems to ignore this and force it to go out of 3306.

6

You probably using localhost as a hostname. Switching it into 127.0.0.1 instead should solve the port ignorance problem.

3

The issue ended up to be due to the host. All traffic was forced out on 3306 regardless of the port specified within the code.

0

The Problem is not from the host server but the local server, especially if you are using WAMP server.

configure your DB connection file on local environment as this,

class Database{
    
    // CHANGE THE DB INFO ACCORDING TO YOUR DATABASE
    private $db_host = 'localhost';
    private $db_name = 'database_api';
    private $db_username = 'root';
    private $port = "3308";
    private $db_password = '';
    
    public function dbConnection(){
        
        try{
            $conn = new PDO('mysql:host='.$this->db_host.'; port=3308; dbname='.$this->db_name,$this->db_username,$this->db_password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            return $conn;
        }
        catch(PDOException $e){
            echo "Connection error ".$e->getMessage(); 
            exit;
        }
          
    }
}

On live or deployment server, like this. @remove the port

class Database{

// CHANGE THE DB INFO ACCORDING TO YOUR DATABASE
private $db_host = 'localhost';
private $db_name = 'database_api';
private $db_username = 'root';
private $db_password = '';

public function dbConnection(){
    
    try{
        $conn = new PDO('mysql:host='.$this->db_host.'; dbname='.$this->db_name,$this->db_username,$this->db_password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $conn;
    }
    catch(PDOException $e){
        echo "Connection error ".$e->getMessage(); 
        exit;
    }
      
}

}

For some weird reason PDO internally translates ‘localhost’ to use a socket connection. That’s why it works when you use 127.0.0.1 instead, cause in that case the TCP/IP is explicitly stated by using an IP address.

That’s why it ignores the port argument, because it’s not even using TCP/IP.

This can become an issue when you have to manage many servers and you need to keep the same connection string accross all of them.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật