mac安裝mysql數據庫及配置環境變量:https://blog.csdn.net/qq_36004521/article/details/80637886
1. 進入mysql
mysql -u root -p
然后輸入密碼。
如果密碼輸錯了,會報‘ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)’錯誤。
錯誤解決方案看這里吧: https://blog.csdn.net/qq_32786873/article/details/79225039
2.退出mysql
exit or quit
3.創建數據庫
create database <數據庫名>; ->這個分號很重要啊,注意別用中文分號,看仔細點。
4.顯示數據庫
show databases;
5.刪除數據庫:
drop database <數據庫名稱>;
6.使用數據庫
use <數據庫名稱>;
7.數據庫表操作
// 創建表(personId設置了關鍵值則不能重復)
create table personTable (personId int, name varchar(20), gender varchar(10), primary key(personId));
// 在表中添加新列
alter table personTable add age int;
// 查看表結構信息
desc personTable;
// 刪除表
drop table personTable;
// 復制表
create table personInfo like personTable;
-------------------------------------------------
8. 簡單sql語句
// 查看數據
select * from personInfo; // 查詢全部
select * from personInfo where age = 35; // 條件查詢
// 插入數據
insert into personInfo (personId, name, gender, age) values (1000, 'devZhang', "male", 35);
1 雙引號中的是一個標准的SQL語句,把他拿到mysql中可以單獨執行
2 因為是SQL語句,字符型的字段的值要用單引號括起來加以標注
字符串類型的變量也要加''
// 更新數據
update personInfo set age = 25,name = "uiChen",gender='female' where personId = 1001;
// 刪除數據
delete from personInfo where age = 23;
錯誤處理:
1. 連接Navicat與mysql時報錯,注意,這個問題是 MySQL 的問題,不是客戶端問題,
錯誤信息 Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/mysql/lib/plugin/caching_sha2_password.so, 2): image not found
解決方法:mysql > ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpassword';
2. 對某個數據庫或者里面的表進行操作時,動不動就卡死,或者出現Lost connection to MySQL server during query
通過 show processlist 一看,滿屏都是 Waiting for table metadata lock 狀態的連接。一個一個id kill 不過來,可以通過重啟mysql服務