settings文件:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '库名',
'USER': 'root',
'PASSWORD': '密码',
'PORT': 3306,
'HOST': '远程数据库ip'
}
}
打开远程服务器终端,查看ip
ifconfig
进入远程数据库所在服务器,配置mysqld.cnf
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
mysql数据库默认绑定本地ip地址,所以我们需要修改,将以下内容复制粘贴进mysqld.cnf文件末尾
bind-address = 数据库所在服务器ip (非127.0.0.1)
重启数据库
sudo service mysql restart
如果报错,错误如下:
django.db.utils.InternalError: (1130, "Host '192.168.153.131' is not allowed to connect to this MySQL server")
解决方法如下:
登入远程mysql数据库后,更改”mysql” 数据库里的 “user” 表里的 “host”项,从”localhost”改称”%
mysql -uroot -p root
mysql->use mysql
mysql->update user set host = '%' where user ='root';
mysql->grant all privileges on *.* to 'root'@'%' with grant option;
mysql->flush privileges;
mysql->exit;
sudo service mysql restart;