下载MySQL
1、mysql官网:https://www.mysql.com/
2、点击Download
3、点击MySQL Community (GPL) Downloads »
4、点击MySQL Community Server
5、在这个页面,下载dmg版本的MySQL
安装并启动MySQL
1、双击下载好的mysql.dmg文件
2、安装过程中mysql会给一个root的初始密码。一定要记下来,不然之后修改密码比较麻烦
3、配置环境变量
$ cd ~
$ vim ./.bash_profile
// 开始编辑文件
export PATH=$PATH:/usr/local/mysql/bin
export PATH=$PATH:/usr/local/mysql/support-files
// 保存并source文件
$ source ~/.bash_profile
$ echo $PATH
4、准备启动mysql
停止MySQL服务
注意:这一步会让你输密码,密码就是电脑的密码。这里与数据库root用户的密码没关系
sudo mysql.server stop
重启MySQL服务
sudo mysql.server restart
查看MySQL服务状态
sudo mysql.server status
5、启动mysql
输入命令:mysql -u root -p
vvHhhdeMacBook-Pro:~ vvhhh$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.28 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
修改root密码,创建数据库
1、修改root用户密码
注意符号,不要有中文符号,新密码注意区分大小写
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Root123';
旧版本更新密码的命令在新版本的mysql上已经不能用了,以下这个命令是旧的
set password for root@localhost=password('root123');
2、创建,使用数据库
// 查看当前存在的数据库
mysql> show databases;
// 创建exercise数据库
mysql> CREATE DATABASE exercise;
// 使用exercise数据库
mysql> use exercise;
// 查看表结构
mysql> show tables;