python操作mysql数据库系列-安装MySql数据库

Posted 测试老兵

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python操作mysql数据库系列-安装MySql数据库相关的知识,希望对你有一定的参考价值。

1.首先下载mysql我选择的版本是5.7.19 基于64位的windows操作系统。mysql的下载地址传送门:https://dev.mysql.com/downloads/mysql/5.7.html#downloads   这里面有不同操作系统的安装包大家有需要都可以自行去下载。

具体安装如下:

1、把 mysql-5.7.20-winx64zip 压缩文件解压到 D:\\application\\mysql 目录下;

 

2、在  D:\\application\\mysql目录下新建 my.ini 配置文件;

 

3、用文本编辑器或其他编辑器打开 my.ini 文件,把以下代码复制粘贴进去,保存退出;

#代码开始
[Client]
#设置3306端口
port = 3306

[mysqld]
#设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=D:\\application\\mysql
# 设置mysql数据库的数据的存放目录
datadir=D:\\application\\mysql\\data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
#代码结束

4.1、新建系统变量并配置变量值为 D:\\application\\mysql ;

管理员身份运行命令提示符cmd(一定要用管理员身份运行,不然权限不够)输入如下命令:

mysqld --defaults-file=my.ini --initialize-insecure
mysqld --install

 

开始mysql服务:net start mysql      关闭mysql服务: net stop mysql 

设置 mysql 的 root 密码,运行以下指令:mysql -u root -p   弹出的密码框可以咱时不用输入密码,直接按回车键 enter键  即可出现下面的页面。

 

然后我们修改一下mysql的登录密码:下面的代码逐一运行。

