How To Install MySQL on Debian 10

I signed up for the 1st challenge of the Golden Stone Project - share the 100,000 prize pool, this is my 2nd article, click to view the details of the event

development machine drill

Back-end programmers are very happy if they have a cloud development machine (AWS, GCP, Azure). We can run various tests, schedule tasks, and verify performance without worrying about affecting the local environment. Even if there is any problem, the big deal is to destroy the instance and apply for a new one.

In the process of learning and growing, maintaining hands-on practice is a necessary habit. Because no matter how much you listen to, if you don't write it down or run it, you won't have a deep understanding. Databases are a good example. Even if we don't mention many simple KV storages, that is, the well-known relational databases MySQL and PostgreSQL, we can simply download and install them on the development machine, and then debug to verify whether our thinking is correct in many scenarios. The author has seen some students working hard to learn the classic database courses, and finally found that they just read Lecture and PDF notes. The results of this kind of learning can be imagined.

After hearing, seeing, using, and supplementing, you can understand how to use it step by step, and slowly understand the principle at the end. Especially in the database-based series, all resources are open to the public. As a back-end engineer who continues to grow, we must make good use of it. Think about what a relational database is, what is the difference between MySQL and PostgreSQL, and how does SQLite support those features.

How much difference is the execution result of the same SQL in different databases, and whether their syntax is different. Each one requires us to actually construct the case, and only know the result after execution. This is not what any column, or course, can offer. Only through personal experience can we know whether it is reliable to use SQLite to do single-test mocking of MySQL in our production environment.

In short, keep a good habit of getting started, apply for a development machine (EC2) on the cloud vendor platform, install the components you want to learn (MySQL, Redis, PostgreSQL, SQLite, etc.), and then start practicing and verifying .

Recently, I often see that some friends have encountered many problems installing MySQL on Linux Debian, especially they were misled and installed MariaDB. Today we will see how to install MySQL server based on Debian.

MariaDB and MySQL

In Debian 10, MariaDB, a community fork of the MySQL project, is packaged as the default MySQL variant. While MariaDB works well in most cases, if you need features found only in Oracle’s MySQL, you can install and use packages from a repository maintained by the MySQL developers.

We know that MySQL is actually a database maintained by Oracle, and MariaDB is a community fork product based on MySQL. In Debian 10, the default MySQL is actually MariaDB.

This is why, when we apt-cache search mysql-serversearch with , we get a series of MariaDB packages:

image.png

But again, since we have to learn, we hope to maintain the mainstream first, otherwise we cannot judge whether it is caused by the difference between MariaDB and MySQL. So, we still have to install genuine MySQL.

Download the installation package

Referring to the official installation guide of Installing MySQL on Linux Using Debian Packages from Oracle , we choose to download from APT Repository. Click on MySQL APT Repository

image.png

MySQL 已经为我们提供了一个 Debian 的 Deb 包,通常是十几 K。我们选择 download。

此时会挑出来一个页面,要求我们登录或注册,忽略即可。

image.png

由于我们是开发机,没有 GUI 页面,这里其实我们真正需要的只是个下载链接。注意下面的 No thanks, just start my download,右键复制即可。

image.png

我这里拿到的链接是: dev.mysql.com/get/mysql-a…

下来我们登录开发机,通过 wget 拿到这个包。

wget https://dev.mysql.com/get/mysql-apt-config_0.8.23-1_all.deb
复制代码

这样就下载到本地了,大家可以 ls 一下看看。

使用 dpkg 进行安装

dpkg is the software at the base of the package management system in the free operating system Debian and its numerous derivatives. dpkg is used to install, remove, and provide information about .deb packages. dpkg itself is a low-level tool.

dpkg 是 Debian 系统中对 .deb 格式文件的一个安装管理器。它能够帮助我们安装,卸载,以及获取 deb 包的一些信息。

我们运行 dpkg --help 就能看到相关的用法,本次我们只需要使用安装能力,用 -i 即可。

$ dpkg --help
Usage: dpkg [<option> ...] <command>

Commands:
  -i|--install       <.deb file name>... | -R|--recursive <directory>...
  --unpack           <.deb file name>... | -R|--recursive <directory>...
  -A|--record-avail  <.deb file name>... | -R|--recursive <directory>...
  --configure        <package>... | -a|--pending
  --triggers-only    <package>... | -a|--pending
  -r|--remove        <package>... | -a|--pending
  -P|--purge         <package>... | -a|--pending
  -V|--verify [<package>...]       Verify the integrity of package(s).
  --get-selections [<pattern>...]  Get list of selections to stdout.
  --set-selections                 Set package selections from stdin.
  --clear-selections               Deselect every non-essential package.
  --update-avail [<Packages-file>] Replace available packages info.
  --merge-avail [<Packages-file>]  Merge with info from file.
  --clear-avail                    Erase existing available info.
  --forget-old-unavail             Forget uninstalled unavailable pkgs.
  -s|--status [<package>...]       Display package status details.
  -p|--print-avail [<package>...]  Display available version details.
  -L|--listfiles <package>...      List files 'owned' by package(s).
  -l|--list [<pattern>...]         List packages concisely.
  -S|--search <pattern>...         Find package(s) owning file(s).
  -C|--audit [<package>...]        Check for broken package(s).
  --yet-to-unpack                  Print packages selected for installation.
  --predep-package                 Print pre-dependencies to unpack.
  --add-architecture <arch>        Add <arch> to the list of architectures.
  --remove-architecture <arch>     Remove <arch> from the list of architectures.
  --print-architecture             Print dpkg architecture.
  --print-foreign-architectures    Print allowed foreign architectures.
  --assert-<feature>               Assert support for the specified feature.
  --validate-<thing> <string>      Validate a <thing>'s <string>.
  --compare-versions <a> <op> <b>  Compare version numbers - see below.
  --force-help                     Show help on forcing.
  -Dh|--debug=help                 Show help on debugging.

  -?, --help                       Show this help message.
      --version                    Show the version.

