问题
I'm running djanog, and I need to have my database inside vagrant that I want to connect to. The Apache is running on my local pc. How should I set up the port forwarting in the vagrant config file and the settings file in django so I can connect to the db inside vagrant. My current django db configuration:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'db_name', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'db_pass', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
'OPTIONS': {
"init_command": "SET storage_engine=INNODB",
}
},
and vagrant config:
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.network :forwarded_port, guest: 8000, host: 8001
thanks
回答1:
Your network call should look like this:
config.vm.network :forwarded_port, guest: [db port x, e.g. 3306 for MySQL], host: [unused host port y]
In your Django configuration, leave the host empty (defaults to localhost) and specify the same port y from above. VirtualBox will forward your request to localhost:y to [guest]:x.
回答2:
I have tried this before, but it was quite a while ago, so this might not work. I was running a lucid32
box with postgres and django installed.
For database, you can just use the db name and the port that you installed it on. For me, installing vagrant did not give issues since my db was hosted on 127.0.0.1:5432
, which was my windows config. I tried it and it worked with Vagrant as well.
You have a different database and different configurations, so this might not work, but its worth a shot.
来源:https://stackoverflow.com/questions/17810475/how-to-connect-to-a-database-inside-vagrant