安装msyql
apt -get install mysql-server
apt install mysql-client-core-8.0
查看mysql启动状态
root@ubuntu:/home/lixuefu2# netstat -tap | grep mysql
tcp 0 0 localhost:33060 0.0.0.0:* LISTEN 822424/mysqld
tcp 0 0 localhost:mysql 0.0.0.0:* LISTEN 822424/mysqld
root@ubuntu:/home/lixuefu2# service mysql status
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2022-02-12 17:40:32 CST; 1 day 15h ago
Process: 822416 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 822424 (mysqld)
Status: "Server is operational"
Tasks: 40 (limit: 19086)
Memory: 360.3M
CGroup: /system.slice/mysql.service
└─822424 /usr/sbin/mysqld
2月 12 17:40:31 ubuntu systemd[1]: Starting MySQL Community Server...
2月 12 17:40:32 ubuntu systemd[1]: Started MySQL Community Server.
命令行直接连接
直接mysql命令连接
root@ubuntu:/home/lixuefu2# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 31
Server version: 8.0.27-0ubuntu0.21.04.1 (Ubuntu)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
使用标准命令连接
mysql -uroot -p或mysql -h localhost -uroot -p
root@ubuntu:/home/lixuefu2# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 32
Server version: 8.0.27-0ubuntu0.21.04.1 (Ubuntu)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
创建mysql的新用户
这里如果使用root直接连接,会报错
django.db.utils.OperationalError: (1698, "Access denied for user 'root'@'localhost'")
所以需要创建一个新的用户
– 使用mysql数据库
use mysql
– 创建用户
mysql> CREATE USER dev IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.01 sec)
– 授权用户
mysql> GRANT ALL ON *.* TO 'dev'@'%';
Query OK, 0 rows affected (0.00 sec)
–查看用户新建情况
mysql> select Host,user from user;
+-----------+------------------+
| Host | user |
+-----------+------------------+
| % | dev |
| % | root |
| localhost | debian-sys-maint |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+------------------+
pycharm启动django连接
mysql配置如下:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_mjtest_app_lab',
'USER': 'dev',
'PASSWORD': '123456',
# 'HOST': '127.0.0.1',
'HOST': 'localhost',
'PORT': 3306,
}
}
启动django
python manage.py runserver
February 14, 2022 - 09:31:04
Django version 3.2.12, using settings 'mjtest_app_lab.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
python manegay runserver