阿里云的便宜机器实在弱爆了,有一次编译个 PHP 都能宕机,因此,编译 MYSQL 的事情,我也不怎么想了,直接 apt-get 吧...

安装 mysql 那是相当的简单,基本上两个步骤就够了:安装&安全配置。 

1. 安装 mysql

一行命令搞定

apt-get install mysql-server

 

2. 配置 mysql

2.1. 执行安全配置命令

~# mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN 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 plugin?

 

2.2. 设置密码强度&密码

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: 1
Please set the password for root here.

New password: 

Re-enter new password: 
Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

 

2.3. 移除匿名用户

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.

 

2.4. 禁止 root 远程访问

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.

 

2.5. 移除测试数据库

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.

 

2.6. 刷新权限&安装成功

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! 

 

2.7. 重置 root 密码

很奇怪的一点是, 即使我安装时设置了密码,仍然可以任意密码或者空密码登录系统。百思不得其解,谷歌是个好东西,解决方案可参考:

https://blog.csdn.net/qq_38737992/article/details/81090373

 

3. mysql 常用命令

# 连接 mysql
mysql -u用户名 -p密码 数据库名

#显示数据库列表
mysql>show databases;

#选择数据库
mysql>use 数据库名;

#显示table列表
mysql>show tables;

#显示 table 表结构
mysql>desc 表名;

#备份本地数据库
mysqldump -u用户名 -p密码 数据库名 > 备份文件路径\备份文件名称

#本地恢复数据库
mysqldump -u用户名 -p密码 数据库名 < 备份文件路径\备份文件名称