wsl2(Ubuntu-22.04)配置mysql8.0

这篇教程详细介绍了在Ubuntu 22.04上安装MySQL8.0的过程,包括使用sudo apt install命令快速安装,启动MySQL服务,通过service命令查看和管理服务状态。同时,教程还解决了root用户无法本地登录的问题,通过修改认证方式和密码,以及更新root用户的host权限为%,实现了远程访问。最后,提到了卸载MySQL的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该教程转载原地址:https://blog.csdn.net/gandongusa/article/details/122964656
原博主为ubuntu-20.04版本,本博主为ubuntu-22.04版本,因此本方法具备通用性。

1. MySQL8.0安装

因为ubuntu内置了最新的mysql8.0版本,因此快捷安装就直接这么整就行了,如果需要安装mysql5.7可以去看看官网的二进制安装。
如果需要在docker中安装可以去看docker相关拉镜像操作。

sudo apt install mysql-server

1.1 启动MySQL服务

WSL子系统Ubuntu中不包含systemctl命令,使用service命令

# 查看MySQL服务运行状态
 sudo service mysql status
 * MySQL is stopped.
 
 #启动MySQL服务
 sudo service mysql start
 * Starting MySQL database server mysqld 
 * su: warning: cannot change directory to /nonexistent: No such file or directory   [ OK ]

查看MySQL是否启动成功

# 查看MySQL服务运行状态
ps -ef | grep mysql
 
mysql    13613   120  0 15:49 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe
mysql    13760 13613  0 15:49 ?        00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --log-error=/var/log/mysql/error.log --pid-file=ZHOU-PC.pid
zhou     13828   121  0 15:51 pts/0    00:00:00 grep --color=auto mysql
sudo service mysql status

 * /usr/bin/mysqladmin  Ver 8.0.28-0ubuntu0.20.04.3 for Linux on x86_64 ((Ubuntu))
Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Server version          8.0.28-0ubuntu0.20.04.3
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/run/mysqld/mysqld.sock
Uptime:                 3 hours 1 min 31 sec

Threads: 2  Questions: 8  Slow queries: 0  Opens: 117  Flush tables: 3  Open tables: 36  Queries per second avg: 0.000

1.2 MySQL登录

mysql -u root -p

Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

1.2.1 第一种登录方法

root用户没有设置密码,不能从本地登录,可以使用sudo命令进入,此时不需要输入密码即可进入

sudo mysql -u root -p

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.28-0ubuntu0.20.04.3 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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>

1.2.2 第二种登录方法

MySQL在安装时会创建很多默认用户,其中就包含一个 debian-sys-maint,并且创建了该用户的随机密码,存储该用户信息的文件位于 /etc/mysql/debian.cnf文件中。

tree /etc/mysql
.
├── conf.d
│   ├── mysql.cnf
│   └── mysqldump.cnf
├── debian-start
├── debian.cnf
├── my.cnf -> /etc/alternatives/my.cnf
├── my.cnf.fallback
├── mysql.cnf
└── mysql.conf.d
    ├── mysql.cnf
    └── mysqld.cnf
sudo cat debian.cnf

# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = YEd1uhCL4DsN5NBD
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = YEd1uhCL4DsN5NBD
socket   = /var/run/mysqld/mysqld.sock

可以利用debian-sys-main用户登录MySQL

mysql -u debian-sys-maint -pYEd1uhCL4DsN5NBD

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 14
Server version: 8.0.28-0ubuntu0.20.04.3 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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>

1.3 更改root用户密码

MySQL中用户相关信息存储于mysql数据库中,需要现转换到mysql数据库,分别用到如下命令
show databases; use database_name; show tables;

mysql> show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
mysql> use mysql;

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;

+------------------------------------------------------+
| Tables_in_mysql                                      |
+------------------------------------------------------+
| columns_priv                                         |
| component                                            |
| db                                                   |
| default_roles                                        |
| engine_cost                                          |
| func                                                 |
| general_log                                          |
| global_grants                                        |
| gtid_executed                                        |
| help_category                                        |
| help_keyword                                         |
| help_relation                                        |
| help_topic                                           |
| innodb_index_stats                                   |
| innodb_table_stats                                   |
| password_history                                     |
| plugin                                               |
| procs_priv                                           |
| proxies_priv                                         |
| replication_asynchronous_connection_failover         |
| replication_asynchronous_connection_failover_managed |
| replication_group_configuration_version              |
| replication_group_member_actions                     |
| role_edges                                           |
| server_cost                                          |
| servers                                              |
| slave_master_info                                    |
| slave_relay_log_info                                 |
| slave_worker_info                                    |
| slow_log                                             |
| tables_priv                                          |
| time_zone                                            |
| time_zone_leap_second                                |
| time_zone_name                                       |
| time_zone_transition                                 |
| time_zone_transition_type                            |
| user                                                 |
+------------------------------------------------------+
37 rows in set (0.00 sec)

