macOS下MySQL 8.0 安裝與配置教程

1、前言

一、本教程主要內容

  • 適用Homebrew安裝MySQL
  • MySQL 8.0 基礎適用於配置
  • MySQL shell管理經常使用語法示例
  • MySQL字符編碼配置
  • MySQL遠程訪問配置

二、本教程環境信息與適用範圍

  • 環境信息
軟件/環境 版本/說明
macOS macOS High Sierra
MySQL MySQL 8.0.12
  • 適用範圍
軟件 版本
macOS macOS
MySQL 8.0.x

2、MySQL安裝

一、Homebrew安裝

macOS下的Homebrew就至關於CentOS下的yum或者是Ubuntu下的apt-getmysql

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

二、Homebrew安裝與啓動MySQL服務

  • 安裝mysql
brew install mysql
  • 配置並啓動MySQL服務
brew tap homebrew/services
brew services start mysql

三、修改root密碼

mysqladmin -u root password 'yourpassword'

四、MySQL安裝測試

  • 查看MySQL版本
#查看MySQL版本
mysql -V

#輸出示例
mysql  Ver 8.0.12 for osx10.13 on x86_64 (Homebrew)
  • MySQL shell測試
#進入MySQL shell
mysql -u root -p

#成功進入會輸出如下信息
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.12 Homebrew

#查看數據庫
mysql> show databases;

#退出
mysql> exit;

3、MySQL安全設置

一、MySQL 8 安全設置介紹

MySQL 8 新增了安全設置嚮導,這對於在服務器部署MySQL來講,簡化了安全設置的操做,很是棒,不過對於macOS來講,不是剛需,若是不感興趣能夠直接跳過這個章節git

安全設置大體分爲如下幾個步驟/選項github

  1. 密碼強度驗證插件
  2. 修改root帳號密碼
  3. 移除匿名用戶
  4. 禁用root帳戶遠程登陸
  5. 移除測試數據庫(test)
  6. 從新加載受權表

以上幾個步驟/選項根據本身須要來便可。sql

二、MySQL 8 安全設置示例

  • 進入安全設置
mysql_secure_installation

-設置示例shell

Securing the MySQL server deployment.

Enter password for user root:

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: no
#這裏我選了不安全密碼強度驗證插件

Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : no
#這裏我選了不修改root密碼
 ... skipping.
 
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : yes
Success.
#這裏我選擇了移除匿名用戶


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? (Press y|Y for Yes, any other key for No) : yes
Success.
#這裏我選擇了禁用root帳號遠程登陸訪問

By default, MySQL 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? (Press y|Y for Yes, any other key for No) : no
 ... skipping.
 #這裏我選擇了不移除測試數據庫
 
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : yes
Success.
#這裏我選擇了從新加載權限表,由於我前面選擇了禁用root帳號遠程登陸訪問

All done!

4、MySQL shell管理語法示例

一、數據庫相關語法示例

#建立數據庫
mysql> CREATE DATABASE mydb;

#查看全部數據庫
mysql> SHOW DATABASES;

#使用數據並建立表
mysql> USE mydb;
mysql> CREATE TABLE test(id int,body varchar(100));

#查看錶
mysql> SHOW TABLES;

二、用戶與訪問受權語法示例

#新建本地用戶
mysql> CREATE USER 'test'@'localhost' IDENTIFIED BY '123456';

#新建遠程用戶
mysql> CREATE USER 'test'@'%' IDENTIFIED BY '123456';

#賦予指定帳戶指定數據庫遠程訪問權限
mysql> GRANT ALL PRIVILEGES ON mydb.* TO 'test'@'%';

#賦予指定帳戶對全部數據庫遠程訪問權限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'test'@'%';

#賦予指定帳戶對全部數據庫本地訪問權限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'test'@'localhost';

#刷新權限
mysql> FLUSH PRIVILEGES;

三、受權相關語法示例

#一、查看權限
SHOW GRANTS FOR 'test'@'%';

#二、賦予權限
GRANT ALL PRIVILEGES ON *.* TO 'test'@'%';

#三、收回權限
REVOKE ALL PRIVILEGES ON *.* FROM 'test'@'%';

#四、刷新權限
FLUSH PRIVILEGES;

#五、刪除用戶
DROP USER 'test'@'localhost';

5、字符編碼配置

MySQL默認的編碼不是utf8,爲了兼容中文的存儲,仍是須要配置一下數據庫

一、 修改字符編碼

#修改配置文件
vi /usr/local/etc/my.cnf

#修改1:增長client配置(文件開頭,[mysqld]以前)
[client]
default-character-set=utf8mb4

#修改2:增長mysqld配置(文件結尾,[mysqld]以後)
#charset
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci

二、重啓生效

  • 重啓MySQL服務
mysql.server restart
#也可使用命令:brew services restart mysql
#不過建議使用命令:mysql.server restart在出錯時能夠看到更準確完整的信息
  • 查看字符編碼
#進入MySQL shell
mysql -u root -p

#查看字符編碼
mysql>  show variables like '%char%';

6、遠程訪問配置

MySQL默認綁定了ip:127.0.0.1。若是咱們須要遠程訪問,去掉該配置便可macos

一、 修改ip綁定

#修改配置文件
vi /usr/local/etc/my.cnf

#註釋掉ip-address選項
[mysqld]
# Only allow connections from localhost
#bind-address = 127.0.0.1

二、重啓生效

  • 重啓MySQL服務
mysql.server restart

7、備註

相關閱讀

  • MySQL中的utf8

http://www.infoq.com/cn/artic...安全

  • MySQL遠程訪問與bind-address問題

https://serverfault.com/quest...ruby


本文首發於:https://ken.io/note/macos-mys...bash

相關文章
相關標籤/搜索