一、安裝MySql
brew install mysql
二、 配置MySql
- 查看mysql安裝路徑
brew info mysql
- 輸入如下命令獲取幫助
mysql --help
會發(fā)現(xiàn)如下說明

image.png
意思是默認配置會依次從一下路徑中的my.cnf文件中讀取,而下面指出了這個文件中可以有哪些配置,比如bind-address,port。先看看這個文件在哪里,以我自己的電腦為例,my.cnf在/opt/homebrew/etc/下
# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 127.0.0.1
mysqlx-bind-address = 127.0.0.1
port = 3306
在更改這個文件之后,需要重啟一下mysql服務,
mysql.server restart
三、連接MySql
使用如下命令登錄,
mysql -uroot -p
輸入登錄密碼后進入連接界面,就可以操作數(shù)據(jù)庫了。常用命令:
status // 查看當前數(shù)據(jù)庫信息,主要關注Current database和Current user
show databases; // 列出所有數(shù)據(jù)庫,注意以;結尾
use 數(shù)據(jù)庫名; // 使用數(shù)據(jù)庫
show tables; // 列出數(shù)據(jù)庫中說有表
其他操作就是常規(guī)sql操作了。
四、格式化查看表數(shù)據(jù)
通過
select * from <table>;
的時候,命令窗口顯示的數(shù)據(jù)非常難看,可以通過以下兩種方式格式化顯示:
- 使用
pager less -SFX;命令,
mysql> pager less -SFX;
PAGER set to 'less -SFX'
mysql> select *from radacct limit 2;
+-----------+-----------------------------------+----------------------------------+-------------+-----------+-------+--------------+-----------+-----------------+---------------+--------
| radacctid | acctsessionid | acctuniqueid | username | groupname | realm | nasipaddress | nasportid | nasporttype | acctstarttime | acctupd
+-----------+-----------------------------------+----------------------------------+-------------+-----------+-------+--------------+-----------+-----------------+---------------+--------
| 665 | 0001.0902005073300000e74b7c204341 | 0f01812fefcea2e1abccc5314a775243 | 132***2090 | | | 221.7.16.202 | 33575645 | Wireless-802.11 | 1428041359 | 142
| 666 | 0001.09020050733000008fbfa7040635 | ca7f3485db61b207454c13b796df8c57 | 132***3834 | | | 221.7.16.202 | 33575645 | Wireless-802.11 | 1428041341 | 142
+-----------+-----------------------------------+----------------------------------+-------------+-----------+-------+--------------+-----------+-----------------+---------------+--------
(END)
然后再執(zhí)行sql語句,使用左右鍵查看表數(shù)據(jù),按下q退出查看模式;
- 在原sql語句后加上
\G,不需要;,回車可以看到命令行將列作為行展示,
mysql> select *from radacct limit 2 \G
*************************** 1. row ***************************
radacctid: 665
acctsessionid: 0001.0902005073300000e74b7c204341
acctuniqueid: 0f01812fefcea2e1abccc5314a775243
username: 132****2090
groupname:
realm:
nasipaddress: 221.7.16.202
nasportid: 33575645
nasporttype: Wireless-802.11
acctstarttime: 1428041359
acctupdatetime: 1428042057
acctstoptime: 1428042057
acctinterval: NULL
acctsessiontime: 698
acctauthentic: RADIUS
connectinfo_start:
connectinfo_stop: 1000000000
acctinputoctets: 640184
acctoutputoctets: 9515780
calledstationid: 00-00-00-00-00-00:ChinaUnicom
callingstationid: 00:16:6d:ce:13:de
acctterminatecause: Lost-Carrier
servicetype: Framed-User
framedprotocol: PPP
framedipaddress: 10.12.9.35
*************************** 2. row ***************************
radacctid: 666
acctsessionid: 0001.09020050733000008fbfa7040635
acctuniqueid: ca7f3485db61b207454c13b796df8c57
username: 132****3834
groupname:
realm:
nasipaddress: 221.7.16.202
nasportid: 33575645
nasporttype: Wireless-802.11
acctstarttime: 1428041341
acctupdatetime: 1428043501
acctstoptime: 1428043656
acctinterval: 0
acctsessiontime: 2315
acctauthentic: RADIUS
connectinfo_start:
connectinfo_stop: 1000000000
acctinputoctets: 4456853
acctoutputoctets: 138112319
calledstationid: 00-00-00-00-00-00:ChinaUnicom
callingstationid: 90:27:e4:55:65:34
acctterminatecause: NAS-Request
servicetype: Framed-User
framedprotocol: PPP
framedipaddress: 10.12.9.166
2 rows in set (0.00 sec)
五、其他事項
- 如何查看mysql端口號?在連接mysql之后,執(zhí)行如下命令
show global variables like 'port';
- 如果不想每次連接mysql都輸入密碼,在my.cnf文件中添加如下一行
skip-grant-tables
之后重啟mysql服務。這個在誤刪root用戶或者忘記登錄密碼時有用,關于如何創(chuàng)建用戶和設置用戶密碼,參照文章https://blog.csdn.net/code_mzh/article/details/108243683