Assertable features: support-predepends, working-epoch, long-filenames,
  multi-conrep, multi-arch, versioned-provides.

Validatable things: pkgname, archname, trigname, version.

Use dpkg with -b, --build, -c, --contents, -e, --control, -I, --info,
  -f, --field, -x, --extract, -X, --vextract, --ctrl-tarfile, --fsys-tarfile
on archives (type dpkg-deb --help).

Options:
  --admindir=<directory>     Use <directory> instead of /var/lib/dpkg.
  --root=<directory>         Install on a different root directory.
  --instdir=<directory>      Change installation dir without changing admin dir.
  --path-exclude=<pattern>   Do not install paths which match a shell pattern.
  --path-include=<pattern>   Re-include a pattern after a previous exclusion.
  -O|--selected-only         Skip packages not selected for install/upgrade.
  -E|--skip-same-version     Skip packages whose same version is installed.
  -G|--refuse-downgrade      Skip packages with earlier version than installed.
  -B|--auto-deconfigure      Install even if it would break some other package.
  --[no-]triggers            Skip or force consequential trigger processing.
  --verify-format=<format>   Verify output format (supported: 'rpm').
  --no-debsig                Do not try to verify package signatures.
  --no-act|--dry-run|--simulate
                             Just say what we would do - don't do it.
  -D|--debug=<octal>         Enable debugging (see -Dhelp or --debug=help).
  --status-fd <n>            Send status change updates to file descriptor <n>.
  --status-logger=<command>  Send status change updates to <command>'s stdin.
  --log=<filename>           Log status changes and actions to <filename>.
  --ignore-depends=<package>,...
                             Ignore dependencies involving <package>.
  --force-...                Override problems (see --force-help).
  --no-force-...|--refuse-...
                             Stop when problems encountered.
  --abort-after <n>          Abort after encountering <n> errors.

Comparison operators for --compare-versions are:
  lt le eq ne ge gt       (treat empty version as earlier than any version);
  lt-nl le-nl ge-nl gt-nl (treat empty version as later than any version);
  < << <= = >= >> >       (only for compatibility with control file syntax).

Use 'apt' or 'aptitude' for user-friendly package management.
复制代码

这里我们在安装目录下执行:

sudo dpkg -i mysql-apt-config_0.8.23-1_all.deb
复制代码

表示我们希望用 dpkg 来安装刚刚下载的 deb 包。

image.png

安装过程中,dpkg 会询问我们是否需要对安装进行一些自定义的配置,我们直接全选 OK 即可,保持默认设置。此时默认希望安装的包就是最新的 MySQL stable 版本。

执行完毕后,我们需要刷新一下 apt 的缓存,让我们可以用 apt 命令找到我们刚刚添加的包。

sudo apt update
复制代码

至此,我们添加了 MySQL 包,并可以通过 apt 工具来安装,如果还需要再次配置,可以使用 sudo dpkg-reconfigure mysql-apt-config 命令选择配置,然后 sudo apt-get update 刷新下缓存即可。

安装 MySQL server

我们可以用 sudo systemctl status mysql 来检验是否存在 mysql 进行运行,此时我们先运行一次,会发现没有任何进程输出。符合预期。

下来我们用 apt 来安装 server

sudo apt install mysql-server
复制代码

这里会要求你输入 root 密码,二次确认。鉴权插件大家看自己的场景,如果你不确定,直接选择 Use Strong Password Encryption 就好,这样更安全。

安装完成后,我们再次运行sudo systemctl status mysql,你会发现,此时 server 已经开始跑了。

image.png

关注这里 Active: active (running) 即可,这里就标识我们进程的状态。

验证安装

安装完成后,我们来看看版本。

运行 mysqladmin -u root -p version,输入你的密码,就能看到我们安装的版本了。

image.png

可以看到,我们的 MySQL 是 8.0.30,跟下载的包一致。这里还是给出 uptime 的时间,以及其他状态信息。

连接 MySQL

安装之后,我们来试试看,既然开发机已经有了 MySQL server 运行,连上去看看效果,以后就可以走 cli 工具很简洁地基于开发机上的 MySQL 来做测试,而不用担心污染生产环境的数据库了。

登录方法很简单,目前选择 user 为 root,我们运行

mysql -u root -p
复制代码

输入密码后就登录成功了,此时 cli 帮助我们创建了一个连接。

image.png

我们看一下目前内置的库有哪些。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
复制代码

端口默认还是 3306,所以我们写代码的时候可以直接走 localhost:3306 来连接

mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.01 sec)
复制代码

Here we can fly freely, create various test tables, and verify the cases of different scenarios. It's all up to you. Enjoy!

References

Guess you like

Origin juejin.im/post/7143060333194641439