mac环境安装mysql教程

为方便大家一边学习回顾,一边实践,写了一篇mac环境安装mysql的完整教程。

一、下载安装包安装

这个安装过程比较简单快速一点,如果仅仅是学习一次性安装后不动,推荐使用这种安装办法。如果以后会涉及多次安装不同版本mysql,更新,卸载重装等操作,推荐使用homebrew安装会更方便。

1.下载安装包

下载mysql地址:https://dev.mysql.com/downloads/mysql/

需要判断一下你mac电脑芯片,ARM和x86不要选错。

uname -m

点DMG 版本到下载详情页下载:

2.执行安装

下载完安装包后,打开dmg包按照向导一步步执行后续安装即可。B站有完整视频此处不再赘述b站安装教程

二、homebrew安装

homebrew安装本来是非常简单的,只需要执行一行命令就可以。但有些国外站点无法访问会报错需要设置动态代理,导致稍微麻烦了一点。

当然未来如果要安装多个mysql版本学习,更新卸载等,使用homebrew会比较方便。另外和jdk,maven,git等组件一样统一交给homebrew管理感觉上会比较统一。

1.安装ihost

安装iHost软件,设置代理。因为mysql安装过程中依赖的部分组件需要从国外网站下载,无法链接时需要通过配置ip代理来访问。

添加系统账号编辑 /etc/hosts 表的权限。

xxxuser 需要替换成你的系统账号名称
 sudo /bin/chmod +a 'user:xxxuser:allow write' /etc/hosts

2.部分国外资源组件代理配置

安装过程中可能会出现有些国外资源无法下载,导致安装失败的情况,比如:

无法访问地址
https://raw.githubusercontent.com/Homebrew/homebrew-core/c75694d561f2be60b8ef6d9f5924b324fd9c5bc5/Formula/i/icu4c@76.rb

这个错误是因为墙无法访问外网https://raw.githubusercontent.com/。可以通过ip138找一个动态ip代理地址,通过iHost配置代理ip地址。如下图:

3.执行安装

查看要安装的版本
brew search mysql

卸载
brew uninstall mysql

安装
brew install mysql@8.0

显示以下信息代表安装成功。由于有些资源要从国外下载,homebrew安装会比较耗时,可能会花1-2小时。

三、环境配置

1、配置环境变量

如果使用的是 zsh(Z Shell),则修改的是 ~/.zshrc 文件。如果你使用的是 bash,那么应该修改 ~/.bash_profile~/.bashrc 文件。

1.使用命令open -e .bash_profile编辑内容。
没有该文件则使用touch .bash_profile创建一个.bash_profile的隐藏配置文件。
open -e .bash_profile
修改添加以下内容(MYSQL_HOME为安装mysql的目录,intel芯片会有所不同,自行调整):
export MYSQL_HOME=/opt/homebrew/opt/mysql@8.0
export PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$MYSQL_HOME/bin:$PATH

Command +S保存.bash_profile文件
 
2.使用source ~/.bash_profile命令,使配置生效
source ~/.bash_profile
 
3.使用echo $MYSQL_HOME命令。命令显示刚才配置的路径,返回的信息对应上了上面的路径就说明配置成功了。

2、root用户密码&安全配置

1.启动服务
brew services start mysql@8.0

2.输入安全配置命令
mysql_secure_installation

3.会提醒当前链接使用的是空密码,询问是否设置 VALIDATE PASSWORD 组件
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

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:


4.输入 Y 进行密码配置,接着会询问密码强度,非生产学习环境选择0-LOW 8个字符即可。
There are three levels of password validation policy: Y

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0


5.配置完密码强度后,提示配置root 8位数密码,
学习环境输入两次12345678 即可将root密码设置为12345678。
Please set the password for root here.
New password: 12345678
Re-enter new password: 12345678


6.MySQL 的 validate_password 插件评估了提供的密码强度为50,询问是否继续,选Y
Estimated strength of the password: 50 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y 


7.在MySQL的默认安装过程中,有时会创建一个匿名空用户。这个匿名用户允许任何人无需创建
用户账户即可登录MySQL,这主要是为了方便测试以及让安装过程更加顺畅。生产环境不能使用。
此步骤询问是否删除匿名用户,我们选择Y。
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) : Y


8.此步骤配置是否仅允许root用户从localhost(即MySQL服务器所在的机器)进行连接。
出于安全考虑此处一般都选 Y。
如果我们需要远程管理MySQL服务器,可以考虑创建一个具有适当权限的非root用户。
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) : Y


