I've got a django project connected to a MySQL database as follows:
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'my_db',
'USER': 'username',
'PASSWORD': 'mypassword',
'HOST': '',
'PORT': '',
}
}
on running python manage.py syncdb, it creates the database and on runserver the application runs perfectly and I'm able to input data into the models using the admin panel correctly.
However, on trying to connect to the database using MySQL Workbench, it gives me
Can't connect to MySQL server on '127.0.0.1' (111) error.
I had created the database as follows:
Grant all on my_db.* to 'username'#'localhost' identified by 'mypassword';
flush privileges;
Why would Workbench show me that error even though the server is running correctly? Thanks in advance!
Edit: Used the word pythong.
I had similar issue but it got resolved by updating #port and #host into configuration file.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'vres', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '3306', # Set to empty string for default.
}
}
It could be that your MySQL server is only listening to localhost.
In your my.cnf file, comment out bind-address.
#bind-address = 127.0.0.1
Restart your MySQL server and see if you can connect with Workbench.
Related
I'm trying to change the default database used in Django to MySQL, I've adjusted my settings.py file like so:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'clients',
'HOST': 'localhost',
'PORT': '3307',
'USER': 'root',
'PASSWORD': '*****',
}
}
I'm still getting the django.db.utils.OperationalError: (1049, "Unknown database 'clients'"). When I try to connect to the 'mysql' database that comes default with MySQL I have no issue. I get this error when I try to use manage.py makemigrations,migrations or runserver. I have MySQL-Python installed and everything. Proof that my clients database exists:
Getting a strange error. I created a database in MySQL, set the database to use it. Using the right settings in my Django settings.py. But still there's an error that no database has been selected.
First I tried:
python manage.py syncdb
Got this traceback:
django.db.utils.OperationalError: (1046, 'No database selected')
settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'my_db',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '3306',
}
}
What have I missed?
Check to make sure your database my_db exists in your MySQL instance. Log into MySQL and run;
show databases;
make sure my_db exists. If it does not, run
create database my_db;
GRANT access privileges to the user mentioned in the file
GRANT ALL PRIVILEGES ON database_name.* TO 'username'#'localhost';
You need not grant all privileges. modify accordingly.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'my_db',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '3306',
}
}
Here name means database
Just delete database my_db and create again
Step 1:- Use drop database databaseName
Step 2:- Create Database databaseName
Then use the migrate command
It will definitely work
I've 2 DB in my one Django app. The two DB's are on the same network, ie on LAN. So, I suppose the HOST IP will be different. Wouldnt it be?
As of right now, to test my code, I've provided same HOST but different PORT.
So, my DB settings are as follow:-
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'vms_db',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '8000',
},
'users': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'aramex_vms_db',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '8080',
}
}
When I syncdb it, it returns an error saying:-
django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)")
But when I use the same port, it works fine but the tables are made in default DB. I know the migrate command by default works on default DB.
1) But what if I want that there should be different tables for default and different for users, how will I do that?
If you want to migrate your database users:
$ ./manage.py migrate --database=users
As you can see here: https://docs.djangoproject.com/en/dev/topics/db/multi-db/#synchronizing-your-databases
If you want to select your database while you retrieve an entry:
#default dabatase
Author.objects.all()
#custom database
Author.objects.using('DATABASE_NAME').all()
Further information: https://docs.djangoproject.com/en/dev/topics/db/multi-db/#manually-selecting-a-database
./manage.py syncdb shows some error
(ENV)vijay#vijay-Ideapad-Z570:~/nightybuild/heroku-landing$ ./manage.py syncdb
ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
(ENV)vijay#vijay-Ideapad-Z570:~/nightybuild/heroku-landing$
in my settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': '',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
but my app running successfully in heroku. when deploy its automatically heroku postgres confiqured. But in the local development its showing this error .
Can you please tell guide me how to set database for local?
What type of database are you using? Postgres, MySQL, SQLite? That will be your ENGINE value.
Also, what is your local postgres username and password? And your database name?
Below is an example with a postgres database named testdb with the username myusername and password mypassword:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'testdb',
'USER': 'myusername',
'PASSWORD': 'mypassword',
'HOST': '',
'PORT': '',
}
You may need to alter depending on your needs. The biggest change will likely be your ENGINE value. Change postgresql_psycopg2 to mysql if using MySQL or sqlite if using SQLite. Also, if using SQLite, you won't have values for USER and PASSWORD.
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.
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',
}
}
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