存储用户相关信息的内容位于user表格中,可以查看表格结构

mysql> desc user;

+--------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field                    | Type                              | Null | Key | Default               | Extra |
+--------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host                     | char(255)                         | NO   | PRI |                       |       |
| User                     | char(32)                          | NO   | PRI |                       |       |
| Select_priv              | enum('N','Y')                     | NO   |     | N                     |       |
| Insert_priv              | enum('N','Y')                     | NO   |     | N                     |       |
| Update_priv              | enum('N','Y')                     | NO   |     | N                     |       |
| Delete_priv              | enum('N','Y')                     | NO   |     | N                     |       |
| Create_priv              | enum('N','Y')                     | NO   |     | N                     |       |
| Drop_priv                | enum('N','Y')                     | NO   |     | N                     |       |
| Reload_priv              | enum('N','Y')                     | NO   |     | N                     |       |
| Shutdown_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Process_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| File_priv                | enum('N','Y')                     | NO   |     | N                     |       |
| Grant_priv               | enum('N','Y')                     | NO   |     | N                     |       |
| References_priv          | enum('N','Y')                     | NO   |     | N                     |       |
| Index_priv               | enum('N','Y')                     | NO   |     | N                     |       |
| Alter_priv               | enum('N','Y')                     | NO   |     | N                     |       |
| Show_db_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| Super_priv               | enum('N','Y')                     | NO   |     | N                     |       |
| Create_tmp_table_priv    | enum('N','Y')                     | NO   |     | N                     |       |
| Lock_tables_priv         | enum('N','Y')                     | NO   |     | N                     |       |
| Execute_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| Repl_slave_priv          | enum('N','Y')                     | NO   |     | N                     |       |
| Repl_client_priv         | enum('N','Y')                     | NO   |     | N                     |       |
| Create_view_priv         | enum('N','Y')                     | NO   |     | N                     |       |
| Show_view_priv           | enum('N','Y')                     | NO   |     | N                     |       |
| Create_routine_priv      | enum('N','Y')                     | NO   |     | N                     |       |
| Alter_routine_priv       | enum('N','Y')                     | NO   |     | N                     |       |
| Create_user_priv         | enum('N','Y')                     | NO   |     | N                     |       |
| Event_priv               | enum('N','Y')                     | NO   |     | N                     |       |
| Trigger_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| Create_tablespace_priv   | enum('N','Y')                     | NO   |     | N                     |       |
| ssl_type                 | enum('','ANY','X509','SPECIFIED') | NO   |     |                       |       |
| ssl_cipher               | blob                              | NO   |     | NULL                  |       |
| x509_issuer              | blob                              | NO   |     | NULL                  |       |
| x509_subject             | blob                              | NO   |     | NULL                  |       |
| max_questions            | int unsigned                      | NO   |     | 0                     |       |
| max_updates              | int unsigned                      | NO   |     | 0                     |       |
| max_connections          | int unsigned                      | NO   |     | 0                     |       |
| max_user_connections     | int unsigned                      | NO   |     | 0                     |       |
| plugin                   | char(64)                          | NO   |     | caching_sha2_password |       |
| authentication_string    | text                              | YES  |     | NULL                  |       |
| password_expired         | enum('N','Y')                     | NO   |     | N                     |       |
| password_last_changed    | timestamp                         | YES  |     | NULL                  |       |
| password_lifetime        | smallint unsigned                 | YES  |     | NULL                  |       |
| account_locked           | enum('N','Y')                     | NO   |     | N                     |       |
| Create_role_priv         | enum('N','Y')                     | NO   |     | N                     |       |
| Drop_role_priv           | enum('N','Y')                     | NO   |     | N                     |       |
| Password_reuse_history   | smallint unsigned                 | YES  |     | NULL                  |       |
| Password_reuse_time      | smallint unsigned                 | YES  |     | NULL                  |       |
| Password_require_current | enum('N','Y')                     | YES  |     | NULL                  |       |
| User_attributes          | json                              | YES  |     | NULL                  |       |
+--------------------------+-----------------------------------+------+-----+-----------------------+-------+
51 rows in set (0.00 sec)
mysql> select user,host,plugin from user;

