skip to Main Content

I am working on a website and using git. The repo is public but I don’t want the mySQL info to be part of it. Should I just put the connection code in a file then block that file with .gitignore?

Is there a better, more sophisticated best practice or is it that simple?

I wish git handled mySQL better by default. I can easily save the files but not the database, but as the app grows those files will be dependent on the database. Short of using flat files or some complex mySQL dumping, I haven’t found a good solution. I think you can use a db engine that uses files instead of databases, but I’ve use mySQL for so long I hate changing everything just to accommodate git. Is there a way have git do I mySQL dump/import whenever a push / deploy is performed?

Sorry for the 3 questions in 1 but I didn’t want to make 3 threads, especially when I think I know the answer to question 1. This is my first real project in years and I almost feel like I’m having to relearn programming. So much stuff is new – flex box and flex grid and php8.2 in stead of 5 and msqli – everything is better, either a lot or just slightly, but it’s so much different stuff all at once. I hope my 38 year old brain can still get it. I’m not able to do the 12 hour coffee flued marathons like when I was in my 20s, I hope programming isn’t just a young man’s game but I’m trying to work smarter, getting up something and marketing it instead of just cranking out tons of needless code.

2

Answers


  1. Create .env file in your project root. in php use $_ENV or getenv(). add .env file to .gitignore to prevent from being included in Git.

    use package vlucas/phpdotenv for PHP for others check equivalent.

    Save your credentials in .env as below:

    DB_HOST=your_host
    DB_USER=your_user
    DB_PASS=your_password
    DB_NAME=your_database
    
    Login or Signup to reply.
  2. Regarding the database connection parameters, professional solutions usually use environment variables, as shown in Mohammed’s answer. You can store these in an .env file and ignore it with .gitignore.

    I wish git handled mySQL better by default. I can easily save the files but not the database, but as the app grows those files will be dependent on the database.

    I don’t quite get the point of your second question. Do you want to store the structural information of the MySQL database in the repository, so when checking out your repository, you could import it and getting started quickly?

    In that case, you can dump the CREATE DATABASE and CREATE TABLE commands into an .sql file. Some MySQL frontends can do it for you. Alternatively, you could have a look at Flyway, a modern database versioning and migration tool — a little bit like git for databases. It requires some learning (you’re never too old), but makes your life easier when you mastered it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search