关于django中mysql连接的几个问题


这是一个教程上找到的文章  英文比较简单 个人感觉挺有用的.

Using MySQL

To start using MySQL, you'll need to go to the MySQL tab on your dashboard, and set up a password. You'll also find the connection settings (host name, username) on th

at tab, as well as the ability to create new databases.

You can start a new MySQL console to access your databases from this tab too, or alternatively you can open a MySQL shell with the following command from a bash console or ssh session:

mysql -u USERNAME -h HOSTNAME -p 'USERNAME$DATABASENAME'

In this:

  • The USERNAME is the username you use to log in to PythonAnywhere
  • The HOSTNAME is on your Databases tab
  • The 'USERNAME$DATABASENAME' is the full name of your database, which comprises your username, then a dollar sign, then the name you gave it. The single quotes around it are important! If you don't put them in there, bash will try to interpret the $DATABASENAME as an environment variable, which will lead to an error saying ERROR 1044 (42000): Access denied for user 'USERNAME'@'%' to database 'USERNAME'

When you run the command, it will prompt you for a password -- use the one you entered on the Databases tab.

Accessing MySQL from Python

The appropriate libraries are installed for all versions of Python that we support, so if you're not using a virtualenv, to access a MySQL database just import MySQLdb.

If you are using a virtualenv, you'll need to install the correct package yourself. Start a bash console inside the virtualenv, then:

For Python 2.7

pip install mysql-python

For Python 3.x

pip install mysqlclient

MySQL with Django

To configure Django to access a MySQL database on PythonAnywhere, you need to do this in your settings file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '<your_username>$<your_database_name>',
        'USER': '<your_username>',
        'PASSWORD': '<your_mysql_password>',
        'HOST': '<your_mysql_hostname>',
    }
}

Again, you can get the username and hostname details from the "Databases" tab.

MySQL with Django tests

When you run Django tests that use the database, Django tries to create a database called test_<original database name> and that will fail because Django does not have permissions to create a new database. To run Django tests on PythonAnywhere, add a TEST key to your database definition in settings.py. Like this:

DATABASES = {
    'default': {
         ...
        'TEST': {
          NAME: '<your username>$test_<your database name>',

More info here: https://docs.djangoproject.com/en/1.10/ref/settings/#test

We suggest you use a form like <your username>$test_<your database name>. Create this database from the PythonAnywhere Databases tab and Django will happily use it and run your tests.

MySQL with web2py

To use MySQL with web2py, you'll need to change your DAL constructor:

db = DAL('mysql://<your_username>:<your_mysql_password>@<your_mysql_hostname>/<your_database_name>')

Using MySQL

To start using MySQL, you'll need to go to the MySQL tab on your dashboard, and set up a password. You'll also find the connection settings (host name, username) on that tab, as well as the ability to create new databases.

You can start a new MySQL console to access your databases from this tab too, or alternatively you can open a MySQL shell with the following command from a bash console or ssh session:

mysql -u USERNAME -h HOSTNAME -p 'USERNAME$DATABASENAME'

In this:

  • The USERNAME is the username you use to log in to PythonAnywhere
  • The HOSTNAME is on your Databases tab
  • The 'USERNAME$DATABASENAME' is the full name of your database, which comprises your username, then a dollar sign, then the name you gave it. The single quotes around it are important! If you don't put them in there, bash will try to interpret the $DATABASENAME as an environment variable, which will lead to an error saying ERROR 1044 (42000): Access denied for user 'USERNAME'@'%' to database 'USERNAME'

When you run the command, it will prompt you for a password -- use the one you entered on the Databases tab.

Accessing MySQL from Python

The appropriate libraries are installed for all versions of Python that we support, so if you're not using a virtualenv, to access a MySQL database just import MySQLdb.

If you are using a virtualenv, you'll need to install the correct package yourself. Start a bash console inside the virtualenv, then:

For Python 2.7

pip install mysql-python

For Python 3.x

pip install mysqlclient

MySQL with Django

To configure Django to access a MySQL database on PythonAnywhere, you need to do this in your settings file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '<your_username>$<your_database_name>',
        'USER': '<your_username>',
        'PASSWORD': '<your_mysql_password>',
        'HOST': '<your_mysql_hostname>',
    }
}

Again, you can get the username and hostname details from the "Databases" tab.

MySQL with Django tests

When you run Django tests that use the database, Django tries to create a database called test_<original database name> and that will fail because Django does not have permissions to create a new database. To run Django tests on PythonAnywhere, add a TEST key to your database definition in settings.py. Like this:

DATABASES = {
    'default': {
         ...
        'TEST': {
          NAME: '<your username>$test_<your database name>',

More info here: https://docs.djangoproject.com/en/1.10/ref/settings/#test

We suggest you use a form like <your username>$test_<your database name>. Create this database from the PythonAnywhere

Databases tab and Django will happily use it and run your tests.

MySQL with web2py

To use MySQL with web2py, you'll need to change your DAL constructor:

db = DAL('mysql://<your_username>:<your_mysql_password>@<your_mysql_hostname>/<your_database_name>')
https://help.pythonanywhere.com/pages/UsingMySQL/

Using MySQL

To start using MySQL, you'll need to go to the MySQL tab on your dashboard, and set up a password. You'll also find the connection settings (host name, username) on that tab, as well as the ability to create new databases.

You can start a new MySQL console to access your databases from this tab too, or alternatively you can open a MySQL shell with the following command from a bash console or ssh session:

mysql -u USERNAME -h HOSTNAME -p 'USERNAME$DATABASENAME'

In this:

  • The USERNAME is the username you use to log in to PythonAnywhere
  • The HOSTNAME is on your Databases tab
  • The 'USERNAME$DATABASENAME' is the full name of your database, which comprises your username, then a dollar sign, then the name you gave it. The single quotes around it are important! If you don't put them in there, bash will try to interpret the $DATABASENAME as an environment variable, which will lead to an error saying ERROR 1044 (42000): Access denied for user 'USERNAME'@'%' to database 'USERNAME'

When you run the command, it will prompt you for a password -- use the one you entered on the Databases tab.

Accessing MySQL from Python

The appropriate libraries are installed for all versions of Python that we support, so if you're not using a virtualenv, to access a MySQL database just import MySQLdb.

If you are using a virtualenv, you'll need to install the correct package yourself. Start a bash console inside the virtualenv, then:

For Python 2.7

pip install mysql-python

For Python 3.x

pip install mysqlclient

MySQL with Django

To configure Django to access a MySQL database on PythonAnywhere, you need to do this in your settings file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '<your_username>$<your_database_name>',
        'USER': '<your_username>',
        'PASSWORD': '<your_mysql_password>',
        'HOST': '<your_mysql_hostname>',
    }
}

Again, you can get the username and hostname details from the "Databases" tab.

MySQL with Django tests

When you run Django tests that use the database, Django tries to create a database called test_<original database name> and that will fail because Django does not have permissions to create a new database. To run Django tests on PythonAnywhere, add a TEST key to your database definition in settings.py. Like this:

DATABASES = {
    'default': {
         ...
        'TEST': {
          NAME: '<your username>$test_<your database name>',

More info here: https://docs.djangoproject.com/en/1.10/ref/settings/#test

We suggest you use a form like <your username>$test_<your database name>. Create this database from the PythonAnywhere Databases tab and Django will happily use it and run your tests.

MySQL with web2py

To use MySQL with web2py, you'll need to change your DAL constructor:

db = DAL('mysql://<your_username>:<your_mysql_password>@<your_mysql_hostname>/<your_database_name>')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值