Mac上安裝MySQL


由於最近學習mysql,首先安裝mysql環境是第一步,使用brew安裝時,遇到各種坑,所以放棄了,采用下載mysql安裝包的方式,成功的給我的mac系統安利了mysql,下面做一下總結:

一.安裝MySQL

  1.趕緊去mysql官網安利一下dmg版本

    mysql-8.0.11-macos10.13-x86_64.dmg

  2.下載后雙擊安裝"mysql-8.0.11-macos10.13-x86_64.dmg",注意安裝到最后一步,設置密碼,不要冒然點擊OK

  3.安裝成功后,打開"系統偏好設置",找到"MySQL",雙擊

  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,然后在輸入你剛才設定的密碼即可。

  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

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM