软件/环境 | 版本/说明 |
---|---|
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