简体   繁体   中英

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. 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.

./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. You probably want 127.0.0.1 .

You should create the database beforehand with create database mydatabase charset utf8; from the mysql prompt.

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? 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. An option is to do this by setting the SELINUX line in /etc/sysconfig/selinux to Disabled:

SELINUX=Disabled

Login to mysql on Terminal and create a DB

mysql -u root -p

CREATE DATABASE dbname;

USE dbname;

Then on setting.py file

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',
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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