Skip to content
大纲

MySQL

MySQL 是一个开源的关系数据库管理系统,开发者为瑞典 MySQL AB 公司。目前 MySQL 被广泛地应用在 Internet 上的大中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了 MySQL 作为网站数据库。

MySQL 是数据库的一种,具有数据库的通用特征,同时,比起其他类型的数据库,它还具有自己鲜明的特点。

MySQL 是一个小型的开源的关系型数据库管理系统。与其他大型数据库管理系统例如 Oracle、DB2、SQL Server 等相比,MySQL 规模小,功能有限,但是它体积小、速度快、成本低,且它提供的功能对稍微复杂的应用已经够用,这些特性使得 MySQL 成为世界上最受欢迎的开放源代码数据库。

MySQL 是一种开放源代码的关系型数据库管理系统(RDBMS),MySQL 数据库系统使用最常用的数据库管理语言——结构化查询语言(SQL)进行数据库管理。

安装

通过 brew 安装 MySQL

sh
brew install mysql

启动 MySQL 服务器

sh
brew services start mysql

配置 MySQL 服务器

我们需要运行以下脚本配置 MySQL 服务器的安全性:

sh
mysql_secure_installation

在这个过程中,你可以设置 root 的密码,配置一些选项以增强 MySQL 服务器的安全性。你会看到如下类似的输出:

sh
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: Y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 25
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.

All done!

MySQL 服务器管理命令

brew 提供了实用的命令可以管理 MySQL 服务器。

  • brew services start mysql: 启动 MySQL 服务器,并设置为自启动。
  • brew services stop mysql: 停止 MySQL 服务器,并设置为不自启动。
  • brew services run mysql: 只启动 MySQL 服务器。
  • mysql.server start: 启动 MySQL 服务器。
  • mysql.server stop: 停止 MySQL 服务器。

连接 MySQL 数据库

当我们安装好 MySQL 服务器后,就可以使用任何客户端程序(例如 mysql 命令行客户端和 MySQL Workbench)连接到 MySQL 服务器。

命令行不需要安装额外的程序,图形界面的程序更易于使用。我们可以根据自己的实际情况选择使用命令行程序还是图形界面的客户端连接到 MySQL 服务器。

使用以下命令连接到 MySQL 服务器:

sh
# 其中 `username` 是你的 MySQL 用户名。执行该命令后,系统会提示你输入密码。
mysql -u <username> -p

-u root 意味着使用 root 用户连接到 MySQL 服务器; -p 指示 mysql 提示输入密码。

然后根据提示输入 root 帐户的密码,并按下回车键。验证通过后,显示以下输出代表进入了 MySQL 控制台:

sh
mysql>

连接成功后,使用 show databases; 显示当前服务器中的所有数据库:

sh
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.02 sec)

至此,使用 mysql 命令行客户端成功连接到了 MySQL 服务器,并查询了所有的服务器。

退出数据库连接:

sh
exit

备份与还原

备份数据库

sh
mysqldump -u <username> -p <database_name> > backup.sql

还原数据库

sh
# 还原之前要先清空指定的表,不然会报错主键冲突
TRUNCATE TABLE <>;

# 还原数据库中指定的表
mysql -u <username> -p <database_name> < backup.sql

导出某个表的数据

sh
mysqldump -u <username> -p <database_name> <table_name> > table.sql

导入某个表的数据

sh
mysql -u <username> -p <database_name> < table.sql

MySQL 8 更改身份验证插件

使用 nodejs 链接 mysql 8 时,会报如下错误:

Error: Client does not support authentication protocol requested by server; consider upgrading MySQL client

这个错误通常是由于客户端与服务器之间的身份验证协议不兼容所引起的。在 MySQL 8 中,默认的身份验证插件由 mysql_native_password 更改为 caching_sha2_password。如果你使用的是旧版本的 MySQL 客户端,它可能不支持新的身份验证协议,因此会导致这个错误。

找到 mysql 8 配置文件,一般在:

  • /opt/homebrew/etc
  • /etc/mysql
  • /usr/local/etc/mysql

在 mysql 8 配置文件中修改:

[mysqld]
default_authentication_plugin=mysql_native_password

修改完成后,重启 mysql 服务

brew services restart mysql

然后需要根据下面创建用户和权限的 sql 语句,在 mysql 中执行,创建基于旧的身份验证插件的用户,才能链接到 mysql 服务。

用户与权限

创建用户

sql
CREATE USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

给用户 root 权限

sql
GRANT ALL PRIVILEGES ON *.* TO 'new_user'@'localhost' WITH GRANT OPTION;

取消用户授权

sql
REVOKE ALL PRIVILEGES ON *.* FROM 'username'@'localhost' WITH GRANT OPTION;