mysql -u root -p
use mysql;
update user set authentication_string=password(\'您的密码\') where user=\'root\';
flush privileges;
exit

至此,Mysql 8.0 winx64 解压缩版配置安装已经完成!

use (database name)即可

 

初次使用的时候,可以无法查询数据库中的表,等信息需要先创建一个数据库,在使用。这个报错原因是没有选择数据库! 首先建立数据库,再在其中建立数据表:

创建的库为ruifeng,但是里面目前还没有表。

下面这些命令mysql常用的命令:

# status 状态
# net start mysql
# net stop  mysql
# mysql -h localhost -u root -p  使用该命令访问mysql
# show databases; 查看mysql有哪些库
# use db; 切换数库
# show tables;查看有哪些表
# desc xxx; 查看表中有哪些字段
# show create table user \\G: 查看创建表的脚本 信息

 1.show databases;查看有哪些库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bjpowernode        |
| mysql              |
| performance_schema |
| ruifeng            |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

 2.use mysql  使用mysql库

mysql> use mysql
Database changed
mysql> show tables;

3.show tables查看mysql库下面有哪些表

mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| 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                      |
+---------------------------+

4.desc 字段;查看表中有哪些字段;

mysql> desc proc;
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+---------------------+-----------------------------+
| Field                | Type                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | Null | Key | Default             | Extra                       |
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+---------------------+-----------------------------+
| db                   | char(64)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | NO   | PRI |                     |                             |
| name                 | char(64)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | NO   | PRI |                     |                             |
| type                 | enum(\'FUNCTION\',\'PROCEDURE\')                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        | NO   | PRI | NULL                |                             |
| specific_name        | char(64)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | NO   |     |                     |                             |
| language             | enum(\'SQL\')                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         | NO   |     | SQL                 |                             |
| sql_data_access      | enum(\'CONTAINS_SQL\',\'NO_SQL\',\'READS_SQL_DATA\',\'MODIFIES_SQL_DATA\')                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | NO   |     | CONTAINS_SQL        |                             |
| is_deterministic     | enum(\'YES\',\'NO\')                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | NO   |     | NO                  |                             |
| security_type        | enum(\'INVOKER\',\'DEFINER\')                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | NO   |     | DEFINER             |                             |
| param_list           | blob                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | NO   |     | NULL                |                             |
| returns              | longblob                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | NO   |     | NULL                |                             |
| body                 | longblob                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | NO   |     | NULL                |                             |
| definer              | char(93)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | NO   |     |                     |                             |
| created              | timestamp                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | NO   |     | CURRENT_TIMESTAMP   | on update CURRENT_TIMESTAMP |
| modified             | timestamp                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | NO   |     | 0000-00-00 00:00:00 |                             |
| sql_mode             | set(\'REAL_AS_FLOAT\',\'PIPES_AS_CONCAT\',\'ANSI_QUOTES\',\'IGNORE_SPACE\',\'NOT_USED\',\'ONLY_FULL_GROUP_BY\',\'NO_UNSIGNED_SUBTRACTION\',\'NO_DIR_IN_CREATE\',\'POSTGRESQL\',\'ORACLE\',\'MSSQL\',\'DB2\',\'MAXDB\',\'NO_KEY_OPTIONS\',\'NO_TABLE_OPTIONS\',\'NO_FIELD_OPTIONS\',\'MYSQL323\',\'MYSQL40\',\'ANSI\',\'NO_AUTO_VALUE_ON_ZERO\',\'NO_BACKSLASH_ESCAPES\',\'STRICT_TRANS_TABLES\',\'STRICT_ALL_TABLES\',\'NO_ZERO_IN_DATE\',\'NO_ZERO_DATE\',\'INVALID_DATES\',\'ERROR_FOR_DIVISION_BY_ZERO\',\'TRADITIONAL\',\'NO_AUTO_CREATE_USER\',\'HIGH_NOT_PRECEDENCE\',\'NO_ENGINE_SUBSTITUTION\',\'PAD_CHAR_TO_FULL_LENGTH\') | NO   |     |                     |                             |
| comment              | text                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | NO   |     | NULL                |                             |
| character_set_client | char(32)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | YES  |     | NULL                |                             |
| collation_connection | char(32)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | YES  |     | NULL                |                             |
| db_collation         | char(32)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | YES  |     | NULL                |                             |
| body_utf8            | longblob                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | YES  |     | NULL                |                             |
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+---------------------+-----------------------------+
20 rows in set (0.00 sec)

mysql>

5.show create table user \\G: 查看创建表的脚本信息

mysql> show create table user \\G:
*************************** 1. row ***************************
       Table: user
Create Table: CREATE TABLE `user` (
  `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT \'\',
  `User` char(32) COLLATE utf8_bin NOT NULL DEFAULT \'\',
  `Select_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Insert_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Update_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Delete_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Create_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Drop_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Reload_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Shutdown_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Process_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `File_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Grant_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `References_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Index_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Alter_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Show_db_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Super_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Create_tmp_table_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Lock_tables_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Execute_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Repl_slave_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Repl_client_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Create_view_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Show_view_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Create_routine_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Alter_routine_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Create_user_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Event_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Trigger_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `Create_tablespace_priv` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `ssl_type` enum(\'\',\'ANY\',\'X509\',\'SPECIFIED\') CHARACTER SET utf8 NOT NULL DEFAULT \'\',
  `ssl_cipher` blob NOT NULL,
  `x509_issuer` blob NOT NULL,
  `x509_subject` blob NOT NULL,
  `max_questions` int(11) unsigned NOT NULL DEFAULT \'0\',
  `max_updates` int(11) unsigned NOT NULL DEFAULT \'0\',
  `max_connections` int(11) unsigned NOT NULL DEFAULT \'0\',
  `max_user_connections` int(11) unsigned NOT NULL DEFAULT \'0\',
  `plugin` char(64) COLLATE utf8_bin NOT NULL DEFAULT \'mysql_native_password\',
  `authentication_string` text COLLATE utf8_bin,
  `password_expired` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  `password_last_changed` timestamp NULL DEFAULT NULL,
  `password_lifetime` smallint(5) unsigned DEFAULT NULL,
  `account_locked` enum(\'N\',\'Y\') CHARACTER SET utf8 NOT NULL DEFAULT \'N\',
  PRIMARY KEY (`Host`,`User`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT=\'Users and global privileges\'
1 row in set (0.00 sec)

    -> exit;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \':
exit\' at line 1
mysql>
mysql>
mysql>
mysql> show create table ruifeng \\G:
ERROR 1146 (42S02): Table \'mysql.ruifeng\' doesn\'t exist
    -> mysql> desc proc;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \':
mysql> desc proc\' at line 1
mysql> +----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+---------------------+-----------------------------+
    -> | Field                | Type                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | Null | Key | Default             | Extra                       |
    -> +----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+---------------------+-----------------------------+
    -> | db                   | char(64)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | NO   | PRI |                     |                             |
    -> | name                 | char(64)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | NO   | PRI |                     |                             |
    -> | type                 | enum(\'FUNCTION\',\'PROCEDURE\')                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        | NO   | PRI | NULL                |                             |
    -> | specific_name        | char(64)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | NO   |     |                     |                             |
    -> | language             | enum(\'SQL\')                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         | NO   |     | SQL                 |                             |
    -> | sql_data_access      | enum(\'CONTAINS_SQL\',\'NO_SQL\',\'READS_SQL_DATA\',\'MODIFIES_SQL_DATA\')                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | NO   |     | CONTAINS_SQL        |                             |
    -> | is_deterministic     | enum(\'YES\',\'NO\')                                                                                                                                                                                                                                                                                

以上是关于python操作mysql数据库系列-安装MySql数据库的主要内容,如果未能解决你的问题,请参考以下文章

MySQL系列教程

mysq'l系列之10.mysql优化&权限控制

Mysql系列三:Centos6下安装Mysql和Mysql主从复制的搭建

python MySQLdb Windows下安装教程及问题解决方法

mysq双主模式

Python教程:操作数据库,MySql的安装详解