mariadb mysql_如何从控制台使用Mysql / MariaDB?

mariadb mysql

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 

Getting Help
Getting Help
获得帮助

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
Connect to Local MySQL/MariaDB Database Server
Connect to Local MySQL/MariaDB Database Server
连接到本地MySQL / MariaDB数据库服务器

连接到远程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;
Show MySQL/MariaDB Databases
Show MySQL/MariaDB Databases
显示MySQL / MariaDB数据库

As we can see from screenshot there is 6 databases.

从屏幕截图可以看到,有6个数据库。

LEARN MORE  How To Create a Database and Table In MySQL and MariaDB?
了解更多如何在MySQL和MariaDB中创建数据库和表?

创建数据库 (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;
Create Database
Create Database
创建数据库

选择要使用查询的数据库(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;
Select Database To Use Query
Select Database To Use Query
选择要使用查询的数据库

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;
Show Tables
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) );
Create Table
Create Table
建立表格

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;
Deleting/Dropping Table
Deleting/Dropping Table
删除/删除表

如何从控制台使用Mysql / MariaDB? 信息移植 (How To Use Mysql / MariaDB From Console? Infografic)

 How To Use Mysql / MariaDB From Console? Infografic
How To Use Mysql / MariaDB From Console? Infografic
如何从控制台使用Mysql / MariaDB? 信息移植

翻译自: https://www.poftut.com/how-to-use-mysql-mariadb-from-console/

mariadb mysql

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值