Python 操作mysql
今天在操作在python中操作mysql ,发现一直连接不成功,困扰了很久,查了各种资料,发现都解决不了。但是通过命令行操作时,都是正常的。
我先在客户端使用命令行,操作mysql,查询账号密码是否一致,经检查,是一致的。
在命令行中查询账号密码的操作如下:
[root@localhost ~]# mysql -uroot -p123456
[root@localhost ~]# mysql -hlocalhost -uroot -p123456
如果忘记密码,则跳过MySQL的密码认证过程。步骤如下:
修改Mysql配置文件:vi /etc/my.cnf(注:windows下修改的是my.ini)。在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程。
重启Mysql:
进入Mysql:[root@localhost ~]# mysql -uroot -p
2、使用mysql数据库,从user表中查看主机,用户名,密码
-- 使用mysql数据库
mysql> use mysql;
-- 查询主机用户名密码:5.7版本之前的
mysql> select host,user,plugin,password from user;
-- 查询主机用户名密码:5.7版本之后的,包括5.7
mysql> select host,user,plugin,authentication_string from user;
mysql> select host,user,plugin,authentication_string from user\G;
mysql> select host,user,plugin,authentication_string from mysql.user;
3、修改密码,刷新一下权限
mysql> update user set password=password("新密码") where user="root";
mysql> flush privileges;
mysql> quit
上面修改密码是在5.7版本之前的。若是5.7版本之后的(包括5.7),没有password这个字段了,则修改方法如下:
mysql> alter user "root"@"localhost" identified by "新密码"; --方法1
mysql> update user set authentication_string=password("新密码") where user="root"; --方法2
mysql> flush privileges;
mysql> quit
4、如果以上不能解决密码修改,则使用下面方法
mysql> use mysql;
mysql> alter user "root"@"localhost" identified with mysql_native_password by "新密码";
mysql> flush privileges;
修改加密规则:mysql> alter user "root"@"localhost" identified by 'password' PASSWORD EXPIRE NEVER;
如果执行以上的操作并没有解决,请再把default_authentication_plugin=mysql_native_password添加到配置中。
5、再去编辑一下my.cnf配置文件,去掉skip-grant-tables。
回到python中,一直无法正常连接mysql的问题,经过资料查阅,发现自己使用的是
import mysql.connector
连接指令是 db = mysql.connector.connect(....)
当我import pymysql
连接指令使用db = pymysql.connect(....)
一切正常。
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
# import mysql.connector
import pymysql
db = pymysql.connect(
host="localhost",
port= 3306,
user="xxxx",
password = "xxxxxxx",
database="xxxxxx"
)
# mycursor.execute("SHOW DATABASES")
cursor = db.cursor()
cursor.execute("CREATE DATABASE testhello2")
print(3)
cursor.execute("CREATE TABLE hello2 (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))")
print(4)
# 插入记录
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
values = [("John Smith", "123 Main St"), ("Alice Johnson", "456 Elm St")]
cursor.executemany(sql, values)
db.commit()
# 查询记录
cursor.execute("SELECT * FROM customers")
results = cursor.fetchall()
for row in results:
print(row)
# 更新记录
sql = "UPDATE customers SET address = %s WHERE name = %s"
values = ("789 Oak St", "Alice Johnson")
cursor.execute(sql, values)
db.commit()
# 删除记录
sql = "DELETE FROM customers WHERE name = %s"
values = ("John Smith",)
cursor.execute(sql, values)
db.commit()
# 关闭游标和数据库连接
cursor.close()
db.close()
# # Press the green button in the gutter to run the script.
# if __name__ == '__main__':
# pass
#
# # See PyCharm help at https://www.jetbrains.com/help/pycharm/