Database¶
Some websites have an SQL database, which can be requested on demand.
Access manually¶
It can be accessed via web-interface.
Select the correct server and connect using the credentials in private/database.yml
.
- Server
sqlweb
: for production webshares (sqlweb.phys.ethz.ch
) - Server
test-sqlweb
: for test webshares (test-sqlweb.phys.ethz.ch
)
With SSH access enabled you can connect using the cli mysql
after connecting via ssh to the web server:
wwwuser@server:~$ mysql
MariaDB [wwwshare]>
Access from code¶
For connections from your dynamic website code, directly include the database parameters from your code. This is important, because the YAML file is generated by us and the parameters may change. It also allows to share identical website configuration between production and testing.
Python¶
Add PyYAML
to your requirements.txt
and re-generate the venv,
then add the code to load the database parameters dynamically from the file:
import yaml
# Load the YAML file
with open("../private/database.yml") as stream:
try:
dbconfig = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
# Access the variables
db_host = dbconfig['database_host']
db_name = dbconfig['database_name']
db_user = dbconfig['database_user']
db_password = dbconfig['database_password']
PHP¶
<?php
try {
// Load the YAML file
$dbconfig = yaml_parse_file('../private/database.yml');
// Access the variables
$dbHost = $dbconfig['database_host'];
$dbName = $dbconfig['database_name'];
$dbUsername = $dbconfig['database_user'];
$dbPassword = $dbconfig['database_password'];
} catch (Exception $e) {
echo "Error loading YAML file: " . $e->getMessage();
}