本文介绍了如何连接到流氓内的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行djanog,我需要将我的数据库放在我想要连接的vagrant内。 Apache在我的本地PC上运行。我应该如何在vagrant配置文件和django中的设置文件中设置端口,以便我可以连接到vagrant内的db。我目前的django数据库配置:

  DATABASES = {
'default':{
'ENGINE' 'django.db.backends.mysql',#添加'postgresql_psycopg2','postgresql','mysql','sqlite3'或'oracle'。
'NAME':'db_name',#或使用sqlite3的数据库文件路径。
'USER':'root',#不与sqlite3一起使用。
'PASSWORD':'db_pass',#不​​与sqlite3一起使用。
'HOST':'',#设置为localhost的空字符串。不适用于sqlite3。
'PORT':'',#设置为空字符串为默认值。不适用于sqlite3。
'OPTIONS':{
init_command:SET storage_engine = INNODB,
}
},

和vagrant config:

  config.vm.network:forwarding_port,客人:80,主机:8080 
config.vm.network:forwarding_port,guest:8000,host:8001

谢谢

解决方案

您的网络呼叫应如下所示:

  config.vm.network:forwarding_port,guest:[db port x,eg 3306 for MySQL],主机:[未使用主机端口y] 

在您的Django配置中,离开主机空(默认为localhost),并从上面指定相同的端口y。 VirtualBox将您的请求转发给localhost:y到[guest]:x。


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

解决方案

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.

这篇关于如何连接到流氓内的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 00:38