Mysql
一、linux
1.安装(linux)
下载
mysql下载地址 https://dev.mysql.com/downloads/mysql/
,选择mysql版本和服务器型号
解压软件包
tar -xf mysql-server_8.0.27-1ubuntu18.04_amd64.deb-bundle.tar
libmysqlclient21_8.0.27-1ubuntu18.04_amd64.deb
libmysqlclient-dev_8.0.27-1ubuntu18.04_amd64.deb
mysql-client_8.0.27-1ubuntu18.04_amd64.deb
mysql-common_8.0.27-1ubuntu18.04_amd64.deb
mysql-community-client_8.0.27-1ubuntu18.04_amd64.deb
mysql-community-client-core_8.0.27-1ubuntu18.04_amd64.deb
mysql-community-client-plugins_8.0.27-1ubuntu18.04_amd64.deb
mysql-community-server_8.0.27-1ubuntu18.04_amd64.deb
mysql-community-server-core_8.0.27-1ubuntu18.04_amd64.deb
mysql-community-server-debug_8.0.27-1ubuntu18.04_amd64.deb
mysql-community-test_8.0.27-1ubuntu18.04_amd64.deb
mysql-community-test-debug_8.0.27-1ubuntu18.04_amd64.deb
mysql-server_8.0.27-1ubuntu18.04_amd64.deb
mysql-server_8.0.27-1ubuntu18.04_amd64.deb-bundle.tar
mysql-testsuite_8.0.27-1ubuntu18.04_amd64.deb
按以下顺序执行
sudo dpkg -i mysql-common_8.0.27-1ubuntu18.04_amd64.deb
sudo dpkg-preconfigure mysql-community-server_8.0.27-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-community-client-plugins_8.0.27-1ubuntu18.04_amd64.deb
sudo dpkg -i libmysqlclient21_8.0.27-1ubuntu18.04_amd64.deb
sudo dpkg -i libmysqlclient-dev_8.0.27-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-community-client-core_8.0.27-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-community-client_8.0.27-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-client_8.0.27-1ubuntu18.04_amd64.deb
#sudo dpkg -i libmecab2_0.996-5_amd64.deb
sudo dpkg -i mysql-community-server-core_8.0.27-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-community-server_8.0.27-1ubuntu18.04_amd64.deb
安装详细过程
sudo dpkg -i mysql-common_8.0.27-1ubuntu18.04_amd64.deb
正在选中未选择的软件包 mysql-common。
(正在读取数据库 ... 系统当前共安装有 224289 个文件和目录。)
正准备解包 mysql-common_8.0.27-1ubuntu18.04_amd64.deb ...
正在解包 mysql-common (8.0.27-1ubuntu18.04) ...
正在设置 mysql-common (8.0.27-1ubuntu18.04) ...
update-alternatives: 使用 /etc/mysql/my.cnf.fallback 来在自动模式中提供 /etc/mysql/my.cnf (my.cnf)
sudo dpkg-preconfigure mysql-community-server_8.0.27-1ubuntu18.04_amd64.deb
此时,会进入以下设置
sudo dpkg -i mysql-community-client-plugins_8.0.27-1ubuntu18.04_amd64.deb
正在选中未选择的软件包 mysql-community-client-plugins。
(正在读取数据库 ... 系统当前共安装有 224303 个文件和目录。)
正准备解包 mysql-community-client-plugins_8.0.27-1ubuntu18.04_amd64.deb ...
正在解包 mysql-community-client-plugins (8.0.27-1ubuntu18.04) ...
正在设置 mysql-community-client-plugins (8.0.27-1ubuntu18.04) ...
正在处理用于 libc-bin (2.27-3ubuntu1.4) 的触发器 ...
sudo dpkg -i libmysqlclient21_8.0.27-1ubuntu18.04_amd64.deb
正在选中未选择的软件包 libmysqlclient21:amd64。
(正在读取数据库 ... 系统当前共安装有 224317 个文件和目录。)
正准备解包 libmysqlclient21_8.0.27-1ubuntu18.04_amd64.deb ...
正在解包 libmysqlclient21:amd64 (8.0.27-1ubuntu18.04) ...
正在设置 libmysqlclient21:amd64 (8.0.27-1ubuntu18.04) ...
正在处理用于 libc-bin (2.27-3ubuntu1.4) 的触发器 ...
sudo dpkg -i libmysqlclient-dev_8.0.27-1ubuntu18.04_amd64.deb
正在选中未选择的软件包 libmysqlclient-dev。
(正在读取数据库 ... 系统当前共安装有 224325 个文件和目录。)
正准备解包 libmysqlclient-dev_8.0.27-1ubuntu18.04_amd64.deb ...
正在解包 libmysqlclient-dev (8.0.27-1ubuntu18.04) ...
正在设置 libmysqlclient-dev (8.0.27-1ubuntu18.04) ...
正在处理用于 man-db (2.8.3-2ubuntu0.1) 的触发器 ...
sudo dpkg -i mysql-community-client-core_8.0.27-1ubuntu18.04_amd64.deb
正在选中未选择的软件包 mysql-community-client-core。
(正在读取数据库 ... 系统当前共安装有 224356 个文件和目录。)
正准备解包 mysql-community-client-core_8.0.27-1ubuntu18.04_amd64.deb ...
正在解包 mysql-community-client-core (8.0.27-1ubuntu18.04) ...
正在设置 mysql-community-client-core (8.0.27-1ubuntu18.04) ...
正在处理用于 man-db (2.8.3-2ubuntu0.1) 的触发器 ...
sudo dpkg -i mysql-community-client_8.0.27-1ubuntu18.04_amd64.deb
正在选中未选择的软件包 mysql-community-client。
(正在读取数据库 ... 系统当前共安装有 224368 个文件和目录。)
正准备解包 mysql-community-client_8.0.27-1ubuntu18.04_amd64.deb ...
正在解包 mysql-community-client (8.0.27-1ubuntu18.04) ...
正在设置 mysql-community-client (8.0.27-1ubuntu18.04) ...
正在处理用于 man-db (2.8.3-2ubuntu0.1) 的触发器 ...
sudo dpkg -i mysql-client_8.0.27-1ubuntu18.04_amd64.deb
正在选中未选择的软件包 mysql-client。
(正在读取数据库 ... 系统当前共安装有 224417 个文件和目录。)
正准备解包 mysql-client_8.0.27-1ubuntu18.04_amd64.deb ...
正在解包 mysql-client (8.0.27-1ubuntu18.04) ...
正在设置 mysql-client (8.0.27-1ubuntu18.04) ...
sudo dpkg -i mysql-community-server-core_8.0.27-1ubuntu18.04_amd64.deb
(正在读取数据库 ... 系统当前共安装有 224561 个文件和目录。)
正准备解包 mysql-community-server-core_8.0.27-1ubuntu18.04_amd64.deb ...
正在将 mysql-community-server-core (8.0.27-1ubuntu18.04) 解包到 (8.0.27-1ubuntu18.04) 上 ...
正在设置 mysql-community-server-core (8.0.27-1ubuntu18.04) ...
正在处理用于 libc-bin (2.27-3ubuntu1.4) 的触发器 ...
正在处理用于 man-db (2.8.3-2ubuntu0.1) 的触发器 ...
若提示缺少依赖的软件包libaio1和libmecab2,此时使用sudo apt-get -f install
将会自动安装libaio1、libmecab2,然后在尝试安装 mysql-community-server-core_8.0.27-1ubuntu18.04_amd64.deb
dpkg: 依赖关系问题使得 mysql-community-server-core 的配置工作不能继续:
mysql-community-server-core 依赖于 libaio1 (>= 0.3.93);然而:
未安装软件包 libaio1。
mysql-community-server-core 依赖于 libmecab2;然而:
未安装软件包 libmecab2。
sudo apt-get -f install
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
正在修复依赖关系... 完成
将会同时安装下列软件:
libaio1 libmecab2
下列【新】软件包将被安装:
libaio1 libmecab2
sudo dpkg -i mysql-community-server_8.0.27-1ubuntu18.04_amd64.deb
正在选中未选择的软件包 mysql-community-server。
(正在读取数据库 ... 系统当前共安装有 224561 个文件和目录。)
正准备解包 mysql-community-server_8.0.27-1ubuntu18.04_amd64.deb ...
正在解包 mysql-community-server (8.0.27-1ubuntu18.04) ...
正在设置 mysql-community-server (8.0.27-1ubuntu18.04) ...
update-alternatives: 使用 /etc/mysql/mysql.cnf 来在自动模式中提供 /etc/mysql/my.cnf (my.cnf)
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
2.测试
查看mysql状态systemctl status mysql
以root用户登入 mysql -u root -p
查看默认数据库show databases;
,MySQL安装好后有默认的这几个数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
查看mysql数据库show tables from mysql;
,mysql里有user表,这个表里放了user、host相关的一些信息。
mysql> show tables from mysql;
+------------------------------------------------------+
| Tables_in_mysql |
+------------------------------------------------------+
| columns_priv |
| component |
| db |
| default_roles |
| engine_cost |
| func |
| general_log |
| global_grants |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| password_history |
| plugin |
| procs_priv |
| proxies_priv |
| replication_asynchronous_connection_failover |
| replication_asynchronous_connection_failover_managed |
| replication_group_configuration_version |
| replication_group_member_actions |
| role_edges |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+------------------------------------------------------+
37 rows in set (0.01 sec)
3.配置
开启远程登入
mysql默认不允许远程登录,所以需要开启远程访问权限
- 先查看mysql中的user表
select User, Host from mysql.user;
mysql> select User, Host from mysql.user;
+------------------+-----------+
| User | Host |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
- 默认都是localhost, 把root调整为远程登陆用户
update mysql.user set host = '%' where user = 'root';
,并再次查看user表
mysql> update mysql.user set host = '%' where user = 'root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select User, Host from mysql.user;
+------------------+-----------+
| User | Host |
+------------------+-----------+
| root | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
- 刷新修改
FLUSH PRIVILEGES;
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
开启防火墙
二、window
1.安装(window)
下载
mysql下载地址 https://dev.mysql.com/downloads/mysql/
,选择Window系统及Mysql型号
安装详细过程
选择Server only,只安装mysql
检测需要安装依赖环境,点击execute,安装完成后即可点击next
开始安装
点击next
端口号等保持默认即可,next
密码验证方式,选择第二种。因为我们后面使用mysql客户端navicat版本是9.X,它链接mysql用的是就是这个加密算法,所以这一步很重要。
设置root用户密码,以及添加账号
window服务注册
执行安装
完成安装
安装后产品配置
空
保存安装日志
2.测试(MySQL 8.0 Command Line Client)
点击菜单中的MySQL 8.0 Command Line Client
,输入密码后,显示以下内容则表示安装成功。
3.测试(命令行)
在命令行中使用mysql命令需要配置环境变量,将命令所在的路径配置成系统环境变量或用户环境变量
寻找路径,这里为C:\Program Files\MySQL\MySQL Server 8.0\bin
添加环境变量
命令行以root用户登入mysql,mysql -u root -p
,如果显示以下内容则表示配置成功。
4.错误解决
1042错误
windows端使用Win+R --> 运行 “services.msc” --> 打开service服务管理器,找到刚才安装mysql的服务名称
右键 --> 属性 --> 登录,更改成“本地系统账户” --> 确定
启动服务
登入闪退
在使用菜单中的MySQL 8.0 Command Line Client
进行登入,输入密码后闪退。
解决:不输入密码,直接按回车健,登入后在修改密码(见 3.2)
三、mysql设置
1.登入
使用语句mysql -u root -p
2.修改密码(root)
#旧版本
update mysql.user set authentication_string=password("密码") where user="root";
#新版本
ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';
3.显示数据库
列出MySQL数据库管理系统的数据库列表show databases;
,MySQL安装好后有以下几个默认的数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql:mysql核心数据库,该库主要存储数据库用户、权限等mysql自身需要使用的信息
performance_schema:用于收集数据库服务器性能参数
information_schema:保存着关于mysql服务器所维护的所有其他数据库的信息
sys:是一个简单版的performance_schema
4.显示数据表
显示指定数据库的所有表show tables;
,使用该命令前需要使用 use 数据库名;
命令来选择要操作的数据库。
mysql> use mysql
Database changed
mysql> show tables;
+------------------------------------------------------+
| Tables_in_mysql |
+------------------------------------------------------+
| columns_priv |
| component |
| db |
| default_roles |
| ....................
| user |
+------------------------------------------------------+
37 rows in set (0.15 sec)
5.添加用户
在 mysql 数据库中的 user 表存储所有的用户信息,对其进行增添或查找即可添加或查询用户信息
查看所有用户select host, user,substring(authentication_string,1,13), select_priv, insert_priv, update_priv, delete_priv, create_priv from user;;
,
host:表示允许登入该用户的主机地址,localhost:只允许该用户在本地登录,不能远程登录。%:允许在除本机之外的任何一台机器远程登录。192.168.0.0:具体的IP表示只允许该用户从特定IP登录。
select_priv等:表示用户所具有的权限
authentication_string:表示经过编码后的密码,这里使用substring(*,1,13)从第1个字符开始截取13个字符显示
mysql> use mysql;
Database changed
mysql> select host, user,substring(authentication_string,1,13), select_priv, insert_priv, update_priv, delete_priv, create_priv from user;
+-----------+------------------+---------------------------------------+-------------+-------------+-------------+-------------+-------------+
| host | user | substring(authentication_string,1,13) | select_priv | insert_priv | update_priv | delete_priv | create_priv |
+-----------+------------------+---------------------------------------+-------------+-------------+-------------+-------------+-------------+
| localhost | mysql.infoschema | $A$005$THISIS | Y | N | N | N | N |
| localhost | mysql.session | $A$005$THISIS | N | N | N | N | N |
| localhost | mysql.sys | $A$005$THISIS | N | N | N | N | N |
| localhost | root | $A$005$7Ba | Y | Y | Y | Y | Y |
+-----------+------------------+---------------------------------------+-------------+-------------+-------------+-------------+-------------+
4 rows in set (0.00 sec)
添加用户并赋予限
#以前的操作(以插入user表的方式添加用户)
insert into mysql.user(Host,User,authentication_string) values("localhost","lin",password("1234"));
#新版本操作(不用以插入user表的方式添加用户)
##1.创建用户(%表示允许所有远程主机以此用户登入,192.168.4.% 网段内的所有主机,192.168.4.1 只允许该主机,localhost 只能本地登入)
mysql> create user lin@"%" identified by "123456";
Query OK, 0 rows affected (0.48 sec)
##2.赋予所有权限(将所有库所有表的全部权限都赋予lin用户)
mysql> grant all on *.* to lin@"%" with grant option;
Query OK, 0 rows affected (0.32 sec)
mysql> flush privileges; # 刷新
Query OK, 0 rows affected (0.06 sec)
##3.赋予部分权限(将mysql库下所有表的查询权限赋予lin用户)
## all(所有权限), usage(无权限), select,update,insert(个别权限)
grant (操作权限) on 库名.表名 to lin@"%" with grant option;
grant select on mysql.* to lin@"%" with grant option;
再次查看用户表
mysql> select host, user,substring(authentication_string,1,5), select_priv, insert_priv, update_priv, delete_priv, create_priv from user;
+-----------+------------------+--------------------------------------+-------------+-------------+-------------+-------------+-------------+
| host | user | substring(authentication_string,1,5) | select_priv | insert_priv | update_priv | delete_priv | create_priv |
+-----------+------------------+--------------------------------------+-------------+-------------+-------------+-------------+-------------+
| % | lin | $A$00 | Y | Y | Y | Y | Y |
| localhost | mysql.infoschema | $A$00 | Y | N | N | N | N |
| localhost | mysql.session | $A$00 | N | N | N | N | N |
| localhost | mysql.sys | $A$00 | N | N | N | N | N |
| localhost | root | $A$00 | Y | Y | Y | Y | Y |
+-----------+------------------+--------------------------------------+-------------+-------------+-------------+-------------+-------------+
5 rows in set (0.00 sec)
使用用户lin登入
C:\Users\lin>mysql -u lin -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.0.27 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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>
6.创建数据库和数据表
# 创建数据库test
mysql> create database test;
Query OK, 1 row affected (0.22 sec)
# 创建数据表users
# auto_increment表示自增+1,primary key表示主键
mysql> use test;
Database changed
mysql> create table users(id int auto_increment primary key, name varchar(20), ped varchar(64));
Query OK, 0 rows affected (2.80 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| users |
+----------------+
1 row in set (0.05 sec)
# 插入数据
mysql> insert into users(name,pwd) values('lin','123456');
Query OK, 1 row affected (0.37 sec)
# 查看表内容
mysql> select * from users;
+----+------+--------+
| id | name | pwd |
+----+------+--------+
| 1 | lin | 123456 |
+----+------+--------+
1 row in set (0.00 sec)
# 查看表结构
mysql> desc users;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| pwd | varchar(64) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.21 sec)
四、使用python操作
直接连接数据库,pymysql直接连接数据库进行操作(cursor=pymysql.cursors.DictCursor
表示以字典形式输出)
安装 pymysql包 pip install PyMySQL
(base) C:\Users\lin>pip install PyMySQL
Collecting PyMySQL
Downloading PyMySQL-1.0.2-py3-none-any.whl (43 kB)
|████████████████████████████████| 43 kB 108 kB/s
Installing collected packages: PyMySQL
Successfully installed PyMySQL-1.0.2
import pymysql
conn = pymysql.connect(host='localhost', port=3306, user='root', password='123456', database='test')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute("select * from users where name=%s and pwd=%s", ['lin', '123456'])
result = cursor.fetchone()
conn.commit()
cursor.close()
conn.close()
print(result) # {'id': 1, 'name': 'lin', 'pwd': '123456'}
连接池连接数据库,pymysql+DbUtils(数据库连接池)进行操作
安装DbUtils包 pip install DBUtils
(base) C:\Users\lin>pip install DBUtils
Collecting DBUtils
Downloading DBUtils-2.0.2-py3-none-any.whl (32 kB)
Installing collected packages: DBUtils
Successfully installed DBUtils-2.0.2
import pymysql
from dbutils.pooled_db import PooledDB
POOL = PooledDB(
creator=pymysql, # 使用链接数据库的模块
maxconnections=6, # 连接池允许的最大连接数,0和None表示不限制连接数
mincached=2, # 初始化时,链接池中至少创建的空闲的链接,0表示不创建
maxcached=5, # 链接池中最多闲置的链接,0和None不限制
maxshared=3,
# 链接池中最多共享的链接数量,0和None表示全部共享。PS: 无用,因为pymysql和MySQLdb等模块的 threadsafety都为1,所有值无论设置为多少,_maxcached永远为0,所以永远是所有链接都共享。
blocking=True, # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
maxusage=None, # 一个链接最多被重复使用的次数,None表示无限制
setsession=[], # 开始会话前执行的命令列表。如:["set datestyle to ...", "set time zone ..."]
ping=0,
# ping MySQL服务端,检查是否服务可用。# 如:0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always
host='127.0.0.1',
port=3306,
user='root',
password='123456',
database='test',
charset='utf8'
)
# conn = pymysql.connect(host='localhost', port=3306, user='root', password='123456', database='test')
conn = POOL.connection()
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute("select * from users where name=%s and pwd=%s", ['lin', '123456'])
result = cursor.fetchall()
conn.close()
print(result) # [{'id': 1, 'name': 'lin', 'pwd': '123456'}]