简体   繁体   English

1049,“未知数据库'数据库'”django mysql无法连接

[英]1049, “Unknown database 'database' ” django mysql can't connect

Exception Type: OperationalError at /
Exception Value: (1049, "Unknown database 'database'")

At the moment i tried this: 目前我试过这个:

DATABASES = {
   'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'database',                      # Or path to database file if using sqlite3.
        'USER': 'root',                      # Not used with sqlite3.
        'PASSWORD': '****',                  # Not used with sqlite3.
        'HOST': '/var/lib/mysql/database/',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '80',                      # Set to empty string for default. Not used with sqlite3.
    }
}

If i don't specify a host i get this error: 如果我没有指定主机,我会收到此错误:

OperationalError at /

(2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/database' (13)")

Can it be something with permissions? 它可以是具有权限的东西吗?

thanks in advance :) 提前致谢 :)

First, create the database on mysql. 首先,在mysql上创建数据库。 Second, edit your default conection like this. 其次,像这样编辑你的默认连接。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'MY_DATABASE_NAME',
        'USER': 'root',
        'PASSWORD': 'MY_PASSWORD',
    }
}

finally run your syncdb. 最后运行你的syncdb。

./manage.py syncdb ./manage.py syncdb

PORT is not the web server port, but the database port, which is 3306 for MySQL, and HOST is the database sever's IP or name. PORT不是Web服务器端口,而是数据库端口,对于MySQL来说是3306 ,而HOST是数据库服务器的IP或名称。 You probably want 127.0.0.1 . 你可能想要127.0.0.1

You should create the database beforehand with create database mydatabase charset utf8; 您应该事先使用create database mydatabase charset utf8; from the mysql prompt. 从mysql提示符。

I dont think you can create a database by the name database. 我不认为你可以通过名称数据库创建数据库。 So please check the following first... 所以请先检查以下内容......

1) Have installed mysql-server and created the database with proper grant all permissions? 1)安装了mysql-server并使用适当的授权创建了数据库所有权限? If you have done so then check next steps 如果您已这样做,请检查后续步骤

2)I dont know about Amazon, but this error was common in older versions of Linux distros from Redhat and Fedora. 2)我不知道亚马逊,但这个错误在Redhat和Fedora的旧版Linux发行版中很常见。 An option is to do this by setting the SELINUX line in /etc/sysconfig/selinux to Disabled: 一个选项是通过将/ etc / sysconfig / selinux中的SELINUX行设置为Disabled来执行此操作:

SELINUX=Disabled SELINUX =禁用

Login to mysql on Terminal and create a DB 登录终端上的mysql并创建一个DB

mysql -u root -p

CREATE DATABASE dbname;

USE dbname;

Then on setting.py file 然后在setting.py文件上

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbname',
        'USER': 'root', #Mysql username
        'PASSWORD': 'password', #mysql password
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM