Setting MySQL Credentials in Linux Environment and Retrieving in PHP/JavaScript
This guide will walk you through the process of securely storing MySQL database credentials in the Linux environment and accessing them in your PHP or JavaScript code. By following these steps, you can keep your sensitive information separate from your codebase.
Prerequisites
Ubuntu Linux distribution
MySQL database server installed
PHP installed and configured with MySQL support
Step 1: Set Environment Variables
Open a terminal window.
Open the .bashrc file in your home directory using a text editor:
nano ~/.bashrc
Add the following lines at the end of the file to set the environment variables for your MySQL credentials:
Replace your_username, your_password, and your_database with your actual MySQL credentials.
Save the changes and exit the text editor (Ctrl+X, then Y, then Enter in nano).
Reload the .bashrc file to apply the changes:
source ~/.bashrc
Step 2: Retrieve Credentials in PHP
Create a new PHP file named mysql_credentials.php and add the following code:
<?php
// Retrieve MySQL credentials from environment variables
$mysql_user = getenv('MYSQL_USER');
$mysql_password = getenv('MYSQL_PASSWORD');
$mysql_database = getenv('MYSQL_DATABASE');
// Connect to the MySQL database
$conn = new mysqli('localhost', $mysql_user, $mysql_password, $mysql_database);
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully to the MySQL database.";
// Close the database connection
$conn->close();
?>
In this example, we use the getenv() function to retrieve the MySQL credentials from the environment variables set in Step 1. We then use these credentials to establish a connection to the MySQL database using the mysqli class.
Step 3: Retrieve Credentials in JavaScript (Node.js)
Create a new JavaScript file named mysql_credentials.js and add the following code:
const mysql = require('mysql');
// Retrieve MySQL credentials from environment variables
const mysql_user = process.env.MYSQL_USER;
const mysql_password = process.env.MYSQL_PASSWORD;
const mysql_database = process.env.MYSQL_DATABASE;
// Create a MySQL connection
const connection = mysql.createConnection({
host: 'localhost',
user: mysql_user,
password: mysql_password,
database: mysql_database
});
// Connect to the MySQL database
connection.connect((err) => {
if (err) {
console.error('Error connecting to the database: ' + err.stack);
return;
}
console.log('Connected successfully to the MySQL database.');
});
// Close the database connection
connection.end();
In this example, we use the process.env object to retrieve the MySQL credentials from the environment variables. We then create a MySQL connection using the mysql module and the retrieved credentials.
Make sure to install the mysql module using npm before running this script:
npm install mysql
Conclusion
By following these steps, you can securely store your MySQL database credentials in the Linux environment and access them in your PHP or JavaScript code. This approach helps keep your sensitive information separate from your codebase and reduces the risk of exposing your credentials.[1][2][4][7]
Remember to never commit your .bashrc file or any other file containing sensitive information to version control systems like Git.[3]
When deploying your application to a production server, make sure to set the environment variables accordingly on the server.[2][6]
Setting MySQL Credentials in Linux Environment and Retrieving in PHP/JavaScript
This guide will walk you through the process of securely storing MySQL database credentials in the Linux environment and accessing them in your PHP or JavaScript code. By following these steps, you can keep your sensitive information separate from your codebase.
Prerequisites
Step 1: Set Environment Variables
Open a terminal window.
Open the
.bashrc
file in your home directory using a text editor:Add the following lines at the end of the file to set the environment variables for your MySQL credentials:
Replace
your_username
,your_password
, andyour_database
with your actual MySQL credentials.Save the changes and exit the text editor (Ctrl+X, then Y, then Enter in nano).
Reload the
.bashrc
file to apply the changes:Step 2: Retrieve Credentials in PHP
Create a new PHP file named
mysql_credentials.php
and add the following code:In this example, we use the
getenv()
function to retrieve the MySQL credentials from the environment variables set in Step 1. We then use these credentials to establish a connection to the MySQL database using themysqli
class.Step 3: Retrieve Credentials in JavaScript (Node.js)
Create a new JavaScript file named
mysql_credentials.js
and add the following code:In this example, we use the
process.env
object to retrieve the MySQL credentials from the environment variables. We then create a MySQL connection using themysql
module and the retrieved credentials.Make sure to install the
mysql
module using npm before running this script:Conclusion
By following these steps, you can securely store your MySQL database credentials in the Linux environment and access them in your PHP or JavaScript code. This approach helps keep your sensitive information separate from your codebase and reduces the risk of exposing your credentials.[1][2][4][7]
Remember to never commit your
.bashrc
file or any other file containing sensitive information to version control systems like Git.[3]When deploying your application to a production server, make sure to set the environment variables accordingly on the server.[2][6]
Citations: [1] https://dba.stackexchange.com/questions/149817/are-there-mysql-client-environment-variables-for-user-and-db-on-linux [2] https://stackoverflow.com/questions/58238853/providing-db-credentials-via-environment-and-not-hard-coded [3] https://dev.mysql.com/blog-archive/mysql-shell-8-0-12-storing-mysql-passwords-securely/ [4] https://stackoverflow.com/questions/11281198/how-do-i-set-mysql-environment-variable-in-ubuntu [5] https://dev.mysql.com/doc/refman/8.3/en/environment-variables.html [6] https://www.tutorialspoint.com/setting-mysql-environment-variables-on-linux [7] https://www.oreilly.com/library/view/php-cookbook/1565926811/ch14s02.html [8] https://www.sqlmaestro.com/products/mysql/phpgenerator/help/events_using_variables/ [9] https://www.sitepoint.com/community/t/is-there-a-secured-way-of-writing-mysql-credentials-in-nodejs/413190 [10] https://community.fly.io/t/how-to-set-password-as-environment-variable-to-connect-to-mysql/17032 [11] https://dev.mysql.com/doc/refman/8.0/en/setting-environment-variables.html [12] https://forum.codeselfstudy.com/t/tutorial-how-to-use-mysql-or-mariadb-with-node-js-and-express/2260 [13] https://www.sitepoint.com/using-node-mysql-javascript-client/ [14] https://help.fortrabbit.com/env-vars [15] https://www.reddit.com/r/PHPhelp/comments/17f1sjh/what_is_the_proper_way_of_storing_mysql/ [16] https://dev.to/fadymr/php-create-your-own-php-dotenv-3k2i [17] https://stackoverflow.com/questions/21619201/environment-variables-in-database-php [18] https://serverfault.com/questions/833974/where-should-i-write-my-database-credentials-in-node-js-app [19] https://processwire.com/talk/topic/26376-database-password-in-configphp/ [20] https://nilsnh.no/2019/05/03/how-to-connect-to-a-mysql-database-server-over-ssl-with-node.js/