因爲最近學習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