9.MySQL默认安装时会创建一个名为test的数据库,所有人都是可访问。
这个数据库主要用于测试目的,在将MySQL服务器部署到生产环境之前,一般会删除。
此处我们选Y 删除test库。
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) : Y


10.执行完前面各种配置后,MySQL会提示是否希望立即重新加载权限表,此处选Y
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) : Y
All done! 
用户密码,安全配置完成。

3、创建一个用户

1.用户root登录mysql
 mysql -u root -p  
 
2.创建一个名为studyuser的用户,不限制登录客户端,密码12345678
CREATE USER 'studyuser'@'%' IDENTIFIED BY '12345678';

3.授予studyuser所有权限,方便学习
GRANT ALL PRIVILEGES ON *.* TO 'studyuser'@'%' WITH GRANT OPTION;

4.刷新权限
FLUSH PRIVILEGES;

5.退出root用户 
quit

6.用新创建的studyuser用户登入
 mysql -u studyuser -p  

四、创建测试数据库&表

1.用户studyuser登录mysql
 mysql -u studyuser -p  

2.创建数据库test_db
CREATE DATABASE IF NOT EXISTS test_db
  DEFAULT CHARACTER SET utf8
  DEFAULT COLLATE utf8_general_ci;
 
USE test_db;

3.创建一张 my_user 表
CREATE TABLE my_user (
  id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '用户id,自增主键',
  name VARCHAR(255) NOT NULL COMMENT '用户姓名', 
  sex TINYINT NOT NULL COMMENT '性别 1-男性 2-女性',
  age INT NOT NULL COMMENT '年龄',
	ext JSON  COMMENT 'json扩展字段'
) ENGINE=InnoDB
  DEFAULT CHARSET=utf8;

4.插入一系列测试数据
INSERT INTO my_user (name, sex, age, ext) VALUES
('Alice', 1, 30, '{"hobbies": ["reading", "traveling"], "address": {"city": "Wonderland", "country": "Fantasyland"}}'),
('Bob', 2, 25, '{"hobbies": ["sports", "music"], "address": {"city": "Metropolis", "country": "Realmland"}}'),
('Charlie', 1, 35, '{"hobbies": ["painting", "cooking"], "address": {"city": "Artisville", "country": "Imaginationland"}}'),
('Diana', 2, 28, '{"hobbies": ["hiking", "photography"], "address": {"city": "Naturestown", "country": "Outdoorsland"}}'),
('Edward', 1, 40, '{"hobbies": ["gardening", "chess"], "address": {"city": "Quietville", "country": "Peaceland"}}'),
('张三', 1, 21, '{"address": {"city": "杭州", "country": "中国"}}'),
('李四', 1, 24, '{"address": {"city": "苏州", "country": "中国"}}'),
('王五', 1, 38, '{"address": {"city": "上海", "country": "中国"}}')
;

5.查询测试
select * from my_user;
+----+---------+-----+-----+-------------------------------------------------------------------------------------------------------+
| id | name    | sex | age | ext                                                                                                   |
+----+---------+-----+-----+-------------------------------------------------------------------------------------------------------+
|  1 | Alice   |   1 |  30 | {"address": {"city": "Wonderland", "country": "Fantasyland"}, "hobbies": ["reading", "traveling"]}    |
|  2 | Bob     |   2 |  25 | {"address": {"city": "Metropolis", "country": "Realmland"}, "hobbies": ["sports", "music"]}           |
|  3 | Charlie |   1 |  35 | {"address": {"city": "Artisville", "country": "Imaginationland"}, "hobbies": ["painting", "cooking"]} |
|  4 | Diana   |   2 |  28 | {"address": {"city": "Naturestown", "country": "Outdoorsland"}, "hobbies": ["hiking", "photography"]} |
|  5 | Edward  |   1 |  40 | {"address": {"city": "Quietville", "country": "Peaceland"}, "hobbies": ["gardening", "chess"]}        |
|  6 | 张三    |   1 |  21 | {"address": {"city": "杭州", "country": "中国"}}                                                      |
|  7 | 李四    |   1 |  24 | {"address": {"city": "苏州", "country": "中国"}}                                                      |
|  8 | 王五    |   1 |  38 | {"address": {"city": "上海", "country": "中国"}}                                                      |
+----+---------+-----+-----+-------------------------------------------------------------------------------------------------------+
8 rows in set (0.00 sec)

五、安装Navicat

navicat是一个对数据库进行可视化操作的客户端软件,针对单机数据库操作比较好用。

安装使用方法自行问度娘。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值