在macOS上使用MariaDB

切换到MairaDB时的MySQL备忘录。

由于尝试让MySQL与其他程序一起运行时遇到了各种错误,所以暂时放弃了这个方案。
在卸载MySQL后,将重新安装MariaDB。

我们假设已安装了Homebrew。

安装MariaDB

使用Homebrew安装MariaDB。

% brew install mariadb

然后自动开始安装,请稍等片刻。

A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

MySQL is configured to only allow connections from localhost by default

To start mariadb now and restart at login:
  brew services start mariadb
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/mariadb/bin/mysqld_safe --datadir\=/usr/local/var/mysql

一旦出现这个,安装就完成了。

2. MariaDB的启动

安装完之后马上启动吧。

% brew services start mariadb

如果没有问题,将显示”成功启动’mariadb’(标签:homebrew.mxcl.mariadb)”。

用ps命令来确认。

% ps -ax | grep mysql
 7888 ??         0:00.03 /bin/sh /usr/local/opt/mariadb/bin/mysqld_safe --datadir=/usr/local/var/mysql
 7961 ??         0:00.30 /usr/local/opt/mariadb/bin/mariadbd --basedir=/usr/local/opt/mariadb --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/opt/mariadb/lib/plugin --log-error=/usr/local/var/mysql/MacBookPro.local.err --pid-file=MacBookPro.local.pid
 8281 ttys000    0:00.00 grep mysql

顺便提一下,要停止Mariadb:
% brew services stop mariadb
要重新启动:
% brew services restart mariadb

3. 安全设置

安装完成后,只有root帐户,并且root帐户没有设置密码,所以首先要为root帐户设置密码。

% 使用sudo命令运行mysql_secure_installation命令

不要忘记使用sudo来执行。
顺便提一下,mysql_secure_installation已经不推荐使用了,我们尽可能使用mariadb-secure-installation。

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 

由于是刚刚安装,所以没有设置root密码。请按Enter键。

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] 

你要切换到unix_socket认证吗?这是一个问题。
unix_socket认证是指当操作系统的用户名和MariaDB的用户名相同时,可以无需密码登录的认证方式。
虽然可能有相同的情况,但是似乎更常见的是不相同的情况,所以输入”n”。

Change the root password? [Y/n] Y
New password: 
Re-enter new password: 

请设置(或更改)路由器密码,输入所要设置的密码后,再次输入以确认。

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] 

「默认有一个匿名用户设置」,这句话表示可以删除它没有问题,选择”Y”。

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]

是否允许以root用户远程登录。
根据开发环境的需求进行适当调整,本次选择为”n”。

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]

是否删除名为test的默认数据库?
因为不需要,所以选择”Y”。

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]

是否确定要确保更改设置被反映?
当然会被反映,没有不被反映的理由,所以选择”Y”。

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

这样安全设置就完成了。

4. 创建用户

首先,我们需要创建一个用于使用MariaDB的用户。首先以root账户登录MariaDB。

% mysql -u root -p
elcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 11.1.2-MariaDB Homebrew

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

确认当前注册用户
MariaDB [(none)]> 选择 mysql.user 表中的 User、Host 和 Password

+-------------+-----------+-------------------------------------------+
| User        | Host      | Password                                  |
+-------------+-----------+-------------------------------------------+
| mariadb.sys | localhost |                                           |
| root        | localhost | ***************************************** |
| OSuser      | localhost | invalid                                   |
| PUBLIC      |           |                                           |
+-------------+-----------+-------------------------------------------+
4 rows in set (0.003 sec)

大概是这样的感觉。
我认为OS用户是注册在终端上的登录用户。
你可以只改变这个用户的密码来使用,也可以新建一个用户也可以。

MariaDB [(none)]> GRANT ALL ON *.* TO 'username'@'localhost' IDENTIFIED BY '********';
Query OK, 0 rows affected (0.022 sec)

如果您打算继续使用现有的OS用户,请使用OS用户;如果您要创建一个新用户,请输入一个新的用户名。在“********”处填入任意的密码。

+-------------+-----------+-------------------------------------------+
| User        | Host      | Password                                  |
+-------------+-----------+-------------------------------------------+
| mariadb.sys | localhost |                                           |
| root        | localhost | ***************************************** |
| OSuser      | localhost | ***************************************** |
| PUBLIC      |           |                                           |
| NewUser     | localhost | ***************************************** |
+-------------+-----------+-------------------------------------------+

これでユーザ作成は完了です。
MariaDBから抜けて作成したユーザでログインできればOKです。

MariaDB [(none)]> exit
Bye

% mysql -u username -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 11.1.2-MariaDB Homebrew

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

通过这个,MariaDB已经准备就绪。

广告
将在 10 秒后关闭
bannerAds