python-pymysql安装和数据库安装

python-pymysql安装和数据库安装

1. pymysql安装

  • 安装python连接数据库pymysql模块
  [root@python ~]#  pip3 install PyMySQl   
  Looking in indexes: http://mirrors.aliyun.com/pypi/simple/   Collecting PyMySQl    Downloading http://mirrors.aliyun.com/pypi/packages/2c/57/af502e0e113f139b3f3add4f1efba899a730a365d2264d476e85b9591da5/PyMySQL-0.10.0-py2.py3-none-any.whl   (47kB)     100% |████████████████████████████████|   51kB 676kB/s    Installing collected   packages: PyMySQl   Successfully   installed PyMySQl-0.10.0   You are using pip   version 19.0.3, however version 20.2b1 is available.   You should consider   upgrading via the 'pip install --upgrade pip' command.   

2. 安装数据库

2.1 安装mysql服务器

[root@python ~]# yum   install mariadb-server -y   
[root@python ~]# /bin/systemctl start mariadb   
[root@python ~]# /bin/systemctl enable mariadb   
Created symlink from   /etc/systemd/system/multi-user.target.wants/mariadb.service to   /usr/lib/systemd/system/mariadb.service. 

2.2 mysql数据库权限开通

[root@python ~]#   mysql -h 127.0.0.1 -A   
MariaDB [(none)]>   create database test;   

MariaDB [(none)]>   CREATE USER 'test'@'127.0.0.1' IDENTIFIED BY 'Test@963852';   
Query OK, 0 rows   affected (0.01 sec)   

MariaDB [(none)]>   grant all on test.* to   'test'@'127.0.0.1';   
Query OK, 0 rows   affected (0.00 sec)       

MariaDB [(none)]>   flush privileges;   
Query OK, 0 rows   affected (0.00 sec)       

[root@python ~]#   mysql -h 127.0.0.1 -u test -p'Test@963852'   
Welcome to the   MariaDB monitor. Commands end with ;   or \g.   
Your MariaDB   connection id is 4   
Server version:   5.5.65-MariaDB MariaDB Server       
Copyright (c) 2000,   2018, Oracle, MariaDB Corporation Ab and others.       
Type 'help;' or '\h'   for help. Type '\c' to clear the current input statement.       
MariaDB [(none)]>   show databases;   
+--------------------+   
| Database      |   
+--------------------+   
| information_schema   |   
| test        |   
+--------------------+   
2 rows in set (0.00   sec)   

2.3 mysql数据库准备,需要有mysql基础

[root@python tmp]#   mysql -h 127.0.0.1 -u test -p'Test@963852'   
Welcome to the   MariaDB monitor. Commands end with ;   or \g.   
Your MariaDB   connection id is 8   
Server version:   5.5.65-MariaDB MariaDB Server       
Copyright (c) 2000,   2018, Oracle, MariaDB Corporation Ab and others.       
Type 'help;' or '\h'   for help. Type '\c' to clear the current input statement.       

MariaDB [(none)]> use test
Database changed

# 执行以下创建表
MariaDB [test]> show tables;
Empty set (0.00 sec)
     

MariaDB [test]> create table user(
    -> id bigint unsigned auto_increment,
    -> username varchar(100) not null,
    -> password varchar(100) not null,
    -> primary key (id)
    -> )ENGINE=innoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.05 sec) 

2.4 手动插入数据,开始验证

MariaDB [test]> INSERT INTO user(id, username, PASSWORD ) values ( 1,'test', 'test01' );
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> select * from user;
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  1 | test     | test01   |
+----+----------+----------+
1 row in set (0.00 sec)

posted @ 2023-01-13 10:29  七月流星雨  阅读(377)  评论(0编辑  收藏  举报