Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put database connection settings?

Tags:

php

pdo

Where do you put the connection settings for a database connection (things like host, dbname, user, password)? Is it placed in the database class or file, or outside in a config file, or somewhere else?

like image 335
Daryll Santos Avatar asked Nov 11 '12 06:11

Daryll Santos


1 Answers

For PostgreSQL, I really like to use pg_service.conf. It allows me to put all connection specific settings (hostname, database name, username, password, etc) into ~/.pg_service.conf or to /etc/postgresql-common/pg_service.conf and give them common name (service name).

Now, any program (Perl, PHP, etc) that wants to connect to database can simply specify "service=name" as their connection string - nice, clean, secure and easily maintainable.

As far as I know, MySQL has similar mechanism for ~/my.cnf or /etc/my.cnf files - you may want to look into that.

like image 131
mvp Avatar answered Sep 21 '22 00:09

mvp