python数据库编程,
安装mysql
yum search mariadb
查找与mariadb有关的软件包
yum install mariadb mariadb-server -y
安装mariadb的server软件和client软件
- 启动mariadb服务
systemctl start mariadb
systemctl enable mariadb
- mariadb监听的端口
netstat -antlpe | grep mysql
ss -antlpe | grep mysql
vim /etc/services
所有服务与端口默认的对应关系 - 只允许本地连接,阻断所有来自网络的连接
vim /etc/my.cnf
skip-networking=1
systemctl restart mariadb
mariadb的初始化
- 设置mysql的登陆密码
mysql_secure_installation
mysql -uroot -p*****
- mysql基本操作语句
show databases;
显示数据库,类似于目录,里面包含多个表
use mysql;
进入名称为mysql的数据库
show tables;
显示该数据库中的表
desc user;
显示表的结构
select * from user;
显示user表中的内容
select Host,User,Password from user;
显示表中某几列
create database students;
创建数据库名称为students
创建表create table student( name varchar(10) not null, score varchar(6) not null );
insert into student values ('stu1','123');
向表中插入内容
insert into student(score,name) values("456","stu2");
按照指定顺序向表中插入数据
update student set score='456' where name="stu1";
更新表中的内容
alter table student add sex varchar(3);
添加sex列到 student 表中
delete from student where name="stu1";
删除表中用户名为stu1的记录
drop table student;
删除表
drop database students;
删除数据库
用户和访问权限的操作
–
create user hello@localhost identified by 'hello';
创建用户hello,可在本机登陆,密码为hello
create user hello@'%' identified by 'hello';
创建用户hello,可在远程登陆,密码为hello
create database mariadb;
创建一数据库mariadb,对普通用户进行访问权限操作
grant all on mariadb.* to hello@localhost;
给hello@localhost用户授权,如果为all,授权所有权(insert,update,delete,select,create)
flush privileges;
刷新,重载授权表
show grants for hello@localhost;
查看用户授权
revoke delete,update on mariadb.* from hello@localhost;
删除指定用户授权
drop user hello@localhost;
删除用户
找回mysql用户密码
mysql
> update mysql.user set Password=password('hello') where User='root';
- 关闭跳过授权表的进程,启动mariadb服务,使用新密码即可
ps aux | grep mysql
kill -9 pid
mysql -uroot -phello
mysql8.0密码找回
### 关闭mysqld服务
systemctl stop mysqld
### 编辑/etc/my.cnf ,跳过验证
文件末尾添加 skip-grant-tables
### 重启mysqld服务
systemctl start mysqld
### 登陆mysql ,并将root密码置空
# mysql
> use mysql
> select host, user, authentication_string, plugin from user;
> update user set authentication_string='' where user='root';
### 退出mysql,并修改/etc/my.cnf文件,将添加部分删除
### 重启mysqld服务
systemctl restart mysqld
### 登陆mysql ,更改密码
> ALTER user 'root'@'localhost' IDENTIFIED BY '(登陆密码)';
### 若遇到ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
可通过
> set global validate_password.policy=0;
## 修改密码强度
> set global validate_password.length=6;
## 密码最小长度
mysql的备份与恢复
备份:
mysqldump -uroot -phello mariadb >mariadb.dump
mysqldump -uroot -phello --no-data mariadb > `date +%Y_%m_%d`_mariadb.dump
mysqldump -uroot -phello --all-databases >mariadb4.dump
恢复:
mysqladmin -uroot -phello create mariadb2
mysql -uroot -phello mariadb2< mariadb.dump
python连接数据库
python2 中 ,import MySQLdb
python3 中 ,import pymysql
连接数据库
conn = pymysql.connect(host=主机地址, user=用户名, password=密码, charset='utf8', autocommit=True)
创建游标,用来给数据库发送sql语句
cur = conn.cursor()
选择要操作的数据库
conn.selet_db('mysql')
在连接数据库时可直接指定要操作的数据库,用法 db = 'mysql'
进行操作
cur.execute(操作语句)
在对数据库进行操作后,需要使用conn.commit()
提交对数据库的操作,如果设置了自动提交autocommit=True
,则不需要进行手动提交
在操作完成后,需要关闭游标cur.close()
,关闭数据库连接conn.close()
在对数据库进行操作时,如果有多条数据,可以通过 executemany 方法直接传入列表,实现多行数据添加
- 查看表内容
cur.fetchone()
查看一条信息, 指针向后移动;
cur.fetchmany(n)
显示n条信息
cur.fetchall()
显示所有信息
cur.scroll(value, mode)
移动指针,value 表示要移动的位,当mode
为relative
时,为相对当前指针移动,当mode
为absolute
时,为绝对移动,如移动到表头为cur.scorll(0, 'absolute')
- 数据库的数据回滚操作
数据回滚要求不能自动提交对数据库的操作,使用conn.rollback()
对数据库数据进行回滚,提交过的数据不能被回滚
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。