+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| debian-sys-maint | localhost | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | auth_socket           |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)bash

上面的localhost说明只允许本地登录

MySQL8.0 中用户默认认证方式为 caching_sha2_password,虽然安全性有保障,但是对于使用本地或者远程连接都造成了一定困扰。因此可以将用户认证方式改为MySQL5.X常用的mysql_native_password方式,只有此种方式才能在MySQL8.0中更改用户密码。

mysql> alter user 'root'@'localhost' identified with mysql_native_password by '0';
Query OK, 0 rows affected (0.01 sec)

1.4 刷新缓存

输入flush privileges,刷新数据库。否则会保留在缓存中。

mysql> flush privileges;

Query OK, 0 rows affected (0.02 sec)

1.5 重启MySQL服务

退出mysql环境可以通过Ctrl+D或者输入quit退出回到Ubuntu环境。

mysql> ^DBye

mysql> quit
Bye

重新进入mysql环境就可以不用sudo在前面,直接使用mysql -u root -p即可
(由于本博主是在xshell中连接的,这里不sudo还是进不去,因为内置ban了su root,这里如果大家有问题留意下,不影响后面数据库连接的,就是进不去=3=)

mysql -u root -p
 
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.28-0ubuntu0.20.04.3 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> ;

重复上面的操作可以查看root用户认证方式

mysql> select user,host,plugin from user;

+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| debian-sys-maint | localhost | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)

此时的root用户认证已经改为mysql_native_password方式。

1.6 设置root用户的远程访问

此时root用户的host属性仍然是localhost,也就是只能从本地访问,因此可以将root用户的访问权限由本地改为本地和外部都可以访问,将host的值由localhost改为 %。

mysql> update user set user.host='%' where user.user='root';

Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

输入flush privileges,刷新数据库。否则会保留在缓存中。

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)
mysql> select user,host,plugin from user;

+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| root             | %         | mysql_native_password |
| debian-sys-maint | localhost | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)

此时root用户的host访问权限已经改为本地和外部都可以。
重启MySQL服务,即可实现远程连接。
WSL中的Ubuntu子系统访问可以直接使用127.0.0.1或localhost进行访问。

1.7 修改MySQL配置文件实现真正的远程访问

此时使用127.0.0.1或者localhost连接都没有问题,但是在外部一旦换成Ubuntu真正的IP地址访问就会报错。
在这里插入图片描述
这时还需要修改MySQL配置文件中的相关配置项 /etc/mysql//mysql.conf.d/mysqld.cnf

在这里插入图片描述
修改 bind-address = 0.0.0.0 。
重启MySQL后,再次通过IP地址远程连接。

在这里插入图片描述

2 MySQL8.0卸载

 sudo apt purge mysql-*
 sudo rm -rf /etc/mysql/ /var/lib/mysql
 sudo apt autoremove
 sudo apt autoclean
