1

I already have and database in MySQL for my one Django project. I need to make two separate Django projects share the same database.

project1/settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS' : {
            'read_default_file': '/etc/mysql/my.cnf',
        },
    }
}

project1/etc/mysql/my.cnf:

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

[client]
database = project1
user = project_user
password = Password
port = 3307
default-character-set = utf8

Here, can I have a different database (database = project2) for my second project?

I am willing to use same user and same password.

How can I do that?

3
  • use create database project2 and use that in your connect sring Commented Apr 25, 2021 at 10:53
  • Sorry, i didn't get you. I can create new database project2 , but how my running project will know that, which database in used? if i go to /etc/mysql/my.cnf from my project2 , it's show same thinks for project1 and project2. Commented Apr 25, 2021 at 10:57
  • 1
    every porject has its own my.cnf. where you declare database , so after creatuing a new datqabase, inmyssql work bench or with mysql client you edit the my.cnf file of your new proiject one project can also have multiple databases docs.djangoproject.com/en/3.2/topics/db/multi-db Commented Apr 25, 2021 at 11:01

1 Answer 1

2
CREATE DATABASE project2;

You can run the above query from MySQL client console. That will create new database "project2" in MySQL.

And change the value to "database = project2" in your second project's my.cnf. That should work. You just need to have a different my.cnf file for second project. First project path is "project1/etc/mysql/my.cnf" as mentioned above. You should have something similar "project2/etc/mysql/my.cnf" as second project my.cnf path.

Sign up to request clarification or add additional context in comments.

5 Comments

well, if i go to my.cnf from my project2 it's showing me [client] database = project1 user = project_user password = Password port = 3307 if i change here, does my project1 will able to insert data to project2?
I have updated the answer above to explain better. You need to have a different my.cnf file for second project. Each project should have a separate my.cnf file.
Thanks for your explanation, now after changing my.cnf file for project2, if i open project1 in my visual code and run the project1, it's doesn't find the project1 database. So , if i want to run my both project in the same environment, how can i get both databases active?
You can have multiple databases/schemas in a MySQL instance. You can create both project1 & project2 databases in your MySQL.
I created project1 and project2 databases in MySQL instance. Now the problem i am facing , if i edit my.cnf file as the name of project2, unfortunately, it's also changed for project1 my.cnf file. What i am trying to say you that, i am getting same my.cnf file by going /etc/mysql/my.cnf this location from different project. I understand that, there should be different my.cnf file for different projects, but while i open my project in vSCode , if i go my.cnf file, both having same database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.