-
在終端輸入
brew install mysql
安裝MySQL,完成之后重啟終端。We've installed your MySQL database without a root password. To secure it run:
? mysql_secure_installation
MySQL is configured to only allow connections from localhost by default
To connect run:
? mysql -uroot
To have launchd start mysql now and restart at login:
brew services start mysql
Or, if you don't want/need a background service you can just run:
mysql.server start
==> Summary
?? /usr/local/Cellar/mysql/5.7.19: 322 files, 233MB
上面說已經(jīng)安裝了MySQL但是沒有密碼。
-
輸入
mysql
再按tab
會(huì)提示一些和mysql相關(guān)的命令漓帅,證明安裝成功mysql mysqlbinlog
mysql.server mysqlcheck
mysql_client_test mysqld
mysql_client_test_embedded mysqld_multi
mysql_config mysqld_safe
mysql_config_editor mysqldump
mysql_embedded mysqldumpslow
mysql_install_db mysqlimport
mysql_plugin mysqlpump
mysql_secure_installation mysqlshow
mysql_ssl_rsa_setup mysqlslap
mysql_tzinfo_to_sql mysqltest
mysql_upgrade mysqltest_embedded
mysqladmin mysqlxtest
至此,mysql就安裝完了耸携。
MySQL簡(jiǎn)單上手
-
MySQL和直接使用sqlite不同,需要有一個(gè)MySQL server在辕翰,之后通過socket去鏈接上MySQL夺衍,然后才可以對(duì)數(shù)據(jù)庫(kù)進(jìn)行相關(guān)操作。
-
啟動(dòng)和關(guān)閉服務(wù)
mysql.server start Starting MySQL . SUCCESS! mysql.server stop Shutting down MySQL . SUCCESS!
之后重新開啟喜命,來進(jìn)行后面的操作
-
配置root用戶
MySQL有自己的用戶體系沟沙,默認(rèn)用戶是 root賬戶,沒有密碼壁榕∶希可以通過下列命令來設(shè)置密碼
mysqladmin -u root password New password: Confirm new password: Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
包括mysqladmin在內(nèi),其他的mysql需要指明用戶的時(shí)候加 -u username,如果用戶添加過密碼牌里,需要使用-p指令颊咬,所以如果已經(jīng)設(shè)置過root用戶的密碼之后再修改密碼,后面就需要加-p牡辽,否則按照前面的就會(huì)報(bào)錯(cuò)
mysqladmin -u root password mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user 'root'@'localhost' (using password: NO)' mysqladmin -u root password -p Enter password: New password: Confirm new password: Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
?
-
使用root用戶連接數(shù)據(jù)庫(kù)
mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.19 Homebrew Copyright (c) 2000, 2017, 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.
-
一些基本操作
-
查看數(shù)據(jù)庫(kù)
SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec)
-
創(chuàng)建數(shù)據(jù)庫(kù) 使用數(shù)據(jù)庫(kù) 查看表
CREATE DATABASE mydb; Query OK, 1 row affected (0.00 sec) SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mydb | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) USE mydb Database changed SHOW TABLES; Empty set (0.00 sec)
?
-
-
-
創(chuàng)建一個(gè)新用戶
? root用戶的權(quán)限一般不會(huì)隨便拿去使用喳篇,所以創(chuàng)建一些用戶,并分配對(duì)應(yīng)的權(quán)限态辛。下面的命令創(chuàng)建的是一個(gè)名字是user12 密碼是123456 的本地用戶
CREATE USER user12@localhost IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
之后分配mydb數(shù)據(jù)庫(kù)的權(quán)限
GRANT ALL ON mydb.* to user12@localhost;
Query OK, 0 rows affected (0.00 sec)
GUI 工具
Mac 有一個(gè)好用且免費(fèi)的GUI工具 Sequel Pro
用socket鏈接數(shù)據(jù)庫(kù)之后麸澜,可以進(jìn)行很多操作。
以上大部分內(nèi)容來源自binderclip 在此表示感謝