在 Ubuntu 系统上安装 MySQL

在 Ubuntu 系统上安装 MySQL 的步骤如下,涵盖从安装到基本配置的完整流程:

1. 更新系统软件包

在安装任何软件之前,建议先更新系统以确保软件包列表和依赖关系最新:

# 更新软件包列表

sudo apt update

 # 升级已安装的软件包(可选)

 sudo apt upgrade -y

2. 安装 MySQL 服务器

使用 apt 包管理器安装 MySQL 服务器:

# 安装 MySQL 服务器

sudo apt install mysql-server -y

3. 验证 MySQL 服务状态

安装完成后,检查 MySQL 服务是否正常运行:

# 查看 MySQL 服务状态

sudo systemctl status mysql

# 如果服务未运行,手动启动

sudo systemctl start mysql

预期输出

4. 安全配置 MySQL

运行安全脚本以增强 MySQL 的安全性:

sudo mysql_secure_installation

  • 交互式配置
    1. 设置 root 密码(如果安装时未设置)。
    2. 删除匿名用户(推荐选择 Y)。
    3. 禁止 root 用户远程登录(推荐选择 N)。
    4. 删除测试数据库(推荐选择 Y)。
    5. 重新加载权限表(推荐选择 Y)。

详细操作如下:

root@ubuntu:~# 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: 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: 100

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) : u

 ... skipping.

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) : no

 ... skipping.

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!

5. 登录 MySQL

使用 root 用户登录 MySQL 命令行工具:

root@ubuntu:~# mysql -uroot -pMax#3306

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 10

Server version: 8.0.19-0ubuntu5 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

4 rows in set (0.01 sec)

6、配置远程访问

默认情况下,MySQL 只允许本地访问。若需远程连接,请按以下步骤配置:

步骤 1:修改 MySQL 配置文件

Vim /etc/mysql/mysql.conf.d/mysqld.cnf

找到修改以下行:

bind-address = 127.0.0.1

替换为:

bind-address = 0.0.0.0

步骤 2:重启 MySQL 服务

sudo systemctl restart mysql

步骤 3:授权远程访问权限

登录 MySQL 并执行以下命令:

CREATE USER 'root'@'%' IDENTIFIED BY 'Max#3306';

-- 授予所有权限

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';

-- 使权限立即生效

FLUSH PRIVILEGES;

MYSQL启动命令

Systemctl start mysql

MYSQL停止命令

Systemctl stop mysql

MYSQL重启命令

Systemctl restart mysql

### 如何在 Ubuntu 系统安装 MySQL #### 更新软件包列表 为了确保获取最新的软件版本,在开始之前应更新系统的软件包索引。这可以通过运行以下命令完成: ```bash sudo apt-get update ``` 此操作会刷新本地的APT缓存并同步最新可用的软件包信息[^1]。 #### 安装 MySQL Server 通过APT包管理器可以直接安装MySQL服务器组件。使用如下命令来实现这一目标: ```bash sudo apt-get install mysql-server ``` 这条指令将会下载并安装MySQL服务端以及必要的依赖项到您的Ubuntu系统中。 另外一种方法涉及手动配置来自官方源的deb文件,这种方法可以更精确控制所安装的具体版本号。具体来说,您需要先加载`mysql-apt-config`这个DEB包,它包含了设置好的APT仓库链接信息。执行下面的shell脚本即可引入这些资源: ```bash dpkg -i mysql-apt-config_0.8.30-1_all.deb ``` 之后再次调用APT工具链重新构建数据库镜像清单,并最终触发实际的产品部署流程[^2]。 请注意无论采用哪种方式都可能被提示输入root账户密码或者确认某些默认选项设定值;按照屏幕上的指示一步步操作下去就可以顺利完成整个过程了! ```python import subprocess def install_mysql(): try: # Update package list and install MySQL server via APT. subprocess.run(['sudo', 'apt-get', 'update'], check=True) result = subprocess.run(['sudo', 'apt-get', 'install', '-y', 'mysql-server'], check=True) if result.returncode != 0: raise Exception("Failed to install MySQL using APT.") except subprocess.CalledProcessError as e: print(f"An error occurred during the installation process: {e}") if __name__ == "__main__": install_mysql() ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值