从零开始:Ubuntu Server中MySQL 8.0的安装与Django数据库配置详解
cac55 2024-10-17 07:59 13 浏览 0 评论
Ubuntu系统纯净安装MySQL8.0
1、安装Mysql8.0
sudo apt install mysql-server
2、检查MySQL状态
sudo systemctl status mysql
如下所示看见Active: active (running)说明mysql状态正常
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabl>
Active: active (running) since Sun 2024-01-21 14:51:23 UTC; 1min 53s ago
Process: 2929 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited,>
Main PID: 2937 (mysqld)
Status: "Server is operational"
Tasks: 37 (limit: 4557)
Memory: 365.6M
CPU: 1.428s
CGroup: /system.slice/mysql.service
└─2937 /usr/sbin/mysqld
Jan 21 14:51:19 qbc systemd[1]: Starting MySQL Community Server...
Jan 21 14:51:23 qbc systemd[1]: Started MySQL Community Server.
lines 1-14/14 (END)
3、运行安全性脚本
sudo mysql_secure_installation
问题1:
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No:
翻译:
当您在安装或配置MySQL服务器时,系统可能会提示您是否要启用VALIDATE PASSWORD组件。这个组件用于增强密码的安全性,它可以帮助您确保用户设置的密码足够强大。
以下是关于如何回答这个问题的建议:
如果您想启用VALIDATE PASSWORD组件以增强密码的安全性,您可以按y或Y键。这会强制用户设置符合一定安全要求的密码,从而增强系统的安全性。
如果您不想启用这个组件,或者不确定是否要启用它,您可以按除y或Y之外的任何键。
总的来说,启用VALIDATE PASSWORD组件是一个好主意,因为它可以帮助您避免使用弱密码,从而增强系统的安全性。但是,最终的决定取决于您的具体需求和安全策略。
问题2:
There are three levels of password validation policy:
LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:
翻译:
为了配置MySQL的密码验证策略,您需要选择一个级别。根据提供的选项,有三个级别:
- LOW:密码长度应至少为8个字符。
- MEDIUM:密码长度应至少为8个字符,并且应包含数字、大小写字母和特殊字符。
- STRONG:密码长度应至少为8个字符,并且应包含数字、大小写字母、特殊字符,并且不应与字典文件中的常见密码匹配。
根据您提供的选项,您需要输入0、1或2来选择相应的策略级别。如果您想选择LOW策略,请输入0;如果您想选择MEDIUM策略,请输入1;如果您想选择STRONG策略,请输入2。
请注意,对于生产环境,强烈建议使用STRONG策略以增强密码的安全性。
问题3:
Skipping password set for root as authentication with auth_socket is used by default. If you would like to use password authentication instead, this can be done with the "ALTER_USER" command. See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.
By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) :
这段信息是MySQL在安装或配置过程中给出的提示。我会为您逐条解释:
- Skipping password set for root as authentication with auth_socket is used by default.
- 这意味着MySQL默认使用auth_socket作为root用户的身份验证方法,而不是设置密码。因此,它跳过了为root用户设置密码的步骤。
- If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
- 如果你想使用密码验证而不是默认的身份验证方法,你可以使用ALTER USER命令来为root用户设置密码。
- See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.
- 这是一个指向MySQL官方文档的链接,提供了关于如何使用ALTER USER命令的更多信息和指导。
- By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them.
- 默认情况下,MySQL安装中有一个匿名用户,允许任何人无需创建用户账户即可登录到MySQL。
- This is intended only for testing, and to make the installation go a bit smoother.
- 这是为了方便测试和使安装过程更顺畅而设置的。
- You should remove them before moving into a production environment.
- 在将环境切换到生产环境之前,您应该删除这些匿名用户。
- Remove anonymous users? (Press y|Y for Yes, any other key for No) :
- 系统询问您是否要删除匿名用户。按y或Y键表示“是”,按除y或Y之外的任何键表示“否”。
简而言之,这段信息是关于MySQL安装过程中的一些安全和配置选项的提示。根据您的需求和环境,您可能需要采取特定的行动,例如为root用户设置密码或删除匿名用户。
问题4:
Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
这段信息是关于MySQL安全性的提示,它询问您是否要禁止root用户从远程进行登录。
如果您选择禁止root用户从远程登录,那么只有从本地主机(例如:localhost)才能使用root用户进行登录。这样可以增加安全性,因为这意味着即使有人尝试通过网络猜测root密码,也无法成功。
您可以根据以下提示进行选择:
- 按y或Y键表示“是”,禁止root用户远程登录。
- 按除y或Y之外的任何键表示“否”,允许root用户远程登录。
在大多数情况下,为了增强安全性,建议选择禁止root用户远程登录。
问题5:
By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
这段信息是关于MySQL中的“test”数据库的提示。默认情况下,MySQL安装时会有一个名为“test”的数据库,任何人都可以访问。这也是为了方便测试而设置的,在将环境切换到生产环境之前,您应该删除这个数据库及其访问权限。
您可以根据以下提示进行选择:
- 按y或Y键表示“是”,删除“test”数据库及其访问权限。
- 按除y或Y之外的任何键表示“否”,保留“test”数据库及其访问权限。
为了增强安全性,建议选择删除“test”数据库及其访问权限。
问题6:
Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :
这段信息是关于MySQL权限表的重新加载的提示。MySQL的权限系统使用所谓的“权限表”来存储和管理用户权限。当您对权限进行更改时(例如,通过GRANT或REVOKE命令),这些更改不会立即生效。为了使这些更改生效,您需要重新加载权限表。
该提示询问您是否现在要重新加载权限表。重新加载权限表将使迄今为止所做的所有权限更改立即生效。
您可以根据以下提示进行选择:
- 按y或Y键表示“是”,重新加载权限表。
- 按除y或Y之外的任何键表示“否”,不重新加载权限表。
为了确保所做的权限更改立即生效,建议选择重新加载权限表。
4、登入Mysql
sudo mysql -u root -p
如果你登录服务器用户不是root超管,那么这里会第一次提示你输入登录服务器的密码!
第二次输入密码提示才是数据库root用户的密码,默认root用户的密码为空,回车即可登录!
5、设置root用户密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'P@ssw0rd!';
提示:Query OK, 0 rows affected (0.00 sec) 代表修改成功!可退出重新登录重试!
6、创建MySQL数据库新用户
CREATE USER 'bayke'@'localhost' IDENTIFIED BY 'Abc123!..';
为创建的新用户授权,以下命令将为用户授予所有数据库的所有权限:
GRANT ALL PRIVILEGES ON *.* TO 'bayke'@'localhost';
在授予权限后,你需要刷新权限以使更改生效:
FLUSH PRIVILEGES;
之后即可退出Mysql数据库终端,记住root用户和新用户(bayke)的密码!
django配置Mysql数据库
1、django项目配置文件settings.py中替换默认的sqlite3数据库为Mysql!
# Mysql数据库配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
# 若开启mysql数据库,请在项目根目录创建一个mysql.cnf文件,内容如下:
# [client]
# database = baykeshop
# user = root
# password = root
# host = 127.0.0.1
# port = 3306
# default-character-set = utf8
'read_default_file': f"{BASE_DIR}/mysql.cnf",
'charset': 'utf8mb4',
},
}
}
2、创建Python虚拟环境并激活虚拟环境
# 创建虚拟环境
python3 -m venv venv
# 激活虚拟环境
source venv/bin/activate
激活虚拟环境之后,根据你的项目使用pip install [包名称]命令安装项目所有的依赖包!
3、安装Mysql和Python的驱动包mysqlclient
pip install mysqlclient
出现如下错误所示:
Getting requirements to build wheel ... error
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> [27 lines of output]
/bin/sh: 1: pkg-config: not found
/bin/sh: 1: pkg-config: not found
/bin/sh: 1: pkg-config: not found
Trying pkg-config --exists mysqlclient
Command 'pkg-config --exists mysqlclient' returned non-zero exit status 127.
Trying pkg-config --exists mariadb
Command 'pkg-config --exists mariadb' returned non-zero exit status 127.
Trying pkg-config --exists libmariadb
Command 'pkg-config --exists libmariadb' returned non-zero exit status 127.
Traceback (most recent call last):
File "/home/qbc/web/venv/lib/python3.10/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 363, in <module>
main()
File "/home/qbc/web/venv/lib/python3.10/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 345, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
File "/home/qbc/web/venv/lib/python3.10/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 130, in get_requires_for_build_wheel
return hook(config_settings)
File "/tmp/pip-build-env-nlhvc8rs/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 325, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=['wheel'])
File "/tmp/pip-build-env-nlhvc8rs/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires
self.run_setup()
File "/tmp/pip-build-env-nlhvc8rs/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 311, in run_setup
exec(code, locals())
File "<string>", line 155, in <module>
File "<string>", line 49, in get_config_posix
File "<string>", line 28, in find_package_name
Exception: Can not find valid pkg-config name.
Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.
这里的报错的意思是编译时pkg-config找不见mariadb和libmariadb两个包,安装即可!
sudo apt-get install pkg-config libmariadb-dev-compat libmariadb-dev
重新运行pip install mysqlclient 此时,将不出意外会安装成功!
4、运行django的迁移数据库命令makemigrations和migrate
python manage.py makemigrations
出现如下所示错误,则代表我们配置的baykeshop数据库未创建,不存在!
5、使用新创建的用户登录mysql数据库终端
mysql -u bayke -p
6、创建名为baykeshop的数据库
create database baykeshop;
7、继续运行django迁移数据库命令makemigrations和migrate
(venv) qbc@qbc:~/web/code$ python manage.py makemigrations
No changes detected
(venv) qbc@qbc:~/web/code$ python manage.py migrate
Operations to perform:
Apply all migrations: account, admin, article, auth, contenttypes, sessions, shop, system
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
...
迁移成功,配置完成!
相关推荐
- 让组策略保护Windows XP的安全
-
默认安装完WindowsXP之后,我们的WindowsXP并不很安全。因此,我们有必要对系统进行一些修修补补,一般情况下我们都要动用到注册表。诚然,修改注册表是一种非常有效的方法,但是它需要一定的...
- 你造吗?十种方式保护你免受"零日攻击"
-
|责编:王迪WindowsXP的寿终正寝,数据安全问题又再一次成为人们关注的焦点。近日,微软透漏,一个基于InternetExplorer的“零日攻击”给用户带来了严重破坏。“零日攻击”一种利用...
- 特立独行——打造游戏专用独立系统
-
大部分人的电脑是为了学习和工作用的,所以,如果你是一个游戏迷,那么推荐你安装一个独立系统专用于游戏,做到工作娱乐两不相扰。方案1:游戏专用移动WindowsXP目的:解决游戏兼容性问题喜欢玩游戏的都...
- 驰为VX8 3G Win8入门教程篇
-
距离Win8.1的正式发布也将近1年了,凭借着Win8.1在移动便携以及娱乐办公上的优势,现在的Win8平板越来越受到消费者的追捧,而驰为VX83G就是其中一款,搭载了卓越的英特尔Z3735G四核芯,...
- 易淘收银软件说明
-
易淘收银系统,简称易淘收银,专为小型及连锁零售、餐饮行业打造。基于SaaS模式,智能便捷,无需维护,轻量级设计却功能强大,简约而不失专业,助力门店高效管理收银。1、前台系统:收银客户端;2、后台系...
- CAD打不开怎么办?原因可能是电脑中毒了,6步就能完美解决问题
-
一、问题描述我的CAD安装后无法打开,安装过程中没有出现任何问题,但是安装后打开就出现一个对话框“DBXCAS0”点击后又出现“FATALERROR:UnhandledAccessViola...
- 腾讯QQ6.1正式版发布更新
-
2014-07-2405:12:00作者:张林【中关村在线软件资讯】7月24日消息:腾讯QQ官网小幅更新了QQ6.1正式版,最新版本号升级至11905,继续主打扁平化、炫酷登录窗口、支持同步最近一...
- Win10等网页版OneDrive无法登陆怎么办?
-
IT之家(www.ithome.com):Win10等网页版OneDrive无法登陆怎么办?Win10之家报道,微软OneDrive云网盘是跨平台的数据同步和存储服务,支持WindowsPC(如Wi...
- 经典回顾:折戟沉沙的Windows Longhorn有着惊艳的登录屏幕
-
尽管微软原先计划让WindowsLonghorn继承WindowsXP操作系统的衣钵,但这个充满雄心壮志的操作系统项目最终还是未能迎来曙光,而是被微软用WindowsVista取而代...
- 电脑怎么优化
-
电脑配置和宽带流量也是硬件,但这些要求其实并不需要很高,关键还是怎么去安全使用电脑并进行有效的优化。电脑的应用和优化处理一、电脑的应用和优化处理二、目前,大家使用的个人电脑,配置方面均没多大问题,比如...
- 怎么安装usb驱动
-
USB驱动主要是针对WIN98时代的说法,如今WINXP已集成大部分USB驱动,通常都能识别。只有极少数情况下,例如手机、打印机或扫描仪等办公设备的USB驱动可能无法自动识别。1、USB驱动偶尔无法...
- 普通话考试多名考生信息被泄露,接投诉后涉事网站被限制访问
-
“陕西普通话成绩查询网(sxpth.cn)”泄露个人信息网站截图网传图片显示,407名普通话考试考生的姓名、身份证号码等个人信息疑似被泄露。9月26日下午,涉事网站sxpth.cn的域名注册商——成...
- 电脑伪技巧——个人电脑无需设置登录密码
-
默认情况下,我们每次登录系统都要输入登录账户对应的密码才能进入桌面。有些朋友觉得这样很麻烦,由于电脑只是自己使用,还不如不要设置密码,这样每次可以自动登录。大家知道,账户密码是系统验证用户合法性的唯一...
- Windows 10/11 自带远程桌面:实用技巧与操作指南
-
Windows10/11自带远程桌面:实用技巧与操作指南在当今快节奏的数字时代,远程访问和控制计算机的需求日益增长。微软在Windows10和Windows11中内置了远程桌面功能,为用户提供...
- 不升级系统的5大原因造吗?
-
2015-01-2405:54:00作者:陈占伟Windows10系统的发布,让人们重新将目光聚焦到生命力长久的Windows系统之上。如今操作系统越来越多,似乎Windows升级的获得的关注度...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- 如何绘制折线图 (52)
- javaabstract (48)
- 新浪微博头像 (53)
- grub4dos (66)
- s扫描器 (51)
- httpfile dll (48)
- ps实例教程 (55)
- taskmgr (51)
- s spline (61)
- vnc远程控制 (47)
- 数据丢失 (47)
- wbem (57)
- flac文件 (72)
- 网页制作基础教程 (53)
- 镜像文件刻录 (61)
- ug5 0软件免费下载 (78)
- debian下载 (53)
- ubuntu10 04 (60)
- web qq登录 (59)
- 笔记本变成无线路由 (52)
- flash player 11 4 (50)
- 右键菜单清理 (78)
- cuteftp 注册码 (57)
- ospf协议 (53)
- ms17 010 下载 (60)