[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