1.Mysql5.7的安裝
1.1. 首先,在mysql官網下載mysql5.7的安裝包,本人下載的版本是mysql-5.7.15-osx10.11-x86_64.dmg,後面所有的實踐也都是基於這個版本的;
1.2. 然後,根據提示一步步安裝,安裝完成後,會生成root賬戶的隨機密碼,一定要保存好這個密碼;
1.3. 配置環境變量,mysql默認是安裝在/usr/local下的,使用命令echo $SHELL查看系統使用的shell類型,本人用的是zsh,所以修改.bash_profile是無效的,應該修改.zshrc文件;
➜ ~ vi ~/.zshrc
export PATH=${PATH}:/usr/local/mysql/bin
1.4. 啓動mysql服務,可以使用命令:
➜ sudo /usr/local/mysql/support-files/mysql.server start
也可以通過【系統偏好設置】,找到mysql圖標的應用,點擊按鈕啓動服務
1.5.使用之前保存的臨時密碼登錄,並重置密碼
➜ ~ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 68
Server version: 5.7.15 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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.
此時可以成功登錄了,但是如果執行任何一個命令,如show databases; 就會報錯:
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
所以需要重置密碼:
set password =password('你的密碼');
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.15 |
+-----------+
1 row in set (0.00 sec)
2.基本使用
參考地址:http://dev.mysql.com/doc/refman/5.7/en/connecting-disconnecting.html
使用下面的命令:mysql -h host -u user -p 連接實例,如果連接的是本地機器,可以省略host
如:
➜ ~ mysql -h 127.0.0.1 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
使用命令quit斷開連接,如:
mysql> QUIT
Bye
參考地址:http://dev.mysql.com/doc/refman/5.7/en/entering-queries.html
比如查詢數據庫實例的版本及當前日期:
mysql> select version(),current_date;
+-----------+--------------+
| version() | current_date |
+-----------+--------------+
| 5.7.15 | 2016-11-19 |
+-----------+--------------+
1 row in set (0.00 sec)
mysql>
這裏有幾個注意的點:
(1). 所有的語句以分號結束,除了quit命令;
多行輸入時,如果突然想取消執行,可以輸入\c,如:
mysql> select
-> user()\c
mysql>
(2). 除了返回結果集,還返回了結果集行數及查詢消耗的時間,但是這個“查詢消耗時間”,並不是精確的,它代表的是“掛鐘時間”,並不是cpu或機器時間,而且還會受到服務器負載和網絡延遲的影響;
(3). 關鍵詞大小寫不敏感,如:
mysql> select Version(),current_DATE;
+-----------+--------------+
| Version() | current_DATE |
+-----------+--------------+
| 5.7.15 | 2016-11-19 |
+-----------+--------------+
1 row in set (0.01 sec)
(4). 多行輸入的提示符及代表的含義,有以下幾種:
2.3 庫表相關
創建庫表以及基礎的SQL查詢比較簡單,這裏不做說明,簡單介紹下獲取數據庫信息的方式:
查看所有的庫:show databases;
使用某個庫:use dbname;
查看庫有哪些表:show tables;
查看錶的列:describe tablename;
直接登錄某個數據庫:mysql -h host -u user -p dbname,如:
➜ ~ mysql -h 127.0.0.1 -u root -p mytest;
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
上面沒有指定端口,使用的是默認端口3306,如果要指定端口,可以加--port或-P,如:
➜ ~ mysql -h 127.0.0.1 -u root -P 3306 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
當然還可以指定其他選項,如protocal,可以參考:
http://dev.mysql.com/doc/refman/5.7/en/connecting.html
3.Mysql5.7的卸載
sudo rm -rf /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/PreferencePanes/My*
sudo rm -rf /var/db/receipts/com.mysql.*
參考:http://blog.csdn.net/maxsky/article/details/40347505