我报名参加金石计划1期挑战——瓜分10万奖池,这是我的第2篇文章,点击查看活动详情
开发机操练
后端程序员如果有一台云端开发机(AWS, GCP,Azure),是一件很幸福的事。我们可以跑各种测试,定时任务,验证性能,又不用担心会影响到本地环境。就算出现什么问题,大不了销毁实例重新申请一个。
在学习成长的过程中,保持上手实践是一个必备的习惯。因为听得再多,不写出来,不运行,你是不会有很深的体会的。数据库就是个很好的案例。哪怕不说很多简单的 KV 存储,就是知名的关系型数据库 MySQL 和 PostgreSQL,我们也都可以很简单的下载安装到开发机上,进而调试,验证自己在很多场景下的思考是否正确。笔者曾经见过一些同学很努力学习经典的数据库课程,到最后发现只是看 Lecture,看看 PDF 笔记,这种学习的成果可想而知。
听过,见过,用过,suffer 过,才能一步步理解到底怎么用,到最后慢慢理解原理。尤其是数据库打基础的系列,一切资源都是公开的,作为持续成长的后端工程师,一定要用好。思考什么是关系型数据库,MySQL 和 PostgreSQL 有什么区别,SQLite 又是如何支持那些 feature 的。
同一条 SQL 在不同数据库执行结果差多少,各自语法是否有不同。每一条都需要我们实际去构造案例,执行之后才知道结果。这不是任何一个专栏,或者课程能提供的。也只有亲身体验过,我们才知道,到底用 SQLite 来做单测 mock 我们生产环境的 MySQL 这件事靠不靠谱。
简言之,保持一个上手的好习惯,到云厂商平台上申请一台开发机(EC2),安装好你想去学习的组件(MySQL,Redis,PostgreSQL,SQLite 等等),然后上手操练,验证。
最近经常看到有小伙伴在 Linux Debian 上安装 MySQL 遇到很多问题,尤其是被误导,安装上了 MariaDB。今天我们就来看看怎样基于 Debian 来安装 MySQL server。
MariaDB 和 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.
我们知道 MySQL 其实是 Oracle 维护的数据库,而 MariaDB 则是是一个基于 MySQL 的社区 fork 产物。在 Debian 10 中,默认的 MySQL 其实是 MariaDB。
这也是为什么,当我们用 apt-cache search mysql-server
搜索的时候,得到的其实是一系列 MariaDB 的包:
但还是那句话,既然要学,希望起步先能保持主流,要不遇到不一致我们也无法判断是否是 MariaDB 和 MySQL 存在差异导致的。所以,我们还是要安装正版的 MySQL。
下载安装包
参照 Installing MySQL on Linux Using Debian Packages from Oracle 官方安装指南,我们选择走 APT Repository 下载。点击 MySQL APT Repository
MySQL 已经为我们提供了一个 Debian 的 Deb 包,通常是十几 K。我们选择 download。
此时会挑出来一个页面,要求我们登录或注册,忽略即可。
由于我们是开发机,没有 GUI 页面,这里其实我们真正需要的只是个下载链接。注意下面的 No thanks, just start my download,右键复制即可。
我这里拿到的链接是: 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 包。
安装过程中,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 已经开始跑了。
关注这里 Active: active (running) 即可,这里就标识我们进程的状态。
验证安装
安装完成后,我们来看看版本。
运行 mysqladmin -u root -p version
,输入你的密码,就能看到我们安装的版本了。
可以看到,我们的 MySQL 是 8.0.30,跟下载的包一致。这里还是给出 uptime 的时间,以及其他状态信息。
连接 MySQL
安装之后,我们来试试看,既然开发机已经有了 MySQL server 运行,连上去看看效果,以后就可以走 cli 工具很简洁地基于开发机上的 MySQL 来做测试,而不用担心污染生产环境的数据库了。
登录方法很简单,目前选择 user 为 root,我们运行
mysql -u root -p
复制代码
输入密码后就登录成功了,此时 cli 帮助我们创建了一个连接。
我们看一下目前内置的库有哪些。
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)
复制代码
在这里我们就可以自由翱翔了,创建各种测试表,验证不同场景的 case。It's all up to you. Enjoy!