MySQL or MariaDB database servers can be managed in different ways. Generally, GUI tools are provided because of their easiness. But in some cases command line connection may be required to connect and management MySQL and MariaDB database server.
MySQL或MariaDB数据库服务器可以通过不同的方式进行管理。 通常,由于其易用性而提供了GUI工具。 但是在某些情况下,可能需要命令行连接才能连接和管理MySQL和MariaDB数据库服务器。
为Ubuntu,Debian,Kali,Mint安装MySQL / MariaDB客户端 (Install MySQL/MariaDB Client For Ubuntu, Debian, Kali, Mint)
We will start by installing MySQL/MariaDB command line client in the deb based distributions like Ubuntu, Debian, Kali, Mint etc. The package is named as mysql-client
which we will install with the apt
command like below.
我们将从在基于deb的发行版(如Ubuntu,Debian,Kali,Mint等)中安装MySQL / MariaDB命令行客户端开始。该软件包名为mysql-client
,将使用以下apt
命令进行安装。
$ sudo apt install mysql-client
为Fedora,CentOS,RedHat安装MySQL / MariaDB客户端(Install MySQL/MariaDB Client For Fedora, CentOS, RedHat)
In the rpm based distributions like Fedora, CentOS, RedHat can be installed with the yum command like below. Also alternatively dnf can be used like below.
在Fedora,CentOS等基于rpm的发行版中,可以使用yum命令安装RedHat,如下所示。 另外,也可以像下面这样使用dnf。
$ sudo dnf install community-mysql
OR
要么
$ sudo yum install community-mysql
从命令行连接MySQL / MariaDB数据库(Connect MySQL/MariaDB Database From Command Line)
If MySQL/MariaDB database is installed on the local system we can use mysql
tool to connect. We can use mysql command with the sudo command for Linux systems like Ubuntu, Debian, Fedora, Mint, CentOS etc.
如果在本地系统上安装了MySQL / MariaDB数据库,我们可以使用mysql
工具进行连接。 对于Linux系统(例如Ubuntu,Debian,Fedora,Mint,CentOS等),我们可以将mysql命令与sudo命令一起使用。
$ sudo mysql
OR for Windows
或Windows
> mysql
从命令行连接指定用户名MySQL / MariaDB数据库(Connect Specifying Username MySQL/MariaDB Database From Command Line)
Default behavior of the mysql command is using the current user name. We can provide the user name explicitly with the -u
option and the user name. In this example, we will connect with the user name root
mysql命令的默认行为是使用当前用户名。 我们可以使用-u
选项和用户名显式地提供用户名。 在此示例中,我们将使用用户名root连接
$ sudo mysql -u root
通过命令行连接指定用户名和密码MySQL / MariaDB数据库(Connect Specifying Username and Password MySQL/MariaDB Database From Command Line)
Up to now, we have connected to the local MySQL/MariaDB database server without explicitly specifying the password. If we want to connect a remote database server we have to specify the password for the given user. We can also specify the remote server hostname or IP address with the -h
option like below.
到目前为止,我们已经连接到本地MySQL / MariaDB数据库服务器,而没有显式指定密码。 如果要连接远程数据库服务器,则必须指定给定用户的密码。 我们还可以使用-h
选项指定远程服务器的主机名或IP地址,如下所示。
$ mysql -u root -h 192.168.142.144
在不连接远程MySQL / MariaDB数据库的情况下运行SQL (Run SQL Without Connecting Remote MySQL/MariaDB Database)
mysql command provides different features like running SQL statements remotely without using the mysql shell. We can run the sql clause with the -e
option like below.
mysql命令提供了不同的功能,例如无需使用mysql shell即可远程运行SQL语句。 我们可以使用-e
选项运行sql子句,如下所示。
$ sudo mysql -u root -e "SELECT VERSION();"
从命令行显示数据库 (Show Databases From Command Line)
If we want to list databases stored on the MySQL/MariaDB database without connecting them explicitly and using mysql shell we can use show databases;
command like below.
如果我们要列出存储在MySQL / MariaDB数据库中的数据库而不用显式连接它们并使用mysql shell,则可以使用show databases;
命令如下。
$ sudo mysql -u root -e "show databases"
从命令行指定默认数据库 (Specify The Default Database From Command Line)
As we use command line tool mysql to connect and manage MySQL/MariaDB databases we need to specify the default database name in order to run SQL queries. We will use use
statement with the database name in order to specify the default database the SQL queries run.
当我们使用命令行工具mysql连接和管理MySQL / MariaDB数据库时,我们需要指定默认数据库名称才能运行SQL查询。 我们将使用带有数据库名称的use
语句,以指定SQL查询运行的默认数据库。
$ sudo mysql -u root -e "use sys;show tables;"
从远程MySQL / MariaDB数据库服务器上的命令行运行SQL脚本或SQL文件 (Run SQL Script or SQL File From Command Line On Remote MySQL/MariaDB Database Server)
SQL scripts and statements can be stored in the SQL script file. We can run this script files without copy and paste just providing the file and redirecting the content of the file. We have the following SQL file content which is sqlscript.sql .
SQL脚本和语句可以存储在SQL脚本文件中。 我们可以仅提供文件并重定向文件内容而无需复制和粘贴来运行该脚本文件。 我们具有以下SQL文件内容sqlscript.sql。
select version();
show databases;
use sys;
select * from host_summary;
We have to also specify the database name as the last parameter to the mysql command which is sys
in this case.
我们还必须指定数据库名称作为mysql命令的最后一个参数,在本例中为sys
。
$ sudo mysql -u root sys < sqlscript.sql
翻译自: https://www.poftut.com/how-to-connect-mysql-mariadb-server-from-command-line-on-linux-and-windows/