Ubuntu Server 19.10 安装 MySQL

安装之前先更新一下源,防止找不到源安装失败:

sudo apt-get update

1. 执行安装命令(默认安装最新版本)

sudo apt-get install mysql-server

2. 安装完成后查看是否运行

ps -e | grep mysql

是不是发现没有让输入密码的地方,这一点和 Ubuntu for 桌面版 的还是有区别的,Ubuntu Server 安装的话是不提示输入密码的,但是 MySQL 为我们提供了其他方法,让你完成对 MySQL 的安全设置。

【注】虽然没有提示输入密码,但是可以通过匿名登录的方式进行访问。如果是 root 用户只需要输入 mysql 回车即可进入数据库,如果不是root用户,需要通过 sudo mysql 进行匿名登录。想要对数据库进行通用的配置看以下操作:

# 执行 MySQL 安全配置命令,并进行相关配置
sudo mysql_secure_installation
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: # 如果是服务器的话建议安装插件,本机使用的话,直接回车即可。

# 设置 root 用户的密码
Please set the password for root here.

New password:  # 本机的话直接默认 123456 即可
Re-enter new password:  #重新输入密码

# 这就是默认没有提示设置密码的原因
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.

# 是否禁止 root 进行远程登录数据库(这个看自己的实际需求)
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :  # 因为我的是本地测试数据库,所以直接回车即可
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 的配置工作,接下来我们就可以连接数据库并进行操作。

【注】sudo mysql_secure_installation 命令也可以用来以后重新初始化 MySQL,比如忘记 root 密码、设置不允许外网通过 root 用户连接数据库等。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注