I'm using docker as MySQL server, in docker I use:
docker run --name what -e MYSQL_ROOT_PASSWORD=abc123 -e MYSQL_DATABASE=mami -p 3306:3306 -d mysql
After that I tested it in MySQL workbench.
It works fine with 192.168.99.100:3306 and root/abc123
but when I try to connect it via django:
import pymysql
pymysql.install_as_MySQLdb()
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mami',
'USER': 'root',
'PASSWORD': 'abc123',
'HOST': '192.168.99.100',
'PORT': '3306',
}
}
It gives me django.db.utils.OperationalError: (1045, "Access denied for user 'root'#'192.168.99.1' (using password: NO)") when I run application or just doing python manage.py migrate
I'm Using Windows and Python 3.6, what am I doing wrong?
Thanks in advance for any help!
Related
Forgive me as I this is my first django project using postgresql (version 11.8). For now I just want to connect to a test database which I have set up locally using pgadmin4. When I create the database I am not given the option to add a password, but when I run python manage.py migrate it is insisting on a password. When I then set the password to "password" on pgadmin, django won't accept it. It's probably something really obvious as I am quite new to Django, but I have tried searching and not found the answer to my problem. In settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'test1',
'USER': 'postgres',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Last line of the error when I run python manage.py migrate:
django.db.utils.OperationalError: fe_sendauth: no password supplied
Any help much appreciated. Craig
When you installed PgAdmin4 it asked you to create a password. This is for the superuser postgres, which is what you are trying to connect as. Use psql to connect to the database using the above settings and supply the password and see if it works.
I resolved it by removing the Postgresql 11 server and reinstalling it.
I am trying to make migrations to my newly created PostgresSql database when I run
python manage.py makemigrations
it gives me the following error:
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL: role "<MYNAME>" does not
exist
the here is my windows username
I think it is maybe because I have installed PostgresSQl in the C: directory. I had to use
psql --username=postgres
in CMD to get into the database and make new user with a superuser role.
Settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('POSTGRES_DB_NAME'),
'USER': os.environ.get('POSTGRES_DB_ADMIN'),
'PASSWORD': os.environ.get('POSTGRES_DB_PASS'),
'HOST': 'localhost',
'PORT': '5432'
}
}
Any help with this would be appreciated.
import MySQLDB as Database
File "c:\pythonprojects\env\lib\site-packages\mysql_python-1.2.2-py3.6-win32.egg\mysqldb__init__py", line 22
raise ImportError, "This is MySQL Version %s, But _mysql is version %r"
SyntaxError: invalid syntax
Make sure you have pymysql installed. Then in settings.py do this
try:
import pymysql
pymysql.install_as_MySQLdb()
except:
pass
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db-name',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
i) First you have to install the mysql server in your machine, if you already have mysql server up and running then jump to next step.
ii) Create a user in mysql (you can use the root user, but I would suggest create a separate one) you can refer mysql documentation for creating a new user. Let, you have created a user with:- username = testuser , password = testpass#123
iii) Create a database in mysql , let named it testDB
iv) Now you have to install mysqlclient-python, to install it from PyPI you can do so by typing pip install mysqlclient (by default pip the python package manager points python2 series if you are setting up your django project on python3 then you have to use pip3) for more information on how to install mysqlclient-python on your particular OS you can refer this link: https://github.com/PyMySQL/mysqlclient-python
v) Now In settings.py under key DATABASES make these changes:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # for mysql engine
'NAME': 'testDB',
'USER': 'testuser',
'PASSWORD': 'testpass#123',
'HOST': 'localhost', # if you want to run your project on your localmachine
}
}
vi) Now change your working directory to your django project directory: " cd yourDjangoProject" and run these django commands
python manage.py makemigrations
python manage.py migrate
Now I hope that above django commands will run successfully and you would be able to use to your mysql database in your django project.
I'm getting an error when I query MySQL on my Django app hosted on elastic beanstalk. The error says:
OperationalError at /admin/login
(1045, "Access denied for user 'adminDB'#'172.30.23.5' (using password: YES)")
Here is my .config file:
container_commands:
01_migrate:
command: "source /opt/python/run/venv/bin/activate && python manage.py migrate --noinput"
leader_only: true
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "mysite.settings"
"PYTHONPATH": "/opt/python/current/app/mysite:$PYTHONPATH"
"ALLOWED_HOSTS": ".elasticbeanstalk.com"
"aws:elasticbeanstalk:container:python":
WSGIPath: mysite/wsgi.py
NumProcesses: 3
NumThreads: 20
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "www/static/"
Here is my the databases section on settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ['RDS_DB_NAME'],
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
}
}
I created the eb environment through the eb create command and then proceeded to create an RDS database on the eb console on their website. Is there anything else I need to do to connect Django to MySQL? Something to do with security groups or something?
Thanks!
Thanks for your response #DrewPierce, but the problem was a simple one, and it's also extremely silly. Turns out the dollar sign in my RDS password was causing a problem. Changed it to a simpler password and I successfully logged in.
Hope this helps anyone else with a similar issue!
I'm running Django 1.6 with MySQL on pythonanywhere.com. In the bash command line:
mysqldb has been pip installed
i can access mysql database from the command line with $ mysql -h $your_IP =P 3306 -u username -p
However, when I try and run python manage.py syncdb I get an error that ends with this:
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'mysql.server' (111)")
In my django settings I have
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydatabasename',
'USER': 'myusername',
'PASSWORD': 'mypassword',
'HOST': 'mysql.server',
'PORT': '3360',
}
}
I tried running python manage.py test and python manage.py testserver and got the exact same error. I tried python -v manage.py etc to get the verbose spit out and it seems a lot of other stuff is failing to import? Could it be that the settings.py isnt being found? The whole set up worked perfectly fine local, and all I've done is push it to the server and changed the database settings.
Any idea???
Your settings read port as 'PORT': '3360', change that to 3306. Also, make sure mysql.server is a valid server host. Or it could just be localhost