Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu Wang 2020-11-08 18:20:51 +08:00
commit 8b4ca521fb
4 changed files with 478 additions and 477 deletions

View File

@ -1,389 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (lxbwolf)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Improve your database knowledge with this MariaDB and MySQL cheat sheet)
[#]: via: (https://opensource.com/article/20/10/mariadb-mysql-cheat-sheet)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
Improve your database knowledge with this MariaDB and MySQL cheat sheet
======
Read this article and download our free cheat sheet to get started using
an open source database.
![Cheat Sheet cover image][1]
When you're writing an application or configuring one for a server, eventually, you will need to store persistent information. Sometimes, a configuration file, such as an INI or [YAML][2] file will do. Other times, a custom file format designed in XML or JSON or similar is better.
But sometimes you need something that can validate input, search through information quickly, make connections between related data, and generally handle your users' work adeptly. That's what a database is designed to do, and [MariaDB][3] (a fork of [MySQL][4] by some of its original developers) is a great option. I use MariaDB in this article, but the information applies equally to MySQL.
It's common to interact with a database through programming languages. For this reason, there are [SQL][5] libraries for Java, Python, Lua, PHP, Ruby, C++, and many others. However, before using these libraries, it helps to have an understanding of what's happening with the database engine and why your choice of database is significant. This article introduces MariaDB and the `mysql` command to familiarize you with the basics of how a database handles data.
If you don't have MariaDB yet, follow the instructions in my article about [installing MariaDB on Linux][6]. If you're not on Linux, use the instructions provided on the MariaDB [download page][7].
### Interact with MariaDB
You can interact with MariaDB using the `mysql` command. First, verify that your server is up and running using the `ping` subcommand, entering your MariaDB password when prompted:
```
$ mysqladmin -u root -p ping
Enter password:
mysqld is alive
```
To make exploring SQL easy, open an interactive MariaDB session:
```
$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.
Commands end with ; or \g.
[...]
Type 'help;' or '\h' for help.
Type '\c' to clear the current input statement.
MariaDB [(none)]>
```
This places you in a MariaDB subshell, and your prompt is now a MariaDB prompt. Your usual Bash commands don't work here. You must use MariaDB commands. To see a list of MariaDB commands, type `help` (or just `?`). These are administrative commands for your MariaDB shell, so they're useful for customizing your shell, but they aren't part of the SQL language.
### Learn SQL basics
The [Structured Query Language (SQL)][8] is named after what it provides: a method to inquire about the contents of a database in a predictable and consistent syntax in order to receive useful results. SQL reads a lot like an ordinary English sentence, if a little robotic. For instance, if you've signed into a database server and you need to understand what you have to work with, type `SHOW DATABASES;` and press Enter for the results.
SQL commands are terminated with a semicolon. If you forget the semicolon, MariaDB assumes you want to continue your query on the next line, where you can either do so or terminate the query with a semicolon.
```
MariaDB [(NONE)]> SHOW DATABASES;
+--------------------+
| DATABASE           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 ROWS IN SET (0.000 sec)
```
This shows there are four databases present: information_schema, mysql, performance_schema, and test. To issue queries to a database, you must select which database you want MariaDB to use. This is done with the MariaDB command `use`. Once you choose a database, your MariaDB prompt changes to reflect the active database.
```
MariaDB [(NONE)]> USE test;
MariaDB [(test)]>
```
#### Show database tables
Databases contain _tables_, which can be visualized in the same way a spreadsheet is: as a series of rows (called _records_ in a database) and columns. The intersection of a row and a column is called a _field_.
To see the tables available in a database (you can think of them as tabs in a multi-sheet spreadsheet), use the SQL keyword `SHOW` again:
```
MariaDB [(test)]> SHOW TABLES;
empty SET
```
The `test` database doesn't have much to look at, so use the `use` command to switch to the `mysql` database.
```
MariaDB [(test)]> USE mysql;
MariaDB [(mysql)]> SHOW TABLES;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| column_stats              |
| columns_priv              |
| db                        |
[...]
| time_zone_transition_type |
| transaction_registry      |
| USER                      |
+---------------------------+
31 ROWS IN SET (0.000 sec)
```
There are a lot more tables in this database! The `mysql` database is the system management database for this MariaDB instance. It contains important data, including an entire user structure to manage database privileges. It's an important database, and you don't always have to interact with it directly, but it's not uncommon to manipulate it in SQL scripts. It's also useful to understand the `mysql` database when you're learning MariaDB because it can help demonstrate some basic SQL commands.
#### Examine a table
The last table listed in this instance's `mysql` database is titled `user`. This table contains data about users permitted to access the database. Right now, there's only a root user, but you can add other users with varying privileges to control whether each user can view, update, or create data. To get an idea of all the attributes a MariaDB user can have, you can view column headers in a table:
```
> SHOW COLUMNS IN USER;
MariaDB [mysql]> SHOW COLUMNS IN USER;
+-------------+---------------+------+-----+----------+
| FIELD       | TYPE          | NULL | KEY | DEFAULT  |
+-------------+---------------+------+-----+----------+
| Host        | CHAR(60)      | NO   | PRI |          |
| USER        | CHAR(80)      | NO   | PRI |          |
| Password    | CHAR(41)      | NO   |     |          |
| Select_priv | enum('N','Y') | NO   |     | N        |
| Insert_priv | enum('N','Y') | NO   |     | N        |
| Update_priv | enum('N','Y') | NO   |     | N        |
| Delete_priv | enum('N','Y') | NO   |     | N        |
| Create_priv | enum('N','Y') | NO   |     | N        |
| Drop_priv   | enum('N','Y') | NO   |     | N        |
[...]
47 ROWS IN SET (0.001 sec)
```
#### Create a new user
Whether you need help from a fellow human to administer a database or you're setting up a database for a computer to use (for example, in a WordPress, Drupal, or Joomla installation), it's common to need an extra user account within MariaDB. You can create a MariaDB user either by adding it to the `user` table in the `mysql` database, or you can use the SQL keyword `CREATE` to prompt MariaDB to do it for you. The latter features some helper functions so that you don't have to generate all the information manually:
```
`> CREATE USER 'tux'@'localhost' IDENTIFIED BY 'really_secure_password';`
```
#### View table fields
You can view fields and values in a database table with the `SELECT` keyword. In this example, you created a user called `tux`, so select the columns in the `user` table:
```
> SELECT USER,host FROM USER;
+------+------------+
| USER | host       |
+------+------------+
| root | localhost  |
[...]
| tux  | localhost  |
+------+------------+
7 ROWS IN SET (0.000 sec)
```
#### Grant privileges to a user
By looking at the column listing on the `user` table, you can explore a user's status. For instance, the new user `tux` doesn't have permission to do anything with the database. Using the `WHERE` statement, you can view only the record for `tux`:
```
> SELECT USER,select_priv,insert_priv,update_priv FROM USER WHERE USER='tux';
+------+-------------+-------------+-------------+
| USER | select_priv | insert_priv | update_priv |
+------+-------------+-------------+-------------+
| tux  | N           | N           | N           |
+------+-------------+-------------+-------------+
```
Use the `GRANT` command to modify user permissions:
```
> GRANT SELECT ON *.* TO 'tux'@'localhost';
> FLUSH PRIVILEGES;
```
Verify your change:
```
> SELECT USER,select_priv,insert_priv,update_priv FROM USER WHERE USER='tux';
+------+-------------+-------------+-------------+
| USER | select_priv | insert_priv | update_priv |
+------+-------------+-------------+-------------+
| tux  | Y           | N           | N           |
+------+-------------+-------------+-------------+
```
User `tux` now has privileges to select records from all tables.
### Create a custom database
So far, you've interacted just with the default databases. Most people rarely interact much with the default databases outside of user management. Usually, you create a database and populate it with tables full of custom data.
#### Create a MariaDB database
You may already be able to guess how to create a new database in MariaDB. It's a lot like creating a new user:
```
> CREATE DATABASE example;
Query OK, 1 ROW affected (0.000 sec)
> SHOW DATABASES;
+--------------------+
| DATABASE           |
+--------------------+
| example            |
[...]
```
Make this new database your active one with the `use` command:
```
`> USE example;`
```
#### Create a table
Creating a table is more complex than creating a database because you must define column headings. MariaDB provides many convenience functions for you to use when creating columns, including data type definitions, automatic incrementing options, constraints to avoid empty values, automated timestamps, and more.
Here's a simple table to describe a set of users:
```
> CREATE TABLE IF NOT EXISTS member (
    -> id INT AUTO_INCREMENT PRIMARY KEY,
    -> name VARCHAR(128) NOT NULL,
    -> startdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
Query OK, 0 ROWS affected (0.030 sec)
```
This table provides a unique identifier to each row by using an auto-increment function. It contains a field for a user's name, which cannot be empty (or `null`), and generates a timestamp when the record is created.
Populate this table with some sample data using the `INSERT` SQL keyword:
```
> INSERT INTO member (name) VALUES ('Alice');
Query OK, 1 ROW affected (0.011 sec)
> INSERT INTO member (name) VALUES ('Bob');
Query OK, 1 ROW affected (0.011 sec)
> INSERT INTO member (name) VALUES ('Carol');
Query OK, 1 ROW affected (0.011 sec)
> INSERT INTO member (name) VALUES ('David');
Query OK, 1 ROW affected (0.011 sec)
```
Verify the data in the table:
```
> SELECT * FROM member;
+----+-------+---------------------+
| id | name  | startdate           |
+----+-------+---------------------+
|  1 | Alice | 2020-10-03 15:25:06 |
|  2 | Bob   | 2020-10-03 15:26:43 |
|  3 | Carol | 2020-10-03 15:26:46 |
|  4 | David | 2020-10-03 15:26:51 |
+----+-------+---------------------+
4 ROWS IN SET (0.000 sec)
```
#### Add multiple rows at once
Now create a second table:
```
> CREATE TABLE IF NOT EXISTS linux (
    -> id INT AUTO_INCREMENT PRIMARY KEY,
    -> distro VARCHAR(128) NOT NULL,
Query OK, 0 ROWS affected (0.030 sec)
```
Populate it with some sample data, this time using a little `VALUES` shortcut so you can add multiple rows in one command. The `VALUES` keyword expects a list in parentheses, but it can take multiple lists separated by commas:
```
> INSERT INTO linux (distro)
 -> VALUES ('Slackware'), ('RHEL'),('Fedora'),('Debian');
Query OK, 4 ROWS affected (0.011 sec)
Records: 4  Duplicates: 0  Warnings: 0
> SELECT * FROM linux;
+----+-----------+
| id | distro    |
+----+-----------+
|  1 | Slackware |
|  2 | RHEL      |
|  3 | Fedora    |
|  4 | Debian    |
+----+-----------+
```
### Create relationships between tables
You now have two tables, but there's no relationship between them. They each contain independent data, but you might need to associate a member of the first table to a specific item listed in the second.
To do that, you can create a new column for the first table that corresponds to something in the second. Because both tables were designed with unique identifiers (the auto-incrementing `id` field), the easiest way to connect them is to use the `id` field of one as a selector for the other.
Create a new column in the first table to represent a value in the second table:
```
> ALTER TABLE member ADD COLUMN (os INT);
Query OK, 0 ROWS affected (0.012 sec)
Records: 0  Duplicates: 0  Warnings: 0
> DESCRIBE member;
DESCRIBE member;
+-----------+--------------+------+-----+---------+------+
| FIELD     | TYPE         | NULL | KEY | DEFAULT | Extra|
+-----------+--------------+------+-----+---------+------+
| id        | INT(11)      | NO   | PRI | NULL    | auto_|
| name      | VARCHAR(128) | NO   |     | NULL    |      |
| startdate | TIMESTAMP    | NO   |     | cur[...]|      |
| os        | INT(11)      | YES  |     | NULL    |      |
+-----------+--------------+------+-----+---------+------+
```
Using the unique IDs of the `linux` table, assign a distribution to each member. Because the records already exist, use the `UPDATE` SQL keyword rather than `INSERT`. Specifically, you want to select one row and then update the value of one column. Syntactically, this is expressed a little in reverse, with the update happening first and the selection matching last:
```
> UPDATE member SET os=1 WHERE name='Alice';
Query OK, 1 ROW affected (0.007 sec)
ROWS matched: 1  Changed: 1  Warnings: 0
```
Repeat this process for the other names in the `member` table to populate it with data. For variety, assign three different distributions across the four rows (doubling up on one).
#### Join tables
Now that these two tables relate to one another, you can use SQL to display the associated data. There are many kinds of joins in databases, and you can try them all once you know the basics. Here's a basic join to correlate the values found in the `os` field of the `member` table to the `id` field of the `linux` table:
```
SELECT * FROM member JOIN linux ON member.os=linux.id;
+----+-------+---------------------+------+----+-----------+
| id | name  | startdate           | os   | id | distro    |
+----+-------+---------------------+------+----+-----------+
|  1 | Alice | 2020-10-03 15:25:06 |    1 |  1 | Slackware |
|  2 | Bob   | 2020-10-03 15:26:43 |    3 |  3 | Fedora    |
|  4 | David | 2020-10-03 15:26:51 |    3 |  3 | Fedora    |
|  3 | Carol | 2020-10-03 15:26:46 |    4 |  4 | Debian    |
+----+-------+---------------------+------+----+-----------+
4 ROWS IN SET (0.000 sec)
```
The `os` and `id` fields form the join.
In a graphical application, you can imagine that the `os` field might be set by a drop-down menu, the values for which are drawn from the contents of the `distro` field of the `linux` table. By using separate tables for unique but related sets of data, you ensure the consistency and validity of data, and thanks to SQL, you can associate them dynamically later.
### [Download the MariaDB and MySQL cheat sheet][9]
MariaDB is an enterprise-grade database. It's designed and proven to be a robust, powerful, and fast database engine. Learning it is a great step toward using it to do things like managing web applications or programming language libraries. As a quick reference when you're using MariaDB, [download our MariaDB and MySQL cheat sheet][9].
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/10/mariadb-mysql-cheat-sheet
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/coverimage_cheat_sheet.png?itok=lYkNKieP (Cheat Sheet cover image)
[2]: https://www.redhat.com/sysadmin/yaml-tips
[3]: https://mariadb.org/
[4]: https://www.mysql.com/
[5]: https://en.wikipedia.org/wiki/SQL
[6]: https://opensource.com/article/20/10/install-mariadb-and-mysql-linux
[7]: https://mariadb.org/download
[8]: https://publications.opengroup.org/c449
[9]: https://opensource.com/downloads/mariadb-mysql-cheat-sheet

View File

@ -1,88 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (5 surprising ways I use Jupyter to improve my life)
[#]: via: (https://opensource.com/article/20/11/surprising-jupyter)
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
5 surprising ways I use Jupyter to improve my life
======
Jupyter is much more than a data-analysis tool. Learn about some of the
most creative ways you can use the Python-based software.
![Computer laptop in space][1]
The [Jupyter][2] project offers interactive ways to write software with technology like JupyterLab and Jupyter Notebook. This software is commonly used for data analysis, but what you might not know (and the Jupyter community didn't expect) is how many things you can do with it.
Here are my top five unexpected and creative ways to use Jupyter.
### 1\. Manipulate images
There are great open source tools out there for [image editing and manipulation][3]—from those that rival Photoshop to the experimental work of [Glimpse][4]. Even with all those options, sometimes I just don't want to leave the world of [Python][5].
Luckily, Jupyter is a great option for doing light image manipulation. Taking advantage of the fact that Jupyter directly shows [Pillow][6] objects as images lets you experiment with pictures as much as you want. I even used it to [make a coloring book page][7] for my child.
### 2\. Make an SSH jumpbox remote control
Since JupyterLab lets you [upload and download][8] files, [edit][9] files, and even [run terminals][10], it has all the pieces necessary to make an SSH jumpbox environment.
With some SSH-forwarding magic, you can make Jupyter your [remote console][11] on the other side of a firewall.
### 3\. Develop web applications
One of my favorite ways to use Jupyter is for an unexpected kind of software development. I [gave a talk][12] where I developed a [web application in real time][13] using a Jupyter notebook. The talk concluded with a simple form that is XSS- and CSS-safe and included some light server-side computation.
A day-to-day Jupyter user may not expect it to be a great web development environment, but it's a remarkably powerful one.
### 4\. Pull reports from your favorite services
Data analysis in JupyterLab is a common use, but what about self-improvement analysis?
You can use Jupyter to [analyze your calendar][14]. If your favorite services allow API export, or even let you export a CSV, you can correlate those against your calendar. If you find out that you were posting on social media when your calendar said you were supposed to be in a meeting with your manager, however, Jupyter can't do much to help you!
### 5\. Develop games
My favorite way to expand the expectations of what I can accomplish in a Jupyter Notebook is by building a game with my child. I wrote about this previously, with a step-by-step tutorial on [writing a game][15] using [PursuedPyBear][16] and Jupyter.
This iterative approach to game development is especially helpful when trying to figure out game mechanics. It's a game-changer (sorry, I had to) to be able to change the rules mid-game.
You can even use IPywidgets to modify the numeric parameters, as [this video][17] shows.
### [Download the eBook][18]
JupyterLab and Jupyter Notebooks offer an incredible environment for experimenting. [Download this guide][18] that contains tutorials on the surprising ways to use Jupyter. 
How are you using it in creative ways? Share your favorites in the comments below.
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/11/surprising-jupyter
作者:[Moshe Zadka][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/moshez
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/computer_space_graphic_cosmic.png?itok=wu493YbB (Computer laptop in space)
[2]: https://jupyter.org/
[3]: https://opensource.com/life/12/6/design-without-debt-five-tools-for-designers
[4]: https://glimpse-editor.github.io/
[5]: https://opensource.com/resources/python
[6]: https://pillow.readthedocs.io/en/stable/index.html
[7]: https://opensource.com/article/20/8/edit-images-python
[8]: https://jupyterlab.readthedocs.io/en/stable/user/files.html#uploading-and-downloading
[9]: https://jupyterlab.readthedocs.io/en/stable/user/files.html#opening-files
[10]: https://jupyterlab.readthedocs.io/en/stable/user/terminal.html
[11]: https://opensource.com/article/20/8/remote-management-jupyter
[12]: https://opensource.com/article/20/8/write-talk-using-jupyter-notebooks
[13]: https://github.com/moshez/interactive-web-development/blob/e31ae72d8cab7637d18bc734c4e8afc10c60251f/interactive-web-development.ipynb
[14]: https://opensource.com/article/20/9/analyze-your-life-jupyter
[15]: https://opensource.com/article/20/5/python-games
[16]: https://ppb.dev/
[17]: https://www.youtube.com/watch?v=JaTf_ZT7tE8
[18]: https://opensource.com/downloads/jupyter-guide

View File

@ -0,0 +1,389 @@
[#]: collector: "lujun9972"
[#]: translator: "lxbwolf"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
[#]: subject: "Improve your database knowledge with this MariaDB and MySQL cheat sheet"
[#]: via: "https://opensource.com/article/20/10/mariadb-mysql-cheat-sheet"
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
使用这个 MariaDB 和 MySQL 备忘单提升你的数据库技能
======
阅读本文并下载我们的免费备忘单,去使用开源的数据库吧。
![Cheat Sheet cover image][1]
当你写一个程序或配置一个服务时,你最终都要持久化存储信息。有时候,你只需要一个 INI 或者 [YAML][2] 配置文件就够了。而有时候,一个自定义格式的 XML 或者 JSON 或其他类似的文件会更好。
但也有时候你需要校验输入、快速查询信息、关联数据、通常还要熟练地处理你的用户的请求。这就是设计数据库的目的,而 [MariaDB][3](由 [MySQL][4] 的原始开发人员开发的一个分支) 是一个极佳的选项。在本文中我使用的是 MariaDB但这些信息同样适用于 MySQL。
通过编程语言与数据库进行交互是很普遍的。正因如此,出现了大量 Java、Python、Lua、PHP、Ruby、C++ 和其他语言的 [SQL][5] 库。然而,在使用这些库之前,理解数据库引擎做了什么以及为什么选择数据库是重要的对我们会很有帮助。本文介绍 MariaDB 和 `mysql` 命令来帮助你熟悉数据库处理数据的基本原理。
如果你还没有安装 MariaDB请查阅我的文章 [在 Linux 上安装 MariaDB][6]。如果你没有使用 Linux请参照 MariaDB [下载页面][7]提供的指导方法。
### 与 MariaDB 交互
你可以使用 `mysql` 命令与 MariaDB 进行交互。首先使用子命令 `ping` 确认你的服务是运行着的,在提示后输入密码:
```
$ mysqladmin -u root -p ping
Enter password:
mysqld is alive
```
为了易于读者理解,打开一个交互式的 MariaDB 会话:
```
$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.
Commands end with ; or \g.
[...]
Type 'help;' or '\h' for help.
Type '\c' to clear the current input statement.
MariaDB [(none)]>
```
你现在是在一个 MariaDB 子 shell 中,提示框是 MariaDB 提示框。普通的 Bash 命令在这里不能使用,只能用 MariaDB 命令。输入 `help` (或 `?`)查看命令列表。这些是你的 MariaDB shell 的管理命令,使用它们可以定制你的 shell但它们不属于 SQL 语言。
### 学习 SQL 基本知识
[结构化查询语言][8] 是基于它们的能力定义的一种通过有规则且一致的语法来查询数据库中的内容得到有用的结果的方法。SQL 看起来像是普通的英文语句,有一点点生硬。例如,如果你登入数据库服务器,想查看有哪些库,输入 `SHOW DATABASES;` 并回车就能看到结果。
SQL 命令以分号作为结尾。如果你忘记输入分号MariaDB 会认为你是想在下一行继续输入你的查询命令,在下一行你可以继续输入命令也可以输入分号结束命令。
```
MariaDB [(NONE)]> SHOW DATABASES;
+--------------------+
| DATABASE |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 ROWS IN SET (0.000 sec)
```
上面的例子显示当前有四个数据库information_schema、mysql、performance_schema 和 test。你必须指定 MariaDB 使用哪个库,才能对该库使用查询语句。指定数据库的命令是 `use`。当你选择了一个库后MariaDB 提示框会切换为选择的库。
```
MariaDB [(NONE)]> USE test;
MariaDB [(test)]>
```
#### 显示数据库的表
数据库里有_表_与电子表格类似有一系列的行在数据库中称为_记录_和列。一个行和一个列唯一确定一个 _filed_
查看一个数据库中可用的表(可以理解为多表单电子表格中的一页),使用 SQL 关键字 `SHOW`:
```
MariaDB [(test)]> SHOW TABLES;
empty SET
```
`test` 数据库是空的,所以使用 `use` 命令切换到 `mysql` 数据库:
```
MariaDB [(test)]> USE mysql;
MariaDB [(mysql)]> SHOW TABLES;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| column_stats |
| columns_priv |
| db |
[...]
| time_zone_transition_type |
| transaction_registry |
| USER |
+---------------------------+
31 ROWS IN SET (0.000 sec)
```
这个数据库中有很多表!`mysql` 数据库是这个 MariaDB 实例的系统管理数据库。它里面包含重要数据,比如用来管理数据库权限的用户结构。这个数据库很重要,你不需要经常直接与它交互,但是使用 SQL 脚本来操作它却很常见。当你学习 MariaDB 时理解 `mysql` 数据库很有用,因为它有助于说明一些基本的 SQL 命令。
#### 检查一个表
这个实例的 `mysql` 数据库的最后一个表名为 `user`。这个表包含了可以访问这个数据库的用户。当前里面只有一个 root 用户,但是你可以添加不同权限的用户,赋予它们查看、更新或创建数据的权限。你可以查看一个表的列首来了解一个 MariaDB 用户的所有属性:
```
> SHOW COLUMNS IN USER;
MariaDB [mysql]> SHOW COLUMNS IN USER;
+-------------+---------------+------+-----+----------+
| FIELD | TYPE | NULL | KEY | DEFAULT |
+-------------+---------------+------+-----+----------+
| Host | CHAR(60) | NO | PRI | |
| USER | CHAR(80) | NO | PRI | |
| Password | CHAR(41) | NO | | |
| Select_priv | enum('N','Y') | NO | | N |
| Insert_priv | enum('N','Y') | NO | | N |
| Update_priv | enum('N','Y') | NO | | N |
| Delete_priv | enum('N','Y') | NO | | N |
| Create_priv | enum('N','Y') | NO | | N |
| Drop_priv | enum('N','Y') | NO | | N |
[...]
47 ROWS IN SET (0.001 sec)
```
#### 创建一个新的用户
不论你是否需要一个普通的账号来管理数据库或者为计算机配置数据库(例如安装 WordPress、Drupal 或 Joomla时在 MariaDB 中多建一个用户账号是很普遍的。你可以通过向 `mysql` 数据库的 `user` 表中添加一个用户或使用 SQL 关键字 `CREATE` 来提示 MariaDB 创建一个 MariaDB 用户。使用 `CREATE` 来创建新用户会默认执行一些有用的方法,因此你不需要手动生成所有的信息:
```
`> CREATE USER 'tux'@'localhost' IDENTIFIED BY 'really_secure_password';`
```
#### 查看表的字段
你可以使用 `SELECT` 关键字来查看数据库表的字段和值。这本例中,你创建了一个名为 `tux` 的用户,因此查询 `user` 表中的列:
```
> SELECT USER,host FROM USER;
+------+------------+
| USER | host |
+------+------------+
| root | localhost |
[...]
| tux | localhost |
+------+------------+
7 ROWS IN SET (0.000 sec)
```
#### 为一个用户赋予权限
通过查看 `user` 表列出的信息,你可以看到用户的状态。例如,新用户 `tux` 对这个数据库没有任何权限。使用 `WHERE` 语句你可以只查 `tux` 那一条记录。
```
> SELECT USER,select_priv,insert_priv,update_priv FROM USER WHERE USER='tux';
+------+-------------+-------------+-------------+
| USER | select_priv | insert_priv | update_priv |
+------+-------------+-------------+-------------+
| tux | N | N | N |
+------+-------------+-------------+-------------+
```
使用 `GRANT` 命令修改用户的权限:
```
> GRANT SELECT ON *.* TO 'tux'@'localhost';
> FLUSH PRIVILEGES;
```
验证你的修改:
```
> SELECT USER,select_priv,insert_priv,update_priv FROM USER WHERE USER='tux';
+------+-------------+-------------+-------------+
| USER | select_priv | insert_priv | update_priv |
+------+-------------+-------------+-------------+
| tux | Y | N | N |
+------+-------------+-------------+-------------+
```
`tux` 用户现在有了从所有表中查询记录的权限。
### 创建自定义的数据库
到目前为止,你一直在与默认的数据库进行交互。除了用户管理,大部分人很少会与默认的数据库进行交互。通常,你会用自定义的数据来填充创建的数据库。
#### 创建一个 MariaDB 数据库
你可能已经可以自己在 MariaDB 中创建新数据库了。创建数据库跟新建用户差不多。
```
> CREATE DATABASE example;
Query OK, 1 ROW affected (0.000 sec)
> SHOW DATABASES;
+--------------------+
| DATABASE |
+--------------------+
| example |
[...]
```
使用 `use` 命令来把这个新建的数据库作为当前使用的库:
```
`> USE example;`
```
#### 创建一个表
创建表比创建数据库要复杂因为你必须定义列首。MariaDB 提供了很多方便的函数,可以用于创建列,引入数据类型定义,自增选项,对空值的约束,自动时间戳等等。
下面是用来描述一系列用户的一个简单的表:
```
> CREATE TABLE IF NOT EXISTS member (
-> id INT AUTO_INCREMENT PRIMARY KEY,
-> name VARCHAR(128) NOT NULL,
-> startdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
Query OK, 0 ROWS affected (0.030 sec)
```
这个表通过使用一个自动递增的方法来唯一标识每一行。表示用户名字的字段不能为空(或 `null`),每一行被创建时会自动生成时间戳。
使用 SQL 关键字 `INSERT` 向这个表填充一些示例数据:
```
> INSERT INTO member (name) VALUES ('Alice');
Query OK, 1 ROW affected (0.011 sec)
> INSERT INTO member (name) VALUES ('Bob');
Query OK, 1 ROW affected (0.011 sec)
> INSERT INTO member (name) VALUES ('Carol');
Query OK, 1 ROW affected (0.011 sec)
> INSERT INTO member (name) VALUES ('David');
Query OK, 1 ROW affected (0.011 sec)
```
验证一下表里的数据:
```
> SELECT * FROM member;
+----+-------+---------------------+
| id | name | startdate |
+----+-------+---------------------+
| 1 | Alice | 2020-10-03 15:25:06 |
| 2 | Bob | 2020-10-03 15:26:43 |
| 3 | Carol | 2020-10-03 15:26:46 |
| 4 | David | 2020-10-03 15:26:51 |
+----+-------+---------------------+
4 ROWS IN SET (0.000 sec)
```
#### 同时增加多行数据
再创建一个表:
```
> CREATE TABLE IF NOT EXISTS linux (
-> id INT AUTO_INCREMENT PRIMARY KEY,
-> distro VARCHAR(128) NOT NULL,
Query OK, 0 ROWS affected (0.030 sec)
```
填充一些示例数据,这次使用 `VALUES` 快捷方式,这样你可以一次添加多行数据。`VALUES` 关键字需要一个用括号包围的列表作为参数,也可以用逗号分隔的多个列表作为参数。
```
> INSERT INTO linux (distro)
-> VALUES ('Slackware'), ('RHEL'),('Fedora'),('Debian');
Query OK, 4 ROWS affected (0.011 sec)
Records: 4 Duplicates: 0 Warnings: 0
> SELECT * FROM linux;
+----+-----------+
| id | distro |
+----+-----------+
| 1 | Slackware |
| 2 | RHEL |
| 3 | Fedora |
| 4 | Debian |
+----+-----------+
```
### 关联多个表
现在你有两个表,之间没有关联。两个表的数据是独立的,但是你可能需要表一中的一个值来识别表二的记录。
你可以在表一中新增一列对应表二中的值。因为两个表都有唯一的标识符(自动递增的 `id` 字段),关联的它们的最简单的方式是,使用表一中的 `id` 字段作为表二的查询条件。
在表一中创建一列用来表示表二中的一个值。
```
> ALTER TABLE member ADD COLUMN (os INT);
Query OK, 0 ROWS affected (0.012 sec)
Records: 0 Duplicates: 0 Warnings: 0
> DESCRIBE member;
DESCRIBE member;
+-----------+--------------+------+-----+---------+------+
| FIELD | TYPE | NULL | KEY | DEFAULT | Extra|
+-----------+--------------+------+-----+---------+------+
| id | INT(11) | NO | PRI | NULL | auto_|
| name | VARCHAR(128) | NO | | NULL | |
| startdate | TIMESTAMP | NO | | cur[...]| |
| os | INT(11) | YES | | NULL | |
+-----------+--------------+------+-----+---------+------+
```
`linux` 表中的独一无二的 ID 分配给每个成员。因为记录已经存在,使用 `UPDATE` 关键字而不是 `INSERT`。尤其是当你想查询某行然后再更新某列值时。语法上,表达方式有点倒装,先更新后查询:
```
> UPDATE member SET os=1 WHERE name='Alice';
Query OK, 1 ROW affected (0.007 sec)
ROWS matched: 1 Changed: 1 Warnings: 0
```
要填充数据,请对其他名字重复执行这个过程。为了数据的多样性,在四行记录中分配三个不同的值。
#### 连接表
现在这两个表彼此有了关联,你可以使用 SQL 来展示关联的数据。数据库中有很多种连接方式,你可以尽请尝试。下面的例子是关联 `member` 表中 `os` 字段和 `linux` 表中 `id` 字段:
```
SELECT * FROM member JOIN linux ON member.os=linux.id;
+----+-------+---------------------+------+----+-----------+
| id | name | startdate | os | id | distro |
+----+-------+---------------------+------+----+-----------+
| 1 | Alice | 2020-10-03 15:25:06 | 1 | 1 | Slackware |
| 2 | Bob | 2020-10-03 15:26:43 | 3 | 3 | Fedora |
| 4 | David | 2020-10-03 15:26:51 | 3 | 3 | Fedora |
| 3 | Carol | 2020-10-03 15:26:46 | 4 | 4 | Debian |
+----+-------+---------------------+------+----+-----------+
4 ROWS IN SET (0.000 sec)
```
连接 `os``id` 字段。
在图像化的应用中,你可以想象 `os` 字段可以在下拉菜单中设置,值的来源是 `linux` 表中的 `distro` 字段。通过使用多个表中独立却有关联的数据,你可以保证数据的一致性和有效性,使用 SQL 你可以动态地关联它们。
### [下载 MariaDB 和 MySQL 备忘单][9]
MariaDB 是企业级的数据库。它是健壮、强大、高效的数据库引擎。学习它是你向管理 web 应用和编写语言库迈出的伟大的一步。你可以[下载 MariaDB 和 MySQL 备忘单][9],在你使用 MariaDB 时可以快速参考。
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/10/mariadb-mysql-cheat-sheet
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[lxbwolf](https://github.com/lxbwolf)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/coverimage_cheat_sheet.png?itok=lYkNKieP "Cheat Sheet cover image"
[2]: https://www.redhat.com/sysadmin/yaml-tips
[3]: https://mariadb.org/
[4]: https://www.mysql.com/
[5]: https://en.wikipedia.org/wiki/SQL
[6]: https://opensource.com/article/20/10/install-mariadb-and-mysql-linux
[7]: https://mariadb.org/download
[8]: https://publications.opengroup.org/c449
[9]: https://opensource.com/downloads/mariadb-mysql-cheat-sheet

View File

@ -0,0 +1,89 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: (wxy)
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (5 surprising ways I use Jupyter to improve my life)
[#]: via: (https://opensource.com/article/20/11/surprising-jupyter)
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
5 种令人惊讶的使用 Jupyter 的方式
======
> Jupyter 不仅仅是一个数据分析工具,让我们看看如何以最有创意的方式使用这个基于 Python 的软件。
!["太空中的电脑笔记本"[1]
[Jupyter][2] 项目提供了用 JupyterLab 和 Jupyter Notebook 等交互式编写软件的技术方式。这个软件通常用于数据分析但你可能不知道Jupyter 社区也没有想到),你可以用它做多少事情。
以下是我使用 Jupyter 的五大意想不到的创造性方法。
### 1、处理图像
在[图像编辑和处理][3]方面,有很多很好的开源工具 —— 从那些可以与 Photoshop 媲美的工具到实验性的 [Glimpse][4]。但即使有这么多选择,有时我还是不想离开 [Python][5] 的世界。
幸运的是Jupyter 是一个做轻量级图像处理的好选择。利用 Jupyter 直接将 [Pillow][6] 对象显示为图像的优势,让你可以尽情地对图片进行实验。我甚至还用它给孩子[做了一个涂色画][7]。
### 2、做一个 SSH 跳板遥控器
由于 JupyterLab 可以让你[上传和下载][8]文件、[编辑][9]文件,甚至[运行终端][10],所以它拥有制作 SSH 跳板环境所需的所有部件。
通过一些 SSH 转发魔法,你可以让 Jupyter 成为防火墙另一边的[远程控制台][11]。
### 3、开发 Web 应用程序
我最喜欢的使用 Jupyter 的方式之一是用于一种意想不到的软件开发。我[做了一次演讲][12],在演讲中,我使用 Jupyter Notebook 实时开发了一个 [Web 应用][13]。讲演的最后是一个简单的表单,它是 XSS 和 CSS 安全的,并包括一些轻量级的服务器端计算。
一个日常的 Jupyter 用户可能不会期望它是一个最棒的 Web 开发环境,但它是一个非常强大的环境。
### 4、从你喜欢的服务中提取报告
JupyterLab 中的数据分析是一种常见的用法,但<ruby>自我提升分析<rt>self-improvement analysis</rt></ruby>呢?
你可以使用 Jupyter 来[分析你的日历][14]。如果你最喜欢的服务允许 API 导出,甚至可以让你导出一个 CSV你可以将这些与你的日历进行关联。如果你发现你在社交媒体上发帖的时候你的日历上写着你应该和你的经理开会那 Jupyter 也救不了你!
### 5、开发游戏
对于扩大对 Jupyter Notebook 的期望值,我最喜欢的方式是和孩子一起建立一个游戏。我之前写过这方面的文章,有一个使用 [PursuedPyBear][16] 和 Jupyter [编写游戏][15]的分步教程。
在试图弄清游戏机制时,这种迭代式的游戏开发方法特别有用。能够在游戏中途改变规则(对不起,我必须得这样做)是一个改变游戏规则的方法。
你甚至可以使用 IPywidgets 来修改数字参数,就像[这个视频][17]所示。
### 下载电子书
JupyterLab 和 Jupyter Notebooks 提供了一个不可思议的实验环境。[下载这本指南][18],其中包含了以令人吃惊的方式使用 Jupyter 的教程。 
你是如何以创造性的方式使用它的?在下面的评论中分享你的最爱。
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/11/surprising-jupyter
作者:[Moshe Zadka][a]
选题:[lujun9972][b]
译者:[wxy](https://github.com/wxy)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/moshez
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/computer_space_graphic_cosmic.png?itok=wu493YbB (Computer laptop in space)
[2]: https://jupyter.org/
[3]: https://opensource.com/life/12/6/design-without-debt-five-tools-for-designers
[4]: https://glimpse-editor.github.io/
[5]: https://opensource.com/resources/python
[6]: https://pillow.readthedocs.io/en/stable/index.html
[7]: https://opensource.com/article/20/8/edit-images-python
[8]: https://jupyterlab.readthedocs.io/en/stable/user/files.html#uploading-and-downloading
[9]: https://jupyterlab.readthedocs.io/en/stable/user/files.html#opening-files
[10]: https://jupyterlab.readthedocs.io/en/stable/user/terminal.html
[11]: https://opensource.com/article/20/8/remote-management-jupyter
[12]: https://opensource.com/article/20/8/write-talk-using-jupyter-notebooks
[13]: https://github.com/moshez/interactive-web-development/blob/e31ae72d8cab7637d18bc734c4e8afc10c60251f/interactive-web-development.ipynb
[14]: https://opensource.com/article/20/9/analyze-your-life-jupyter
[15]: https://opensource.com/article/20/5/python-games
[16]: https://ppb.dev/
[17]: https://www.youtube.com/watch?v=JaTf_ZT7tE8
[18]: https://opensource.com/downloads/jupyter-guide