mac mysql语句_Mac上安装MySQL

由于最近学习mysql,首先安装mysql环境是第一步,使用brew安装时,遇到各种坑,所以放弃了,采用下载mysql安装包的方式,成功的给我的mac系统安利了mysql,下面做一下总结:

一.安装MySQL

1.赶紧去mysql官网安利一下dmg版本

6b73c67074302aeb097417c4a32e1415.png

2.下载后双击安装"mysql-8.0.11-macos10.13-x86_64.dmg",注意安装到最后一步,设置密码,不要冒然点击OK

3.安装成功后,打开"系统偏好设置",找到"MySQL",双击

023737f1e3e0c3cb98242e4251aec71f.png

4.点击"Start MySQL Server",这就开启MySQL成功。

二.连接数据库

第一步已经顺顺利利的把mysql寄养在你的mac家里了,可以开始高高兴兴的搞事情了。

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,然后在输入你刚才设定的密码即可。

9d98f1d1d1b99b67d5209ab99ab05b2b.png

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 intauto_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 nullprimary key,-> name char(16));

mysql>create table student (-> id int not nullprimary 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值