免费
MySQL Community Server 社区版本 免费 但是MySQL 不提供官方技术支持
MySQL Cluster 集群版 开源免费 可将几个 MySQL Server 封装乘一个Server
收费
MySQL Enterprise Edition
商业版 该版本是收费的 可以试用30天 官方提供技术支持
MySQL Cluster CGE
高级集群版,需付费。
mac mysql 下载地址
MySQL :: Download MySQL Community Server
下载x86 一路安装
配置环境变量
export PATH=$PATH:/usr/local/mysql/bin
export PATH=$PATH:/usr/local/mysql/support-files
我的环境变量是 ~/.zshrc 有其他机器可能是 ~/.bash_profile
MySQL服务的启停和状态的查看
开启MySQL服务(需要输入电脑密码,不是数据库登录密码)
sudo mysql.server start停止MySQL服务
sudo mysql.server stop重启MySQL服务
sudo mysql.server restart查看MySQL服务状态
sudo mysql.server status
Mac MySql - 查看以及修改编码格式 - 掘金
登录mysql
mysql -uroot -p
mysql可视化工具
Download | DBeaver Community
安装
安装完后新建数据库链接
选择mysql
填写密码其他保持默认
sql语法
sql 语法大小写不敏感
单行注释 # 或者--(空格)
多行注释 /**/
以分号结尾;
DDL
#显示所有库
show
--注释
#注释
databases;#使用库
use sys;#查看库
SELECT DATABASE();#创建数据库
CREATE database test charset utf8;
#显示数据库
show databases;
#删除数据库
DROP database test;
创建表
use mysql;
#显示当前库的所有表
show tables;
#创建表
CREATE table student(id int,name varchar(10),age int);
#删除表
DROP table student;
DML 对数据操作
#显示当前库的所有表
show tables;
#创建表
CREATE table student(id int,name varchar(10),age int);
#删除表
DROP table student;*//*show databases;
CREATE database test charset utf8;
show databases;
use test;
SELECT DATABASE();
show tables;*/
CREATE table student(id int, name varchar(10), age int);
show tables;
#插入数据 INSERT INTO 表名 [列1,列2,列三,...列n] values[值1,值2,...值n],[值1,值2,...值n][值1,值2,...值n]
INSERT INTO student (id) values(1),(2),(3);#sql 值支持单引号
INSERT INTO student (id,name,age)values(4,'赵天霸',22);
#输入多条
INSERT INTO student (id,name,age)values(5,'赵地霸',22),(6,'赵大霸',22);
#省略前面的键全部赋值
INSERT INTO student values(7,'赵二霸',22),(8,'赵三霸',22);#删除 DELETE FROM 表名 [where 判断条件 = > < >= <= !=] 无条件则删除整张表数据
DELETE FROM student WHERE id >= 5 && id < 8;
DELETE FROM student WHERE id = 1;
#更新 UPDATE 表名 SET 列 = 值 [where 条件判断]
UPDATE student SET name = '张大仙' where id = 2;
#不带where 条件 修改的是整张表的数据
UPDATE student SET age = 18;
数据查询
#查询 SELECT 字段列表|*from 表 where 条件判断;
#查询对应的 列
SELECT id,name from student;
#查询所有
SELECT * FROM student;
#带条件查询
SELECT *FROM student WHERE age = 22;
DQL
/*use mysql;
--
#显示当前库的所有表
show tables;
#创建表
CREATE table student(id int,name varchar(10),age int);
#删除表
DROP table student;*//*show databases;
CREATE database test charset utf8;
show databases;
use test;
SELECT DATABASE();
show tables;*/
DROP table student;
CREATE table student(id int, name varchar(10), age int,sex varchar(5));
show tables;
#插入数据 INSERT INTO 表名 [列1,列2,列三,...列n] values[值1,值2,...值n],[值1,值2,...值n][值1,值2,...值n]
INSERT INTO student (id,name,age,sex) values(1,'李诗诗',18,'女'),(1,'李容容',18,'女'),(1,'李云',20,'女');#sql 值支持单引号
INSERT INTO student (id,name,age,sex)values(4,'赵天霸',22,'男');
#输入多条
INSERT INTO student (id,name,age,sex)values(5,'赵地霸',22,'男'),(6,'赵大霸',22,'男');
#省略前面的键全部赋值
INSERT INTO student values(7,'赵二霸',20,'男'),(8,'赵三霸',18,'男');#删除 DELETE FROM 表名 [where 判断条件 = > < >= <= !=] 无条件则删除整张表数据
DELETE FROM student WHERE id >= 5 && id < 8;
DELETE FROM student WHERE id = 1;
#更新 UPDATE 表名 SET 列 = 值 [where 条件判断]
UPDATE student SET name = '张大仙' where id = 2;
#不带where 条件 修改的是整张表的数据
UPDATE student SET age = 18;#查询 SELECT 字段列表|*from 表 where 条件判断;
#查询对应的 列
SELECT id,name from student;
#查询所有
SELECT * FROM student;
#带条件查询
SELECT *FROM student WHERE age = 22;#聚合函数
SELECT sex, AVG(age),SUM(age),MIN(age),MAX(age), COUNT(*) FROM student GROUP BY sex;
排序
/*use mysql;
--
#显示当前库的所有表
show tables;
#创建表
CREATE table student(id int,name varchar(10),age int);
#删除表
DROP table student;*//*show databases;
CREATE database test charset utf8;
show databases;
use test;
SELECT DATABASE();
show tables;*/
DROP table student;
CREATE table student(id int, name varchar(10), age int,sex varchar(5));
show tables;
#插入数据 INSERT INTO 表名 [列1,列2,列三,...列n] values[值1,值2,...值n],[值1,值2,...值n][值1,值2,...值n]
INSERT INTO student (id,name,age,sex) values(1,'李诗诗',18,'女'),(1,'李容容',18,'女'),(1,'李云',20,'女');#sql 值支持单引号
INSERT INTO student (id,name,age,sex)values(4,'赵天霸',22,'男');
#输入多条
INSERT INTO student (id,name,age,sex)values(5,'赵地霸',22,'男'),(6,'赵大霸',22,'男');
#省略前面的键全部赋值
INSERT INTO student values(7,'赵二霸',20,'男'),(8,'赵三霸',18,'男');#删除 DELETE FROM 表名 [where 判断条件 = > < >= <= !=] 无条件则删除整张表数据
DELETE FROM student WHERE id >= 5 && id < 8;
DELETE FROM student WHERE id = 1;
#更新 UPDATE 表名 SET 列 = 值 [where 条件判断]
UPDATE student SET name = '张大仙' where id = 2;
#不带where 条件 修改的是整张表的数据
UPDATE student SET age = 18;#查询 SELECT 字段列表|*from 表 where 条件判断;
#查询对应的 列
SELECT id,name from student;
#查询所有
SELECT * FROM student;
#带条件查询
SELECT *FROM student WHERE age = 22;#聚合函数
SELECT sex, AVG(age),SUM(age),MIN(age),MAX(age), COUNT(*) FROM student GROUP BY sex;SELECT *FROM student WHERE age > 20 ORDER BY age ASC ;
#取三条
SELECT *FROM student limit 3;
#从第三条开始取2条
SELECT *FROM student limit 3,2;SELECT sex, AVG(age),SUM(age),MIN(age),MAX(age), COUNT(*) FROM student WHERE age = '18' GROUP BY sex ORDER BY age ASC LIMIT 1 ;
python 链接mysql
(1045, "Access denied for user 'root'@'localhost' (using password: YES)")
检查下用户名和密码有没有输入正确
'cryptography' package is required for sha256_password or caching_sha2_password auth methods
安装cryptography
pip install cryptography
from pymysql import Connectionconnect = Connection(host='localhost',port=3306,user='root',password='xxxxx')
#获取游标对象
cursor = connect.cursor()
#选择数据库 就是执行sql的use 数据库
connect.select_db("test")
#cursor.execute("create table test_people(id int, name varchar(10), age int,sex varchar(5))")
cursor.execute("select *from student")
result = cursor.fetchall()
print(f"type = {type(result)} result = {result}")
for r in result:print(r)print(connect.get_server_info())
connect.close()
插入数据
from pymysql import Connectionconnect = Connection(host='localhost',port=3306,user='root',password='12345678',autocommit=True)
#获取游标对象
cursor = connect.cursor()
#选择数据库 就是执行sql的use 数据库
connect.select_db("test")
#cursor.execute("create table test_people(id int, name varchar(10), age int,sex varchar(5))")
cursor.execute("INSERT INTO student values(11,'赵飞燕',17,'女')")
#若connection 没有设置自动提交 这里 插入的时候需要提交一下
#connect.commit()cursor.execute("select *from student")
result = cursor.fetchall()
print(f"type = {type(result)} result = {result}")
for r in result:print(r)print(connect.get_server_info())
connect.close()