编者按:快速入手才是硬道理!
【免责声明】本公众号文章仅代表个人观点,与任何公司无关,仅供参考。

编辑|SQL和数据库技术(ID:SQLplusDB)
前面我们介绍了【安装虚拟机和Linux环境(基于Multipass和Virtual Box的Ubuntu)】。
本文介绍如何通过multipass 快速安装和配置 MySQL数据库。
创建虚拟机
通过multipass launch 命令创建一台虚拟机。
C:\Users\Administrator>multipass listNo instances found.C:\Users\Administrator>multipass launch -n mysql-vmLaunched: mysql-vmC:\Users\Administrator>multipass listName State IPv4 Imagemysql-vm Running N/A Ubuntu 22.04 LTSC:\Users\Administrator>multipass info mysql-vmName: mysql-vmState: RunningIPv4: N/ARelease: Ubuntu 22.04.2 LTSImage hash: 345fbbb6ec82 (Ubuntu 22.04 LTS)Load: 0.08 0.02 0.01Disk usage: 1.4G out of 4.7GMemory usage: 178.5M out of 969.5MMounts: --C:\Users\Administrator>
连接这台虚拟机。
C:\Users\Administrator>multipass shell mysql-vmWelcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-67-generic x86_64)* Documentation: https://help.ubuntu.com* Management: https://landscape.canonical.com* Support: https://ubuntu.com/advantageSystem information as of Sun Mar 26 08:51:26 CST 2023System load: 0.0 Processes: 86Usage of : 30.9% of 4.67GB Users logged in: 0Memory usage: 21% IPv4 address for enp0s3: 10.0.2.15Swap usage: 0%* Introducing Expanded Security Maintenance for Applications.Receive updates to over 25,000 software packages with yourUbuntu Pro subscription. Free for personal use.https://ubuntu.com/proExpanded Security Maintenance for Applications is not enabled.0 updates can be applied immediately.Enable ESM Apps to receive additional future security updates.See https://ubuntu.com/esm or run: sudo pro statusThe list of available updates is more than a week old.To check for new updates run: sudo apt updateTo run a command as administrator (user "root"), use "sudo <command>".See "man sudo_root" for details.ubuntu@mysql-vm:~$
安装MySQL
使用的主要命令:
sudo apt updatesudo apt show mysql-serversudo apt install mysql-serversudo systemctl start mysql.service
1. 升级apt仓库为最新
ubuntu@mysql-vm:~$ sudo apt update
2. 安装mysql-server
ubuntu@mysql-vm:~$ sudo apt install mysql-server
查看服务器的状态。
ubuntu@mysql-vm:~$ sudo systemctl start mysql.serviceubuntu@mysql-vm:~$ sudo systemctl status mysql.service● mysql.service - MySQL Community ServerLoaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)Active: active (running) since Sun 2023-03-26 09:00:03 CST; 27s agoProcess: 4074 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)Main PID: 4084 (mysqld)Status: "Server is operational"Tasks: 39 (limit: 1116)Memory: 358.1MCPU: 1.123sCGroup: /system.slice/mysql.service└─4084 /usr/sbin/mysqldMar 26 09:00:02 mysql-vm systemd[1]: Starting MySQL Community Server...Mar 26 09:00:03 mysql-vm systemd[1]: Started MySQL Community Server.ubuntu@mysql-vm:~$
如果未启动,通过下面的命令启动。
sudo systemctl start mysql.service
初始化配置MySQL
进入mysql
ubuntu@mysql-vm:~$ sudo mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 8Server version: 8.0.32-0ubuntu0.22.04.2 (Ubuntu)Copyright (c) 2000, 2023, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.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.00 sec)mysql>
修改root用户的密码
修改root用户的密码为rootroot。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'rootroot';Query OK, 0 rows affected (0.01 sec)mysql>mysql> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.01 sec)
查看用户信息。
mysql> select User,authentication_string,Host from mysql.user;+------------------+------------------------------------------------------------------------+-----------+| User | authentication_string | Host |+------------------+------------------------------------------------------------------------+-----------+| debian-sys-maint | $A$005$;+GAh1A?rIAa^W=tG4leu19sgs1gd8MOLJGcPMLGRHLAuf5SoGIJg4rsoz. | localhost || mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | localhost || mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | localhost || mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | localhost || root | *6C362347EBEAA7DF44F6D34884615A35095E80EB | localhost |+------------------+------------------------------------------------------------------------+-----------+5 rows in set (0.00 sec)mysql>
使用root登录mysql
退出并重新登录mysql
mysql> exitByeubuntu@mysql-vm:~$ mysql -u root -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 9Server version: 8.0.32-0ubuntu0.22.04.2 (Ubuntu)Copyright (c) 2000, 2023, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
其他参考:服务相关操作
启动和停止mysql服务的命令如下。
systemctl start mysld.servicesystemctl stop mysld.servicesystemctl status mysld.service
service mysql status例:ubuntu@mysql-vm:~$ service mysql status● mysql.service - MySQL Community ServerLoaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)Active: active (running) since Mon 2022-09-12 20:52:54 CST; 7min agoProcess: 608 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)Main PID: 717 (mysqld)Status: "Server is operational"Tasks: 37 (limit: 1131)Memory: 423.6MCGroup: /system.slice/mysql.service└─717 /usr/sbin/mysqldSep 12 20:52:49 mysql-vm systemd[1]: Starting MySQL Community Server...Sep 12 20:52:54 mysql-vm systemd[1]: Started MySQL Community Server.```
这样一个简单的MySQL环境就算构筑完成了。
MySQL相关:
手把手教你在Windows 10安装MySQL 8.0(详细图文)
MySQL入门:Linux 6 RPM方式安装MySQL 8.0
Oracle 和 Mysql 的索引在Null字段上处理的异同
文章转载自SQL和数据库技术,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




