Mysql 如何连接到vagrant内部的数据库

Mysql 如何连接到vagrant内部的数据库,mysql,django,vagrant,Mysql,Django,Vagrant,我正在运行djanog,我需要将我的数据库放在我想要连接的vagrant中。Apache正在我的本地pc上运行。我应该如何在vagrant配置文件和django中的设置文件中设置端口以连接到vagrant中的db。我当前的django db配置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgre

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

   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",
            }
        },
和流浪者形态:

   config.vm.network :forwarded_port, guest: 80, host: 8080
   config.vm.network :forwarded_port, guest: 8000, host: 8001

谢谢

我以前试过这个,但那是很久以前的事了,所以这可能不起作用。我正在运行一个安装了postgres和django的
lucid32

对于数据库,您可以只使用db名称和安装它的端口。对我来说,安装vagrant没有问题,因为我的数据库托管在
127.0.0.1:5432上,这是我的windows配置。我试过了,对流浪汉也有效


您有不同的数据库和不同的配置,因此这可能不起作用,但值得一试。

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

config.vm.network :forwarded_port, guest: [db port x, e.g. 3306 for MySQL], host: [unused host port y]
在Django配置中,将主机保留为空(默认为localhost),并从上面指定相同的端口y。VirtualBox会将您的请求转发到localhost:y到[guest]:x