Python安装,配置,连接MySQL

应用安装

  1. MySQL 8.0安装
    从下面地址下载mysql-8.0.12-winx64.zip(根据电脑选择不同版本)
    https://dev.mysql.com/downloads/mysql/
  2. Python数据库适配器安装
    cmd输入pip install MySQL-python==1.2.5
    (参考:https://pypi.org/project/MySQL-python/1.2.5/)。如果缺少Microsoft Visual C++依赖,可参考方法二;
    方法二:下载mysqlclient-1.3.13-cp35-cp35m-win_amd64.whl
    (地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python)
    cmd打开下载目录,输入命令:
    pip install mysqlclient-1.3.13-cp35-cp35m-win_amd64.whl
    方法三:
    使用pymysql,安装命令:python -m pip install pymysql

MySQL配置

  • 解压mysql-8.0.12-winx64.zip到安装目录,比如:D:\mysql
  • 添加配置文件my.ini
    注意:basedir和datadir根据实际情况填写
[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录
basedir=D:\mysql
# 设置mysql数据库的数据的存放目录
datadir=D:\mysql\data
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 服务端使用的字符集默认为UTF8
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8

初始化数据库

cd /d D:\mysql\bin
mysqld --initialize --console
结果如下:(注意记下root用户的初始密码)
C:\WINDOWS\system32>cd /d d:\mysql\bin

d:\mysql\bin>mysqld.exe --initialize --console
2018-09-01T02:35:35.925203Z 0 [System] [MY-013169] [Server] d:\mysql\bin\mysqld.exe (mysqld 8.0.12) initializing of server in progress as process 11180
2018-09-01T02:35:51.864676Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Corxg&Kug6(;
2018-09-01T02:36:02.383318Z 0 [System] [MY-013170] [Server] d:\mysql\bin\mysqld.exe (mysqld 8.0.12) initializing of server has completed

安装并开启MySQL服务

mysqld.exe --install mysql
net start mysql
结果如下:
d:\mysql\bin>mysqld.exe --install mysql
The service already exists!
The current server installed: D:\mysql\bin\mysqld.exe MySQL

d:\mysql\bin>net start mysql
MySQL 服务正在启动 ..
MySQL 服务已经启动成功。

修改初始密码为新密码:‘password’

mysql -u root -p
ALTER USER 'root'@'localhost'   IDENTIFIED WITH mysql_native_password   BY 'password';	
结果如下:
d:\mysql\bin>mysql -u root -p
Enter password: ************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.12

Copyright (c) 2000, 2018, 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.

mysql> ALTER USER 'root'@'localhost'
    ->   IDENTIFIED WITH mysql_native_password
    ->   BY 'password';
Query OK, 0 rows affected (0.08 sec)

mysql>


至此,MySQL配置完成。

Python适配器连接MySQL

>>> import MySQLdb
>>> cxn = MySQLdb.connect(user='root', password='password')
>>> cxn.query('CREATE DATABASE test')
>>> cxn.query('GRANT ALL ON test.* to "root"@"localhost"')
>>> cxn.commit()
>>> cxn.close()

MySQL配置官方文档:

https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password

猜你喜欢

转载自blog.csdn.net/lylfv/article/details/82284750