<think>好的,我需要帮助用户在Docker Desktop和WSL2Ubuntu环境中设置和运行Hyperf框架。首先,我需要理解用户的需求:他们想在WSL2Ubuntu子系统中使用Docker Desktop来配置Hyperf微服务框架。用户已经引用了两篇关于Docker在WSL2中的配置的文章,可能需要结合这些信息。 首先,用户需要确认Docker Desktop已经正确配置并集成了WSL2Ubuntu。根据引用[^1],Docker Desktop可以通过设置启用与WSL发行版的集成,所以用户需要确保在Docker Desktop的设置中勾选了Ubuntu 22.04的集成选项。然后,用户可能需要在Ubuntu子系统中安装必要的依赖,比如Docker CLI和Compose插件,或者根据引用[^2]中的方法,在不安装Docker Desktop的情况下使用Docker,但这里用户已经安装了Docker Desktop,所以应该直接利用其集成功能。 接下来,Hyperf框架通常需要PHP环境、Swoole扩展以及Composer等工具。用户需要创建一个Docker镜像,其中包含这些组件。可能需要使用官方的PHP镜像作为基础,然后安装Swoole和其他扩展。此外,Hyperf项目可能需要配置Dockerfile和docker-compose.yml文件来定义服务、网络和卷。 用户可能还需要处理端口映射、持久化数据(比如数据库)以及环境变量的配置。例如,在docker-compose.yml中定义Hyperf服务,并链接到MySQL或Redis等其他服务。同时,需要确保Swoole的协程配置正确,避免在Docker环境中出现兼容性问题。 另外,用户可能遇到权限问题,特别是在WSL2中挂载卷时的文件权限问题。可能需要调整Docker的配置或文件权限设置。此外,性能优化也是一个考虑点,比如使用WSL2的内存和CPU分配,或者调整Docker的守护进程参数。 最后,用户需要测试Hyperf应用的运行情况,确保服务能够正常启动,并且能够处理请求。可能需要通过curl或浏览器访问本地端口来验证。如果遇到问题,查看Docker容器的日志进行调试,或者检查Hyperf的配置文件是否正确。 总结步骤大致为:确认Docker环境配置正确→创建Hyperf项目→编写Dockerfile和docker-compose.yml→构建镜像并启动容器→测试和调试。需要详细指导每个步骤的具体操作,包括命令和配置示例,确保用户能够逐步执行。</think>### 在 Docker Desktop with WSL2Ubuntu 上设置和运行 Hyperf 微服务框架 #### 步骤 1: 配置 Docker Desktop 与 WSL2 集成 1. **启用 WSL2 后端** - 在 Windows 中打开 PowerShell,执行: ```powershell wsl --set-default-version 2 ``` - 在 Docker Desktop 的 `Settings > General` 中勾选 *Use WSL 2 based engine*。 2. **集成 Ubuntu 子系统** - 在 Docker Desktop 的 `Settings > Resources > WSL Integration` 中启用 Ubuntu 22.04 的集成选项。 #### 步骤 2: 准备 Ubuntu 子系统环境 1. **更新系统并安装依赖** 在 Ubuntu 终端中执行: ```bash sudo apt update && sudo apt upgrade -y sudo apt install git curl unzip -y ``` 2. **验证 Docker 集成** 确保 Docker 命令可用: ```bash docker --version docker-compose --version # 若未安装,需通过 Docker Desktop 启用 Compose 插件 ``` #### 步骤 3: 创建 Hyperf 项目 1. **通过 Composer 创建项目** ```bash docker run --rm -v $(pwd):/app composer create-project hyperf/hyperf-skeleton hyperf-demo ``` - 此命令使用 Composer 容器初始化 Hyperf 项目,避免本地 PHP 环境依赖。 2. **进入项目目录** ```bash cd hyperf-demo ``` #### 步骤 4: 配置 Docker 容器 1. **编写 Dockerfile** 在项目根目录创建 `Dockerfile`,内容如下: ```dockerfile FROM php:8.1-swoole RUN apt update && apt install -y \ libssl-dev \ libcurl4-openssl-dev \ libpq-dev \ && docker-php-ext-install pdo_mysql \ && pecl install redis \ && docker-php-ext-enable redis WORKDIR /var/www COPY . . RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \ && composer install --no-dev --optimize-autoloader EXPOSE 9501 CMD ["php", "bin/hyperf.php", "start"] ``` 2. **编写 docker-compose.yml** 创建 `docker-compose.yml`,定义服务和网络: ```yaml version: '3' services: hyperf: build: . ports: - "9501:9501" volumes: - .:/var/www networks: - hyperf-net mysql: image: mysql:8.0 environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: hyperf volumes: - mysql-data:/var/lib/mysql networks: - hyperf-net volumes: mysql-data: networks: hyperf-net: driver: bridge ``` #### 步骤 5: 构建并启动容器 1. **构建镜像** ```bash docker-compose build ``` 2. **启动服务** ```bash docker-compose up -d ``` 3. **验证服务状态** ```bash curl http://localhost:9501 ``` 若返回默认的 Hyperf 欢迎页,则配置成功。 #### 常见问题处理 - **权限问题**: 如果挂载卷导致文件权限错误,在 Ubuntu 中执行: ```bash sudo chown -R $USER:$USER . ``` - **Swoole 兼容性**: 确保 Dockerfile 使用与 Hyperf 兼容的 Swoole 版本(官方镜像通常已适配)。 --- ###
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值