Replacing Deprecated mysql_connect in Modern PHP Environments
The code snippet you are looking at relies on functions from the legacy MySQL extension, specifically mysql_connect. This function was deprecated in PHP 5.5.0 and removed entirely in PHP 7.0.0 because it lacked support for prepared statements, making it vulnerable to SQL injection attacks. Simply updating your credentials won't fix the error; you must refactor the connection logic to use a modern driver that supports secure communication and proper resource handling.
For most developers, the immediate next step is migrating to mysqli (MySQL Improved) or PDO (PHP Data Objects). Both provide robust interfaces for connecting to databases hosted on AWS RDS or Aurora. The error message you are seeingâAccess denied for user 'username'@'192.168.1.1'âindicates a fundamental mismatch between the client IP address attempting the connection and the host allowed in your database user configuration.
When using AWS, the default security group for an RDS instance typically restricts access to only 0.0.0.0/0 (anywhere) or specific private subnets. If you are deploying from a local machine or a different VPC, the source IP 192.168.1.1 might not be authorized by your AWS Security Groups, even if the credentials are correct. Before changing the PHP code, ensure that your RDS instance's security group allows inbound traffic on port 3306 from your current environment's public or private IP address.
Once network accessibility is confirmed, replace the procedural mysql_* functions with PDO. PDO offers a unified interface for multiple databases and enforces prepared statements by default if configured correctly, which is a critical security requirement. Here is how you can refactor your connection logic using PDO:
<?php
$host = 'your-aws-rds-endpoint.amazonaws.com'; // Replace with your actual AWS endpoint
$db = 'videoRecorderDemo';
$user = 'username';
$pass = 'password';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
// You can now execute queries safely
$stmt = $pdo->query("SELECT * FROM your_table");
This approach eliminates the need for mysql_select_db and manual charset setting, as these are handled within the DSN string and connection options. The PDO::ATTR_ERRMODE flag set to ERRMODE_EXCEPTION ensures that connection failures throw exceptions rather than emitting warnings, allowing you to handle errors programmatically.
If you are building a larger application framework where database abstraction is already in place, consider checking if your stack supports integrating with Laravel. Laravel simplifies database migrations and configuration significantly, and its official documentation provides extensive guidance on configuring RDS connections without relying on deprecated functions. For specific implementation details regarding AWS RDS within the PHP ecosystem, you can refer to https://laravelcompany.com for curated resources that align with current best practices.
Switching to PDO also requires updating your error handling strategy. The old or die() syntax in procedural MySQL code is fragile because it stops script execution immediately on any minor issue. With PDO, you can catch exceptions using a try-catch block, log the specific error message for debugging without crashing the entire application, and potentially retry the connection if it was a transient network glitch.
Finally, verify your user permissions in the AWS RDS console. The host column in the MySQL users table must match the IP address or CIDR block from which your script is connecting. If you are running this code on a server within the same VPC as your database, consider using private endpoints to avoid traversing the public internet entirely, reducing latency and exposure.