軟件/環境 | 版本/說明 |
---|---|
macOS | macOS High Sierra |
MySQL | MySQL 8.0.12 |
軟件 | 版本 |
---|---|
macOS | macOS |
MySQL | 8.0.x |
macOS下的Homebrew就至關於CentOS下的yum或者是Ubuntu下的apt-getmysql
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install mysql
brew tap homebrew/services brew services start mysql
mysqladmin -u root password 'yourpassword'
#查看MySQL版本 mysql -V #輸出示例 mysql Ver 8.0.12 for osx10.13 on x86_64 (Homebrew)
#進入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;
MySQL 8 新增了安全設置嚮導,這對於在服務器部署MySQL來講,簡化了安全設置的操做,很是棒,不過對於macOS來講,不是剛需,若是不感興趣能夠直接跳過這個章節git
安全設置大體分爲如下幾個步驟/選項github
以上幾個步驟/選項根據本身須要來便可。sql
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!
#建立數據庫 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';
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.server restart #也可使用命令:brew services restart mysql #不過建議使用命令:mysql.server restart在出錯時能夠看到更準確完整的信息
#進入MySQL shell mysql -u root -p #查看字符編碼 mysql> show variables like '%char%';
MySQL默認綁定了ip:127.0.0.1。若是咱們須要遠程訪問,去掉該配置便可macos
#修改配置文件 vi /usr/local/etc/my.cnf #註釋掉ip-address選項 [mysqld] # Only allow connections from localhost #bind-address = 127.0.0.1
mysql.server restart
http://www.infoq.com/cn/artic...安全
https://serverfault.com/quest...ruby
本文首發於:https://ken.io/note/macos-mys...bash