Connecting to MySQL from Django

his my first project with django..i already tried connecting to sqlite.but now m, using the django connection to mysql nm, getting the following error ...

D:\project\wogma>manage.py syncdb Traceback (most recent call last): File "D:\project\wogma\manage.py", line 11, in <module> execute_manager(settings) File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line 340, in execute_manager utility.execute() File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line 295, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Python26\lib\site-packages\django\core\management\base.py", line 192, in run_from_argv self.execute(*args, **options.__dict__) File "C:\Python26\lib\site-packages\django\core\management\base.py", line 218, in execute self.validate() File "C:\Python26\lib\site-packages\django\core\management\base.py", line 246, in validate num_errors = get_validation_errors(s, app) File "C:\Python26\lib\site-packages\django\core\management\validation.py", lin e 65, in get_validation_errors connection.validation.validate_field(e, opts, f) File "C:\Python26\lib\site-packages\django\db\backends\mysql\validation.py", l ine 8, in validate_field db_version = connection.get_server_version() File "C:\Python26\lib\site-packages\django\db\backends\mysql\base.py", line 27 7, in get_server_version self.cursor() File "C:\Python26\lib\site-packages\django\db\backends\__init__.py", line 56, in cursor cursor = self._cursor(settings) File "C:\Python26\lib\site-packages\django\db\backends\mysql\base.py", line 26 2, in _cursor self.connection = Database.connect(**kwargs) File "C:\Python26\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect return Connection(*args, **kwargs) File "C:\Python26\lib\site-packages\MySQLdb\connections.py", line 188, in __in it__ super(Connection, self).__init__(*args, **kwargs2) _mysql_exceptions.OperationalError: (1049, "Unknown database 'd:/project/wogma/w ogma'") 

I executed the commands in mysql as follows. I already created the wogma database by simply using the create n database provided by priviliges..in mysql. whts the prb ??

+4
source share
5 answers

You are trying to specify the database along the file path (which will work for SQLite). MySQL needs a database name (for example, "wogma"). You will need to follow the instructions for installing MySQL, create a new user and database, etc. It looks like you are running Windows, so I can no longer help.

+7
source
  • open sql console and type SHOW DATABASES;
  • if wogma appears, just put this in your settings (you are using django 1.1):

    DATABASE_ENGINE = 'django.db.backends.mysql'

    DATABASE_NAME = 'wogma'

    DATABASE_HOST = '127.0.0.1'

    DATABASE_PORT = '3306'

  • if you do not just create it in this console CREATE DATABASE wogma ; and follow the steps again.

+4
source

Try with this connection configuration:

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

But First , create a database in your mysql.

+3
source

As indicated in the answers above, the code below will not work

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

You would have to download the MySQL Connector . Read this post to set up your MySQL database in Django 2.1.x

0
source

Here is a complete explanation for Windows users: I do not think any of the answers is valid. First of all, you need to install the mysql connector: https://dev.mysql.com/downloads/connector/python/1.0.html or

 pip install mysql-connector pip install protobuf //Protocol buffers (Protobuf) is a method for efficiently serializing structured data from an application so that it can be stored in a database. pip install pymysql 

After installing pymysql, you need to add this code to the file "init.py" located in the project folder.

 import pymysql pymysql.install_as_MySQLdb() 

In the folder of your project, open the settings.py file and configure the "database" section.

 DATABASES = { 'default': { 'NAME': 'db name',//u have to create the db in mysql, unlike mongoose 'ENGINE': 'mysql.connector.django', //this is where we use the connector. it is a database driver. 'USER': 'mysqlUserName', 'PASSWORD': 'password', } } 

This is the configuration for your database. Before creating a server in django, always run these two lines of code:

go to your project folder:

 python manage.py makemigrations python manage.py migrate 
0
source

Source: https://habr.com/ru/post/1337778/


All Articles