本地开发环境准备和复盘

MySQL@5.7、Redis@4.0、Zookeeper@3.4.6

Posted by HH on July 13, 2021

本地开发环境准备和复盘

Brew

在mac开发和本地测试,少不了要安装一些软件,比如Java常见的依赖:mysql、redis、zookeeper等。

在mac上安装了brew后,就像Windows下的各类软件管家一样,可以直接拉取软件,避免了去官网下载的麻烦。

安装brew的方式:进入mac终端,执行下面的命令(不能翻墙者的福音,有gitee的中转)

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

复盘:

  1. zsh,一款用于交互式使用的shell,可以作为脚本解释器来使用。其包含了bash、ksh、tcsh等其他shell中许多优秀功能。macOS Catalina版开始,其默认shell从bash改为zsh。
  2. -c,Take the first argument as a command to execute, rather than reading commands from a script or standard input. If any futhers arguments are given, the first one is assigned to $0, rather than being used as a positional parameter.

Zookeeper

安装zookeeper,在终端输入下面的命令

brew install zookeeper

MySQL

安装MySQL在终端输入下面的命令

brew install mysql

安装过程中依赖了openssl@1.1

==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/openssl%401.1-1.1.1k.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/mysql%405.7-5.7.34.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Installing dependencies for mysql@5.7: openssl@1.1
==> Installing mysql@5.7 dependency: openssl@1.1
==> Pouring openssl@1.1-1.1.1k.mojave.bottle.tar.gz
Warning: The post-install step did not complete successfully
You can try again using:
  brew postinstall openssl@1.1
==> Summary
🍺  /usr/local/Cellar/openssl@1.1/1.1.1k: 8,071 files, 18.4MB
==> Installing mysql@5.7
==> Pouring mysql@5.7-5.7.34.mojave.bottle.tar.gz
==> /usr/local/Cellar/mysql@5.7/5.7.34/bin/mysqld --initialize-insecure --user=jiawenhao --basedir=/usr/local/Cellar/mysql@5.7/5.7.34 --datadir=/usr/local/var/mysql --tmpdir=/tmp
==> Caveats

通过安装信息,我们能够看到

–basedir=/usr/local/Cellar/mysql@5.7/5.7.34

–datadir=/usr/local/var/mysql

–tmpdir=/tmp

在这里我们扩展一下/usr目录。它的全称可能是Unix System/Software Resources。/usr是unix系统最重要的目录之一,涵盖了二进制文件,各种文档,各种头文件,还有各种库文件,还有诸多程序,如ftp,telnet等等。下面简单罗列一下可能存在的目录的含义:

  • /usr/bin:所有可执行文件,如gcc/firefox(指不在/sbin和/bin内的)
  • /usr/include:各种头文件,编译文件等时需要使用
  • /usr/lib:所有可执行文件需要的库文件
  • /usr/local:存放手动安装的软件。
  • /usr/doc:文档,/usr/share/doc的软连接
  • /usr/share:包含各个程序间的共享文件,如字体,图标,文档等。
  • /usr/share/man:manual操作手册
  • /usr/src:Linux内核的源码和说明文档等

与之相应的还有/var目录,variable的简称,主要针对常态性变动的文件,包括缓存cache、登陆档log file以及某些软件运作所产生的文件。

  • /var/cache:应用程序本身运作过程中会产生一些暂存文档
  • /var/lib:程序本身执行的过程中,需要使用到的数据文件放置的目录。在此目录下各自的软件应该要有各自的目录。举例来说,MySQL的数据库放置到/var/lib/mysql而rpm的数据则放到/var/lib/rpm去
  • /var/lock:某些装置或者事文件资源一次只能被一个应用程序所使用哦,如果同时有两个程序使用该装置时,就可能产生一些错误的情况,因此就得要在该装置上锁。
  • /var/log:非常重要。这是登陆文件放置的目录。里面比较重要的文件,比如/var/log/messages,/var/log/wtmp记录登入者的信息等。
  • /var/mail:放置个人电子邮件信箱的目录,不过这个目录也被放置到/var/spool/mail目录中,通常这两个目录是互为链接文件。
  • /var/run:某些程序或者服务启动后,会将他们的PID放置到这个目录下。
  • /var/spool:这个目录通常放置一些队列数据,所谓队列就是排队等待其他程序使用的数据。这些数据被使用后通常会被删除。举例说明,系统收到新信惠放置到/var/spool/mail中,但使用者收下该信件后原则上会被删除,如果暂时寄不出去会放到/var/spool/mqueue中,等被送出后就要被删除。如果是工作排程数据crontab,就会放到/var/spool/cron目录中。

在安装之后设置一些环境变量

echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> /Users/jiawenhao/.bash_profile
echo 'export LDFLAGS="-L/usr/local/opt/mysql@5.7/lib"'>> /Users/jiawenhao/.bash_profile 
echo 'export CPPFLAGS="-I/usr/local/opt/mysql@5.7/include"'>> /Users/jiawenhao/.bash_profile

启动mysql

brew services start mysql@5.7
if you don't want/need a background service you can just run:
/usr/local/opt/mysql@5.7/bin/mysql.server start
brew services stop mysql@5.7
brew services restart mysql@5.7
brew services list

排查问题

ERROR! The server quit without updating PID file (/usr/local/var/mysql/your_computer_name-Pro.local.pid).

ps aux | grep mysqld | grep grep -v
sudo kill -9 mysqlpid

Google了很多文章,都说kill掉重启就会好,发现无法杀掉进程,每次kill掉都会自己起一个新的进程,而且这个无法kill的mysql进程还是_mysql用户起来的,很明显跟我没啥关系,于是开始Google如何kill掉这个mysqld进程,然后看下面的命令

sudo launchctl unload -w /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist

再然后kill -9 mysqldid就可以了,原因这里简述一下,并不深入。简单来说就是:Mac系统在更新OS X之后,加入了 “Rootless机制”,也就意味着有一些目录的内容我们无法进行随意的进行读写;即使设置root权限也不能使用。得,总而言之就是坑,把 链接 贴上来。

然后就是继续mysql@5.7

mysql_secure_installation

结束!

Redis

brew search redis
brew install redis@4.0
echo 'export PATH="/usr/local/opt/redis@4.0/bin:$PATH"' >> /Users/jiawenhao/.bash_profile

启动redis

brew services start redis@4.0
if you don't want/need a background service you can just run:
/usr/local/opt/redis@4.0/bin/redis-server /usr/local/etc/redis.conf