[Python] install Django in ubuntu, connect MySQLdb to the database [XMAPP], and djangomysqldb

Source: Internet
Author: User
Tags install django

[Python] install Django in ubuntu, connect MySQLdb to the database [XMAPP], and djangomysqldb

 

By default, python2.7 and 3.4 are installed in Ubuntu. Here, Django is installed through pip (tool for installing and managing Python packages ).

Install pip before installing pip

 

apt-get install python-pip  
apt-get install python3-pip
The first is the python 2.x Installation tool.

 

After installation, pip installs Django online.

  pip  install Django==1.8        

The equals sign is followed by the Django version number. Here, I use 1.8. pip applies to python2. If you want to install it under python3, use the following method:

 

  pip3 install Django==1.8

To install Python 3, replace pip with pip3.

 

Test Django. The output version number indicates that the installation is successful.

 

 

Then install mysqlclient and connect to the database using python.

pip install mysqlclient

After the installation, the python directory can be seen in dist-packages.

 

Open the python console and enter import MySQLdb. If no error occurs, the installation is successful.

Next, test the connection to the database:

>>>  import MySQLdb>>> conn = MySQLdb.connect(host='localhost',user='',passwd='')

Press enter. An error occurred...

 

Cannot connect to the database, because I use the XMAPP environment, mysql is not in the default path, and mysql needs to be specified during connection. the parameter unix_socket = 'your mysql. sock path'

 

 

Similarly, in the Django project, setting. py configures the path of mysql. sock.

 

Of course, if you install apt-get, the default path is enough.

 

[Summarized several problems that cannot be connected to the database]

1. First, check whether the database service is running.

2. There may be logon IP restrictions. Edit mysql my. cnf

[Mysqld]

Bind-address = 127.0.0.1 to 0.0.0.0

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.