mariadb mysql
Mysql is one of the most popular databases in IT world. It is open source and have rich features were paid databases provides. MariaDB is fork of the MySQL and very similar to it. We have already looked how to login and do simple steps in this post
Mysql是IT界最受欢迎的数据库之一。 它是开源的,具有付费数据库提供的丰富功能。 MariaDB是MySQL的分支,并且与它非常相似。 我们已经看过了如何在登录并做简单的步骤,这个帖子
获得帮助(Getting Help)
In order to connect to a MySQL or MariaDB database server we will use mysql
command. mysql
command provides a lot of different options and features. We can list help information about mysql command with the -I
option like below.
为了连接到MySQL或MariaDB数据库服务器,我们将使用mysql
命令。 mysql
命令提供了许多不同的选项和功能。 我们可以使用-I
选项列出有关mysql命令的帮助信息,如下所示。
$ mysql -I
As we can see hep information can be also listed with -?
or --help
options.
如我们所见,hep信息也可以用-?
列出-?
或--help
选项。
连接到本地MySQL / MariaDB数据库服务器 (Connect to Local MySQL/MariaDB Database Server)
Connecting to the local MySQL server is very easy. We will just use sudo
command with the user root
. We will provide root
user with the -u
option like below. If we do not provide the sudo
we will get ERROR 1698 (28000):Access denied for user 'root'@'localhost'
which is permission related error.
连接到本地MySQL服务器非常容易。 我们将只对root
用户使用sudo
命令。 我们将为root
用户提供-u
选项,如下所示。 如果不提供sudo
,则会收到ERROR 1698 (28000):Access denied for user 'root'@'localhost'
这是与权限相关的错误。
$ sudo mysql -u root
连接到远程MySQL / MariaDB数据库服务器(Connect to Remote MySQL/MariaDB Database Server)
We can also connect to the remote MySQL server with the mysql
command too. We will provide the remote system IP address or host name with the -h
option. If we are providing host name we make sure that DNS works properly.
我们也可以使用mysql
命令连接到远程MySQL服务器。 我们将使用-h
选项提供远程系统IP地址或主机名。 如果我们提供主机名,请确保DNS正常运行。
$ mysql -h 192.168.1.20 -u root
显示MySQL / MariaDB数据库 (Show MySQL/MariaDB Databases)
There is default databases like information_schema,mysql,… and user added databases. We will list currently existing databases with the show databases
command. We will also add ;
to the each command in order to set command end.
有默认数据库,例如information_schema,mysql等,以及用户添加的数据库。 我们将使用show databases
命令列出当前存在的数据库。 我们还将添加;
到每个命令以设置命令结尾。
> show databases;
As we can see from screenshot there is 6 databases.
从屏幕截图可以看到,有6个数据库。
创建数据库 (Create Database)
If database we want to use is not exist we can create a new database with the create database
command by providing database name. In this example we will create a database named poftut
.
如果我们要使用的数据库不存在,我们可以使用create database
命令通过提供数据库名称来创建新数据库。 在此示例中,我们将创建一个名为poftut
的数据库。
> create database poftut;
选择要使用查询的数据库(Select Database To Use Query)
Before issuing queries we need to select the database we want to query. If not our query will not run on any database. We will use use
command by providing database we want to query. In this example we will use poftut
as database.
在发出查询之前,我们需要选择要查询的数据库。 如果不是这样,我们的查询将不会在任何数据库上运行。 我们将通过提供要查询的数据库来使用use
命令。 在此示例中,我们将使用poftut
作为数据库。
> use poftut;
As we can see that after successful database selection we will get Database changed
message.
我们可以看到,成功选择数据库后,我们将收到“ Database changed
消息。
显示表格 (Show Tables)
show command can be used to show tables too;
show命令也可以用于显示表;
> show tables;
As we can see there is no table in the current database named poftut
我们可以看到,当前数据库中没有名为poftut
建立表格 (Create Table)
We need to create a table we will provide table name and field names with their types. We will create a table named Persons with fields PersoneID type of integer, …
我们需要创建一个表,我们将提供表名和字段名及其类型。 我们将创建一个名为Persons的表,其字段PersoneID类型为integer ,…
> CREATE TABLE Persons ( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) );
As we can see from CREATE TABLE command result that Query OK
which means we have successfully completed table creation.
从CREATE TABLE命令的结果可以看出, Query OK
表示我们已经成功完成了表的创建。
删除/删除表 (Deleting/Dropping Table)
We will delete table completely not just the row. Drop Table is the command with the table name.
我们将完全删除表而不仅仅是行。 删除表是带有表名的命令。
> DROP Table Persons;
如何从控制台使用Mysql / MariaDB? 信息移植 (How To Use Mysql / MariaDB From Console? Infografic)
翻译自: https://www.poftut.com/how-to-use-mysql-mariadb-from-console/
mariadb mysql