暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

一分钟安装和配置 MySQL(基于Multipass 和Ubuntu 20.04)

1521

编者按:快速入手才是硬道理

【免责声明】本公众号文章仅代表个人观点,与任何公司无关,仅供参考。


编辑|SQL和数据库技术(ID:SQLplusDB)


前面我们介绍了【安装虚拟机和Linux环境(基于Multipass和Virtual Box的Ubuntu)】。
本文介绍如何通过multipass 快速安装和配置 MySQL数据库。

创建虚拟机

通过multipass launch 命令创建一台虚拟机。

    C:\Users\Administrator>multipass list
    No instances found.


    C:\Users\Administrator>multipass launch -n mysql-vm
    Launched: mysql-vm


    C:\Users\Administrator>multipass list
    Name State IPv4 Image
    mysql-vm Running N/A Ubuntu 22.04 LTS


    C:\Users\Administrator>multipass info mysql-vm
    Name: mysql-vm
    State: Running
    IPv4: N/A
    Release: Ubuntu 22.04.2 LTS
    Image hash: 345fbbb6ec82 (Ubuntu 22.04 LTS)
    Load: 0.08 0.02 0.01
    Disk usage: 1.4G out of 4.7G
    Memory usage: 178.5M out of 969.5M
    Mounts: --


    C:\Users\Administrator>


    连接这台虚拟机。

      C:\Users\Administrator>multipass shell mysql-vm
      Welcome 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/advantage


      System information as of Sun Mar 26 08:51:26 CST 2023


      System load: 0.0 Processes: 86
      Usage of : 30.9% of 4.67GB Users logged in: 0
      Memory usage: 21% IPv4 address for enp0s3: 10.0.2.15
      Swap usage: 0%




      * Introducing Expanded Security Maintenance for Applications.
      Receive updates to over 25,000 software packages with your
      Ubuntu Pro subscription. Free for personal use.


      https://ubuntu.com/pro


      Expanded 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 status




      The list of available updates is more than a week old.
      To check for new updates run: sudo apt update


      To run a command as administrator (user "root"), use "sudo <command>".
      See "man sudo_root" for details.


      ubuntu@mysql-vm:~$


      安装MySQL

      使用的主要命令:

        sudo apt update
        sudo apt show mysql-server
        sudo apt install mysql-server
        sudo 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.service
              ubuntu@mysql-vm:~$ sudo systemctl status mysql.service
              mysql.service - MySQL Community Server
              Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
              Active: active (running) since Sun 2023-03-26 09:00:03 CST; 27s ago
              Process: 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.1M
              CPU: 1.123s
              CGroup: /system.slice/mysql.service
              └─4084 /usr/sbin/mysqld


              Mar 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 mysql
                  Welcome to the MySQL monitor. Commands end with ; or \g.
                  Your MySQL connection id is 8
                  Server 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 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.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> exit
                        Bye
                        ubuntu@mysql-vm:~$ mysql -u root -p
                        Enter password:
                        Welcome to the MySQL monitor. Commands end with ; or \g.
                        Your MySQL connection id is 9
                        Server 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 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>

                        其他参考:服务相关操作

                        启动和停止mysql服务的命令如下。

                          systemctl  start mysld.service
                          systemctl stop mysld.service
                          systemctl  status mysld.service
                            service mysql status
                            例:
                            ubuntu@mysql-vm:~$ service mysql status
                            mysql.service - MySQL Community Server
                            Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
                            Active: active (running) since Mon 2022-09-12 20:52:54 CST; 7min ago
                            Process: 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.6M
                            CGroup: /system.slice/mysql.service
                            └─717 /usr/sbin/mysqld


                            Sep 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

                            MySQL入门02:关于MySQL连接的ABC

                            MySQL入门03:MySQL修改root密码的方法

                            Oracle 和 Mysql 的索引在Null字段上处理的异同


                            文章转载自SQL和数据库技术,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                            评论