MySQL 与 JSON - 为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7985145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:31:51  来源:igfitidea点击:

MySQL vs. JSON - Why?

mysqljson

提问by avinashbot

I'm designing a little web-app/game. What would be better: MySQL tables or json files? They both store information. They can both be parsed by PHP. What are the advantages/disadvantages?

我正在设计一个小的网络应用程序/游戏。什么会更好:MySQL 表或 json 文件?它们都存储信息。它们都可以被 PHP 解析。有什么优点/缺点?

This is what I mean:

这就是我的意思:

username | password
-------------------
seefour  | abc123

vs.

对比

{
  "username":"seefour",
  "password":"abc123"
}


EDIT: Wow, It's been just 3 years since I asked this question and it's surprising to see how much I've matured since when I asked this question. From a future me to the past me, this is why the two don't work. (In case anybody naive like me at the time can refer to this)

编辑:哇,距我问这个问题仅仅过去了 3 年,自从我问这个问题以来,我已经成熟了多少令人惊讶。从未来的我到过去的我,这就是为什么两者不起作用。(如果当时像我这样天真的人可以参考这个)

I used to think the two were interchangeable because they were both pretty much ways of storing information, although storing and using JSON files was easier to me at the time. Databases are separate pieces of software that make retrieving data much faster and don't end up being bloated over time. Also, carrying all the data in one or two files makes it dangerously easy to end up getting your data stolen or lost, where as a database is much more secure with those. Fundamentally, data shouldn't be part of your code; it should be a separate thing that your code works with.

我曾经认为这两者是可以互换的,因为它们几乎都是存储信息的方式,尽管当时存储和使用 JSON 文件对我来说更容易。数据库是独立的软件,可以更快地检索数据,并且不会随着时间的推移而变得臃肿。此外,将所有数据保存在一个或两个文件中,很容易导致您的数据被盗或丢失,而数据库则更安全。从根本上说,数据不应该是代码的一部分;它应该是您的代码使用的一个单独的东西。

Also, you'll learn about hashing and salting a couple of years down the line, so don't store passwords in plain text!

此外,您将在几年后学习散列和加盐,所以不要以纯文本形式存储密码!

采纳答案by Teekin

To be really blunt about, MySQL is a database while JSON is not, so the correct answer is MySQL, without hesitation. JSON is just a language, and barely even that. JSON was never designed to handle anything like concurrent connections or any sort of data manipulation, since its own function is to representdata, not to manageit.

坦率地说,MySQL 是一个数据库,而 JSON 不是,所以正确的答案是 MySQL,毫不犹豫。JSON 只是一种语言,仅此而已。JSON 从来没有被设计为处理诸如并发连接或任何类型的数据操作之类的事情,因为它自己的功能是表示数据,而不是管理数据。

So go with MySQL for storing the data. Then you should use some programming language to read that database, and send that information as JSON, rather than actually storing anything in JSON.

所以使用 MySQL 来存储数据。然后,您应该使用某种编程语言来读取该数据库,并将该信息作为 JSON 发送,而不是实际将任何内容存储在 JSON 中。

If you store the data in files, whether in JSON format or anything else, you will have all sorts of problems that people have stopped worrying about since databases started being used for the same thing. Size limitations, locks, name it. It's good enough when you have one user, but the moment you add more of them, you'll start solving so many problems that you would probably end up by writing an entire database engine just to handle the files for you, while all along you could have simply used an actual database.

如果您将数据存储在文件中,无论是 JSON 格式还是其他任何格式,您都会遇到各种各样的问题,自从数据库开始用于同样的事情以来,人们就不再担心了。大小限制,锁,命名。当您有一个用户时就足够了,但是当您添加更多用户时,您将开始解决如此多的问题,以至于您最终可能会编写一个完整的数据库引擎来为您处理文件,而一直以来可以简单地使用一个实际的数据库。

回答by Meleneth

MySQL will be preferable for many reasons, not the least of which being you do not want your web server process to have write access to the filesystem (except for possibly logging) because that is an easy way to get exploited.

出于多种原因,MySQL 会更受欢迎,其中最重要的是您不希望您的 Web 服务器进程对文件系统具有写访问权限(可能的日志记录除外),因为这是一种容易被利用的方法。

Also, the MySQL team has put a lot of engineering effort into things such as replication, concurrent access to data, ACID compliance, and data integrity.

此外,MySQL 团队在复制、数据并发访问、ACID 合规性和数据完整性等方面投入了大量工程工作。

Imagine if, for instance, you add a new field that is required in whatever data structure you are storing. If you store in JSON files, you will have to have some process that opens each file, adds the field, then saves it. Compare this to the difficulty of using ALTER TABLE with a DEFAULT value for the field. (A bit of a contrived example, but how many hacks do you want to leave in your codebase for dealing with old data?)

想象一下,例如,如果您添加一个新字段,该字段在您存储的任何数据结构中都是必需的。如果您存储在 JSON 文件中,则必须有一些过程来打开每个文件,添加字段,然后保存它。将此与将 ALTER TABLE 与字段的 DEFAULT 值一起使用的难度进行比较。(有点人为的例子,但你想在你的代码库中留下多少黑客来处理旧数据?)

回答by Yogurt The Wise

The 2 are not really comparable.

2真的没有可比性。

MySQL stores data in a database or actually is a database. JSON stores data in a format to be passed to and from the server to the client. Javascript/jquery can use JSON as data objects, but they only exist on the client side for the life of the page.

MySQL 将数据存储在数据库中或实际上是一个数据库。JSON 以在服务器和客户端之间传递的格式存储数据。Javascript/jquery 可以使用 JSON 作为数据对象,但它们只在页面的整个生命周期中存在于客户端。

So if you wanted to store data as JSON(not recommended) you'd probably have to store them as text files to save the data.

因此,如果您想将数据存储为 JSON(不推荐),您可能必须将它们存储为文本文件以保存数据。

You should store data in a database. Use functions to convert it to JSON format, then pass it to the webpage for javascript to consume and present to the user.

您应该将数据存储在数据库中。使用函数将其转换为JSON格式,然后将其传递给网页供javascript使用并呈现给用户。