Home >Database >Mysql Tutorial >What is mysql-connector

What is mysql-connector

WBOY
WBOYOriginal
2022-05-12 16:04:548900browse

"mysql-connector" is the driver officially provided by mysql, which can be used to connect and use mysql; you can use the "pip install mysql-connector" command to install it, and use "import mysql.connector" to test whether the installation is successful.

What is mysql-connector

The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.

What is mysql-connector

Use mysql-connector to connect to MySQL. mysql-connector is the driver officially provided by MySQL.

We can use the pip command to install mysql-connector:

python -m pip install mysql-connector

Use the following code to test whether the mysql-connector is installed successfully:

demo_mysql_test.py:
import mysql.connector

Execute the above code, if no error is generated , indicating that the installation is successful.

Note: If your MySQL is version 8.0, the password plug-in verification method has changed. The early version is mysql_native_password, and the 8.0 version is caching_sha2_password, so you need to make some changes:

Modify my. ini configuration:

[mysqld]
default_authentication_plugin=mysql_native_password

Then execute the following command under mysql to change the password:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';

The example is as follows:

Create a database connection

Yes Use the following code to connect to the database:

demo_mysql_test.py:
import mysql.connector
 
mydb = mysql.connector.connect(
  host="localhost",       # 数据库主机地址
  user="yourusername",    # 数据库用户名
  passwd="yourpassword"   # 数据库密码
)
 
print(mydb)

Recommended learning: mysql video tutorial

The above is the detailed content of What is mysql-connector. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn