因为最近学习mysql,首先安装mysql环境是第一步,使用brew安装时,遇到各类坑,因此放弃了,采用下载mysql安装包的方式,成功的给个人mac系统安利了mysql,下面作一下总结:mysql
一.安装MySQLsql
1.赶忙去mysql官网安利一下dmg版本数据库
mysql-8.0.11-macos10.13-x86_64.dmgmacos
2.下载后双击安装"mysql-8.0.11-macos10.13-x86_64.dmg",注意安装到最后一步,设置密码,不要冒然点击OKide
3.安装成功后,打开"系统偏好设置",找到"MySQL",双击学习
4.点击"Start MySQL Server",这就开启MySQL成功。spa
二.链接数据库
code
第一步已经顺顺利利的把mysql寄养在你的mac家里了,能够开始高高兴兴的搞事情了。blog
1.链接数据库,首先cd到你mysql安装到目录,而后执行以下命令排序
xm:~ xm$ mysql -u root -p
输入密码后,发现我勒个菜,又搞事情,不会相信爱情了。
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/Cellar/mysql/5.7.22/lib/plugin/caching_sha2_password.so, 2): image not found
后来通过各类查找,发现忘记给mysql定义别名了,输入alias命令:
alias mysql=/usr/local/mysql/bin/mysql
alias mysqladmin=/usr/local/mysql/bin/mysqladmin
回车在输入,而后在执行命令"mysql -u root -p",你会神奇的发现成功了
一劳永逸的作法:
系统偏好设置 -> MySQL -> Initialize Database -> 设置密码,此时在链接mysql,执行mysql -u root -p,而后在输入你刚才设定的密码便可。
2.若是登录远程主机上的mysql数据库
mysql -h 主机地址 -u 用户名 -p 用户密码
3.增长新用户
格式以下:
grant 操做权限 on 数据库.* to 用户名@登录主机地址 identified by '密码';
意思是:授予,某主机上的某用户(附带该用户的登录密码)在某数据库上,执行某些操做的权限
(1)好比:任意主机上("%"),用户(用户名:test1,密码:adc)在全部数据库上,执行任意操做的权限(很危险)
grant all privileges on *.* to test1@"%" identified by "abc";
其中all privileges表示查询,插入,修改,删除的权限:select,insert,update,delete
以上命令等价于:
grant select,insert,update,delete on *.* to test1@"%" identified by "abc";
而后刷新权限
flush privileges;
(2)好比:受权本地主机上的用户操做数据库的权限
建立数据库(好比:openfire)
create database openfire;
授予本地主机用户(用户名:test2,密码:123)访问数据库(数据库名称:openfire)的操做权限
grant all privileges on openfire.* to test2@localhost identified by "123";
flush privileges;
以后,就能够用新的用户,访问openfire数据库了
4.更新指定账户的密码(用户名:test1,新密码:1234)
update mysql.user set password=password('1234') where User="test1" and Host="localhost";
三.mysql的经常使用操做(注意:mysql语句后面必须加";")
1.显示全部数据库列表 ,命令以下
mysql> show databases;
2.建立数据库文件,而且让其支持中文,命令以下
mysql> create database test charset 'utf8';
3.打开数据库文件,命令以下
mysql> use test;
4.显示数据库文件中全部的表,命令以下
mysql> show tables;
5.进入某个表结构,命令以下
mysql> desc student;
6.建立表,首先进入数据库文件
myslq> use test
而后建立表(你确定懂一点mysql基础,因此表支持的数据类型就再也不赘述)
mysql> create table teacher ( -> tea_id int auto_increment, -> name char(32) not null,
-> age int not null, -> sex char(2) not null, -> primary key(tea_id));
实例解析:
若是你不想数据为null,能够设置字段属性为not null,这样在操做数据库时若是插入数据为空,则会报错。
atuo_increment定义字段为自增属性,通常用于主键。
primary key设置字段为主键
7.删库 drop database 库名
mysql> drop database test1;
8.删表 drop table 表名
mysql> drop table student
9.插入数据
mysql> insert student (name,age,enroll_date) values('dn',56,'2018-09-28');
10.查询数据
mysql> select * from student limit 2 offset 1 where age > 23;
实例解析:
offset指示select语句查询的数据偏移量,默认偏移量为0
limit限定查询结果的条数
11.更新语句
mysql> update student set age = 45 where id = 2;
12.删除语句
mysql> delete from student where id = 4;
13.like语句
mysql> select * from student where name like 'x%';
14.排序和分组语句
mysql> select * from student order by age desc;
四.mysql的高级操做(注意:mysql语句后面必须加";")
1.alter命令操做
mysql> alter table stu_table drop sex; # 删除sex字段 mysql> alter table stu_table add sex char(10) not null default 'male'; # 添加sex字段
mysql> alter table stu_table modify name char(12); # 修改表字段类型
mysql> alter table stu_table rename student; # 修改表名
2.外键约束
mysql> create table class ( -> id int not null primary key, -> name char(16)); mysql> create table student ( -> id int not null primary key, -> name char(16) not null, -> class_id int not null, -> key fk_class_key (class_id), -> constraint fk_class_key foreign key (class_id) references class (id));
mysql> insert into student(id,name,class_id) values(1,'xm01',1); # 若是class中不存在id,student也插入不了,这就叫外键约束
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`student`, CONSTRAINT `fk_class_key` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`))
mysql> insert into class (id,name) values(1,'Python');
Query OK, 1 row affected (0.03 sec)
mysql> insert into student(name,class_id) values('xm01',1);
Query OK, 1 row affected (0.08 sec)
mysql> delete from class where id=1; # 没法删除,由于student与其关联
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`test`.`student`, CONSTRAINT `fk_class_key` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`))
mysql> delete from student where id = 1;
Query OK, 1 row affected (0.03 sec)
3.NULL值处理
关于NULL的条件运算比较特殊。你不能使用 = NULL或 != NULL在列表中查找NULL值。
MYSQL中处理NULL只能使用IS NULL和IS NOT NULL