Recently, installing MySQL8 on Macos should have been a simple task, but it took me more than half a day. I had no Internet in my hometown, and I still used my mobile phone’s 4G network.

The brew way

On the Mac, I install the software first with the brew, and mainly are also don’t want to go to deal with all kinds of dependence, uninstall software is convenient, if you want to upgrade the software is also very convenient, he basically is the way through the soft links, such as commonly used PHP and I go, it is convenient to upgrade, after the installation, soft links under the change to go, However, if you do not update brew frequently, it will update itself first when installing the software, and sometimes the network speed is not good, so you can turn off the automatic update.

Brew install mysql@8 is not compatible with my current MacOS (Sierra 10.12). Brew Search mysql@8 is a compatibility error. Before installation, I executed the search function to confirm that the package can be found. So I executed brew Search, mysql, PHP and other packages to search for this package. Brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset = brew update-reset But I didn’t dare to use BREW anymore, no, I thought I would have to use DMG instead.

DMG way

As a result, I downloaded the macOS version of Mysql from the official website of Mysql, and it went smoothly from download to installation. After installing Mysql, go to the system preferences and find the Mysql startup status is a red one, not a green one, which means the service is not up. I think it is also normal, after all, I have installed a local 5.7, and I was not allowed to choose the port when installing, my first thought is to make sure that I change the port. So, I changed the port to 3307 in the configuration file, and then went to start the service, but still not up. With reference to the Internet, in the/Library/LaunchDaemons/com. Oracle. Oss. Mysql. Mysqld. Plist change of some parameters in this file, such as path and port

 <key>ProgramArguments</key>
        <array>
< string > / usr/local/mysql - 8.0.21 - macos10.15 - x86_64 / bin/mysqld < / string >< string > - defaults - file = / usr/local/etc/my8.0 CNF < / string >< string > - basedir = / usr/local/mysql - 8.0.21 - macos10.15 - x86_64 < / string >< string > - datadir = / usr/local/mysql - 8.0.21 - macos10.15 - x86_64 / data < / string >< string > - the plugin - dir = / usr/local/mysql - 8.0.21 - macos10.15 - x86_64 / lib/plugin < / string > <string>--early-plugin-load=keyring_file=keyring_file.so</string> < string > - keyring - file - data = / usr/local/mysql - 8.0.21 - macos10.15 - x86_64 / keyring/keyring < / string >< string > - log - error = / usr/local/mysql - 8.0.21 - macos10.15 - x86_64 / data/mysqld. Local. Err < / string >< string > - pid - file = / usr/local/mysql - 8.0.21 macos10.15 - x86_64 / data/mysqld. Local. Pid < / string > <string>--user=_mysql</string>  <string>--default_authentication_plugin=mysql_native_password</string>  <string>--port=3307</string>  </array> Copy the code

But the result is still the same, so I enter mysql8 installation directory, and stop using my local 5.7, directly connect line mysqld, found a permissions problem, the users of the directory is _mysql, then I changed to another user to start again, this time is to start, but after I went in, I found that the database is still under 5.7, I clearly specified 8.0 own data path, how to run 5.7 down. You can use mysql_multi to start multiple instances, so you can use Docker instead.

Docker way

Docker is a handy way to experience something new, and if you don’t want it, just delete the image or container.

Installing mysql8 with Docker is easy, so start with a search

$ docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relati...   10014               [OK]
mariadb                           MariaDB is a community-developed fork of M...   3666                [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Crea...   733                                     [OK]
Copy the code

Pull the mirror

$Docker pull mysql: 8.0
Copy the code

After the drawing is complete, check the mirror image

$docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
Mysql 8.0e1d7dc9731da 3 weeks ago 544MBCopy the code

Before using the image, create a few directories, mainly configuration and data mapping, if you do not host the mapping, your container after restart, the data will be gone.

$ mkdir -p ~/Application/Docker/mysql/data ~/Application/Docker/mysql/log ~/Application/Docker/mysql/conf
Copy the code

Run container:

$cd ~/Application/Docker/mysql/
$ docker run -p 3307:3306 --name mysql8 -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0
Copy the code

Command description:

-p 3307:3306: maps port 3306 of a container to port 3307 of a host.- v - v $PWD/conf: / etc/mysql/conf. D: will host the current directory of the conf/my CNF mounted to the container's/etc/mysql/my CNF.-v $PWD/logs:/logs: Mounts the logs directory under the current directory of the host to /logs of the container.-v $PWD/data:/var/lib/mysql: mount the data directory in the current host directory to /var/lib/mysql in the container.-e MYSQL_ROOT_PASSWORD=123456: Initializes the password of user root.Copy the code

Docker container ls/docker ps/docker container ls/docker ps/Docker container ls Docker rm mysql8 will be used to delete the previous container, otherwise the next time you run the container will report that it already exists with the same name.

$docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
37 bcead3d1b1 mysql: 8.0 "docker - the entrypoint..." 23 hours ago Up 21 hours 33060/ TCP, 0.0.0.0:3307->3306/ TCP mysql8Copy the code

Once the container above is started, you can look inside the container

$ docker exec -it mysql8 /bin/bash
Copy the code

Inside the container, you can use mysql -u root -p123456 to connect to mysql8

Mysql8 is using a new password algorithm, so we need to change the algorithm to mysql_native_password

Go inside the container we started, connect to mysql, and execute separately

mysql> grant all PRIVILEGES on *.* to root@The '%' WITH GRANT OPTION;
mysql> ALTER user 'root'@The '%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER; 
mysql> ALTER user 'root'@The '%' IDENTIFIED WITH mysql_native_password BY '123456'; 
mysql>  FLUSH PRIVILEGES;
Copy the code

After the execution is complete, the installation is complete, and port 3307 can be used to connect to the host remotely.

Not to mention, using Docker to complete these environments although there are a lot of small problems, but in general, it is very convenient, it seems that Docker will be used in the future, as long as some data for persistent processing, others as little as possible, It’s also a wonderful thing.

This article is formatted using MDNICE