I have installed MySQLdb for Python and I am able to import MySQLdb. Now I try to connect to the MySQL Community Server on my local machine, using this code:
db=MySQLdb.connect(
host="localhost",
user="br_admin",
passwd="blabla",
db="br_brain"
)
This code fails with this error:
Traceback (most recent call last):
File "<pyshell#22>", line 5, in <module>
db="brainse_brain"
File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")
How do I resolve this error?
Make sure to provide the proper host and port:
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'yourdbname',
'USER': 'root',
'PASSWORD': 'your password',
'HOST': '127.0.0.1',
'PORT': '3306',
},
This is my settings.py file config for my django app.
Same for you please take host "127.0.0.1" and port "3306".
This might solve your problem.
And for python idle I've tested like...
>>> import MySQLdb
>>> Con = MySQLdb.Connect(host="127.0.0.1", port=3306, user="yoruname", passwd="yourpwd", db="test")
>>> Cursor = Con.cursor()
>>> sql = "SELECT * FROM test.testing"
>>> Cursor.execute(sql)
2L
I had the same trouble too, I'm working on a Windows of 64 bits, and the solution just was changing the host variable value. I had set "localhost" when the correct value have to be "127.0.0.1". However, when I'm working on Windows of 32 bits. I can set "localhost" or "127.0.0.1" in the host variable value and no matter, my django's project runs perfectly.
This will work fine :
db = MySQLdb.connect(host="127.0.0.1",user="db_username",passwd="db_password",db="db_name")
or
db= MySQLdb.connect("127.0.0.1","db_username","db_password","db_name")
In Windows 32, if you set host as 127.0.01 it gives the down error:
OperationalError: (2005, "Unknown MySQL server host '127.0.01' (0)")
But if you set host as 127.0.0.1 then you get no error.
If you are using windows you should specify the IP to "127.0.0.1", using "localhost" will give you that error 2003. On Ubuntu I had no problem.
1.Go to MySQL workbench
2.On the task bar above, click on server->and then click on Startup/Shutdown
3.On the screen, click on Start the server and you will get logs of server starting and running
- DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dataflair',
'USER':'root',
'PASSWORD':'',
'HOST':'127.0.0.1',
'PORT':'3306',
'OPTIONS':{
'init_command':"SET sql_mode=STRICT_TRANS_TABLES" }
Related
I'm using python 2.7.14, django 1.11 and mysql 5.7 to my local server and it work fine until i tried to connect in our external mysql 5.1 database and got below error when checking my connection (python manage.py runserver). I suspected because external Mysql is version 5.1. Below is my database setting and error message:
default':
{
'ENGINE': 'django.db.backends.mysql',
#'NAME': os.path.join(BASE_DIR, 'db.mysql'),
#'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'NAME': 'Mfg',
'USER': 'arastra',
'PASSWORD': '', #no password
'HOST': 'localhost',
'PORT': '3356', # Tunnel set-up.
File "C:\Python27\Lib\site-packages\django\db\backends\mysql\base.py", line 101, in execute
return self.cursor.execute(query, args)
File "C:\Python27\Lib\site-packages\MySQLdb\cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "C:\Python27\Lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.OperationalError: (1193, "Unknown system variable 'innodb_strict_mode'")
I am attempting to change my database settings in my django project from sqlite3 to mysql.
I edited the database object in my settings.py file :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Identity',
'USER' : 'root',
'PASSWORD': ''
}
}
I ran django-admin dbshell and got this error :
File
"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/conf/init.py",
line 39, in _setup
% (desc, ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured: Requested setting
DATABASES, but settings are not configured. You must either define the
environment variable DJANGO_SETTINGS_MODULE or call
settings.configure() before accessing settings.
I followed the instruction from this answer to use settings.configure()
from django.conf import settings
settings.configure()
It returned this message :
Traceback (most recent call last): File "", line 1, in
File
"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/conf/init.py",
line 63, in configure
raise RuntimeError('Settings already configured.') RuntimeError: Settings already configured.
When I ran python3 manage.py shell it gives me this error :
File
"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/mysql/base.py",
line 28, in
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb
module: No module named 'MySQLdb'
All I want to do is use mySql instead of sqlite db.
How do I do this ?
It seems MySQLdb packages are missing,
can you check this package are installed
apt-get install mysql-server
apt-get install mysql-client
apt-get install libmysqlclient-dev
In your virtual environment,conform mysql client installed
pip install mysqlclient
then do the mysql configuration in settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test_db',#database name
'HOST': '127.0.0.1',#
'PORT': '3306',#mysql port
'USER': 'root',#mysql username
'PASSWORD': 'test123',#mysql password
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
'charset': 'utf8mb4',
}
}
}
I'm trying to use sshtunnel and MySQLdb in python2.7 to tunnel my connection and facing following problem:
I used Sequel Pro to connect mysql was OK, but the python code did not work!
Sequel Pro is like this:
connect info
and code is like this:
`from sshtunnel import SSHTunnelForwarder
import MySQLdb
with SSHTunnelForwarder(
('2.2.2.2', 22),
ssh_username='name2',
ssh_password='mypassword',
remote_bind_address=('127.0.0.1', 3306)
) as tunnel:
connection = MySQLdb.connect(
user='name1',
password='mypassword',
host='1.1.1.1',
database='mydata',
port=3306)
I searched some example code like:
from sshtunnel import SSHTunnelForwarder
import MySQLdb
with SSHTunnelForwarder(
(_host, _ssh_port),
ssh_username=_username,
ssh_password=_password,
remote_bind_address=(_remote_bind_address, _remote_mysql_port),
local_bind_address=(_local_bind_address, _local_mysql_port)
) as tunnel:
connection = MySQLdb.connect(
user=_db_user,
password=_db_password,
host=_local_bind_address,
database=_db_name,
port=_local_mysql_port)
I want to know that did I do the right way to built ssh or connect to mysql? Thanks for help!
update:
the error message:
2017-06-15 17:52:58,415| ERROR | Could not connect to gateway 1.1.1.1:22 : 110
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
File "build/bdist.linux-x86_64/egg/sshtunnel.py", line 1483, in __enter__
File "build/bdist.linux-x86_64/egg/sshtunnel.py", line 1225, in start
File "build/bdist.linux-x86_64/egg/sshtunnel.py", line 1037, in _raise
sshtunnel.BaseSSHTunnelForwarderError: Could not establish session to SSH gateway
Update your code to add the local_bind_address:
from sshtunnel import SSHTunnelForwarder
import MySQLdb
with SSHTunnelForwarder(
('2.2.2.2', 22),
ssh_username='name2',
ssh_password='mypassword',
remote_bind_address=('127.0.0.1', 3306),
local_bind_address = ('1.1.1.1', 3306)
) as tunnel:
connection = MySQLdb.connect(
user='name1',
password='mypassword',
host='1.1.1.1',
database='mydata',
port=3306)
Unless you specify your Localhost (as '1.1.1.1' in your case) & Port; MYSQL service will not be available for the localhost.
I Hope this helps.
60 DATABASE_ENGINE = 'django.db.backends.mysql'
61 DATABASE_NAME = 'mysite'
62 DATABASE_USER = 'root'
63 DATABASE_PASSWORD = 'password'
64 DATABASE_HOST = '127.0.0.1'
65 DATABASE_PORT = '3306'
66
67 DATABASES = {
68 'default': {
69 'ENGINE': DATABASE_ENGINE,
70 'NAME': DATABASE_NAME,
71 'USER': DATABASE_USER,
72 'PASSWORD': DATABASE_PASSWORD,
73 'HOST': DATABASE_HOST,
74 'PORT': DATABASE_PORT,
75 }
76 }
Under the settings.py above in my django project,I tried to see if DB adapter(MySQLdb : http://sourceforge.net/projects/mysql-python/) is working OK with my django configuration.
But I ended up with meeting some kind of error I have never seen before like this.
python manage.py shell
>>> from django.db import connection
>>> a = connection()
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: 'DefaultConnectionProxy' object is not callable
I installed mysql on my machine, of course, and since I've done this configuration before and it worked at that time, I am confused what did I have done it wrong this time.
I know what error messages would be like when the error is something due to django's settings.py configuration. But I totally don't get what's going on with this error.
Please help me.
"The object django.db.connection represents the default database connection. To use the database connection, call connection.cursor() to get a cursor object. Then, call cursor.execute(sql, [params]) to execute the SQL and cursor.fetchone() or cursor.fetchall() to return the resulting rows."
https://docs.djangoproject.com/en/1.7/topics/db/sql/#executing-custom-sql-directly
So, if you need connection object you should do:
connection = django.db.connection
Django use persistent connection to database, so you no need to initialize it.
I'm working through the Django tutorial and receiving the following error when I run the initial python manage.py syncdb:
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 362 in execute_manager
utility.execute()
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 303, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 195, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 222, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/Library/Python/2.6/site-packages/django/core/management/commands/syncdb.py", line 49, in handle_noargs
cursor = connection.cursor()
File "/Library/Python/2.6/site-packages/django/db/backends/dummy/base.py", line 15, in complain
raise ImproperlyConfigured, "You haven't set the DATABASE_ENGINE setting yet."
django.core.exceptions.ImproperlyConfigured: You haven't set the DATABASE_ENGINE setting yet.
My settings.py looks like:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'dj_tut', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # 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.
}
}
I'm guessing this is something simple, but why isn't it seeing the ENGINE setting?
It looks like you are using an earlier version of Django. That way of setting database configuration is from Django 1.2, but the error you are getting is from 1.1. If you are using version 1.1, use this version of the tutorial.
'ENGINE': 'mysql',
'NAME': 'dj_tut',
and you will want to set a user and password.
Same problem happened frequently to me and everytime the problem was cyclic dependencies between sttings.py and another module.
At command prompt you should write:
edit settings.py
then there will be a new module for editing your
settings.py