1.虚拟机数据库设置--重启数据库
(1)vi /etc/my.cnf
bind = 0.0.0.0
(2).远程不能用root用户连,得新建用户
select host,user from mysql.user; 查看所有用户
create user devops@‘%‘ identified by ‘123456‘;
grant all on *.* to devops@‘%‘; 给新建的devops用户授权--可操作所有库的所有表
set PASSWORD for devops@‘%‘=PASSWORD (‘123456‘); 设置密码
flush privileges;
3.pycharm设置:
(1)settings.py:
DATABASES = {
‘default‘: {
‘ENGINE‘: ‘django.db.backends.mysql‘,
‘NAME‘: ‘devops‘,
‘USER‘: ‘devops‘,
‘PASSWORD‘: ‘123456‘,
‘HOST‘: ‘127.0.0.1‘,
‘PORT‘: 3306,
‘OPTIONS‘:{
‘init_command‘: ‘SET default_storage_engine=INNODB;‘,
},
}
}
(2)
因为我们连接的是虚拟机的MySQL,所以需要填SSH,如下图中:
最后点击Test Connection,
4.若报如下错误:是时区问题
Connection to Django default failed. [08001] Could not create connection to database server. Attempt
解决:(1)进入mysql查看时区并转时区
show variables like ‘%time_zone%‘; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | CEST | cest是欧洲中部夏令时间,CEST时间加上6个小时就是北京时间 | time_zone | SYSTEM | +------------------+--------+
MariaDB [(none)]> set global time_zone = ‘+6:00‘; 重新退出数据库,再登录即可看到时区改了
MariaDB [(none)]> show variables like ‘%time_zone%‘;
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CEST |
| time_zone | +06:00 |
(2)重新退出pycharm再打开即可连接上