Mac通过Homebrew安装MySQL
一、安装
安装Homebrew
参见: Mac安装Homebrew
注意,如果之前已经安装过的话,需要先删除如下的文件或者目录;如果是全新安装,这一步可以忽略
rm -rf /usr/local/etc/my.cnf*
rm -rf /usr/local/Cellar/mysql@5.7/
rm -rf /System/Volumes/Data/usr/local/Cellar/mysql@5.7
rm -rf /System/Volumes/Data/usr/local/etc/my.cnf
rm -rf /System/Volumes/Data/usr/local/Cellar/mysql@5.7
1、执行安装命令
brew install mysql@5.7 # 这里一定要加上版本号,否则是安装最新的,比如最新的是8.0.19,则可能有些早期的mysql客户端无法链接
localhost:~ local$ brew install mysql
==> Downloading https://homebrew.bintray.com/bottles/mysql-5.6.26.yosemite.bottle.1.tar.gz
Already downloaded: /Library/Caches/Homebrew/mysql-5.6.26.yosemite.bottle.1.tar.gz
==> Pouring mysql-5.6.26.yosemite.bottle.1.tar.gz
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.
1.1、创建软连接(可选,如果不存在就需要创建,为了方便/usr/local/bin加入环境变量的情况下可以直接使用命令):
ln -s /usr/local/opt/mysql@5.7/bin/mysqladmin /usr/local/bin/mysqladmin
ln -s /usr/local/opt/mysql@5.7/bin/mysql.server /usr/local/bin/mysql.server
ln -s /usr/local/opt/mysql@5.7/bin/mysql /usr/local/bin/mysql
2、启动mysql
mysql.server start
2.1、首次连接:
mysql -uroot
3、停止mysql
mysql.server stop
4、修改root密码:
/usr/local/bin/mysqladmin -u root password 1234567890(root的新密码)
# /usr/local/bin/mysqladmin -> ../Cellar/mysql/8.0.19_1/bin/mysqladmin
5、执行安全设置
mysql_secure_installation
显示如下
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
按照提示选择密码等级,并设置root密码
二、创建新的数据库、用户并授权
1、登录mysql
mysql -u root -p
按提示输入root密码
local:~# mysql -u root -p
Enter password:
2、创建数据库
CREATE DATABASE IF NOT EXISTS `allinone` DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
3、创建用户
create user \'adminuser\'@\'%\' identified by \'test2020\';
4、授权用户
grant all privileges on allinone.* to \'adminuser\'@\'%\';
flush privileges;
5、查看数据库列表
show databases;
6、切换数据库;
use allinone; # allinone 为数据库名称
7、显示当前数据库所有的表
show tables
三、建表
CREATE TABLE `xmind2chandaocase` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` int(11) NOT NULL COMMENT \'上传xmind文件进行解析:upload=1;\\n从禅道导出未xmind用例:export=2;\',
`fire_time` datetime NOT NULL,
`ip` varchar(18) NOT NULL,
`mac` varchar(48) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
四、检查mysql状态
local % mysql.server status
SUCCESS! MySQL running (60677)
local % mysql.server stop
Shutting down MySQL
.. SUCCESS!
local % mysql.server status
ERROR! MySQL is not running
local % mysql.server start
Starting MySQL
. SUCCESS!
local % mysql.server status
SUCCESS! MySQL running (60992)
local %