本来要做的是用Python的pymysql库连接MySQL,结果安装就折腾了好久。
自己挖的坑,哭着也要往深里挖。
准备工作
1. 下载安装MySQL(使用版本5.7)
官网下载对应版本。解压到C:/Program Files/mysql。
添加环境变量:
C:/Program Files/mysql/bin/
。在【我的电脑】--【属性】--【高级系统设置】--【环境变量...】--【PATH】。-
修改mysql主目录下的my-default.ini文件。名字改为my.ini。简单配置:
[mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. basedir = C:\\Program Files\\mysql datadir = D:\\MySQLdata port = 3306 # server_id = .....
去掉注释,添加路径。
-
安装:使用管理员身份打开cmd窗口。找到
提示命令符
应用,右键以管理员身份运行
。切换到安装文件夹的bin目录下cd C:\Program Files\mysql\bin
,据说必须的,不然会出错。然后执行如下命令:mysqld install mysql --defaults-file="C:\Program Files\mysql\my.ini"
显示:
Service successfully installed.
说明安装成功。 初始化:
mysqld --initialize
。还有传各种参数的形式,等我回家再Mac系统再试试,这里先不折腾了。-
启动服务:
net start mysql mysql 服务正在启动 . mysql 服务已经启动成功。
停止服务是
net stop mysql
,不知道是不是因为在管理员权限下安装的缘故,我这里启动服务的命令必须在管理员权限下操作才行。(下次试一下普通权限安装)或者也可以在【服务】中启动和停止,【我的电脑】--【管理】--【服务和应用程序】--【服务】中找到mysql。
-
关于密码:
默认无密码:使用
mysql -u root
进入初次使用为root用户更改密码:使用
mysqladmin -u root password
用密码登陆:
mysql -u root -p
,它会让你Enter password:
,输入之后回车即可。-
【1045错误】密码出错的修改方法:(是啦,我特么一上来就不知道为什么说我密码不对。)
在my.ini文件中[mysqld]个条目下加入
skip-grant-tables
。然后重启mysql服务。使用
mysql -u root -p
进入,这次就不需要密码了。-
执行
use mysql;
。然后是修改密码的语句:update user set authentication_string=password("...") where user="root"
-
改完之后退出。把my.ini中的
skip-grant-tables
删掉。重启服务。在进入就OK了。整个过程如下:
C:\Users\cc>mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.17 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> use mysql; Database changed mysql> update user set authentication_string=password("你自己的密码") where user="root"; Query OK, 1 row affected, 1 warning (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye C:\Users\cc>mysql -u root -p Enter password: ******* Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.17 Copyright (c) 2000, 2016, 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>
-
在使用之前还要AlterUser,执行命令:
ALTER USER 'root'@'localhost' IDENTIFIED BY '你的密码'
-
至此,mysql可以用了。测试一下:
mysql> create database scraping; Query OK, 1 row affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | scraping | | sys | +--------------------+ 5 rows in set (0.00 sec)
2. 在mysql中创建存放爬虫数据的数据表
直接贴上操作的结果吧,不需要说明了:
mysql> use scraping
Database changed
mysql> create database scraping;
Query OK, 1 row affected (0.01 sec)
mysql> create table test(ID int PRIMARY KEY, Name varchar(20));
Query OK, 0 rows affected (0.30 sec)
mysql> create table pages(id bigint(7) NOT NULL AUTO_INCREMENT, tiltle VARCHAR(200), content TEXT(163830), created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,PRIMARY KEY(id));
Query OK, 0 rows affected (0.20 sec)
mysql> show tables;
+--------------------+
| Tables_in_scraping |
+--------------------+
| pages |
| test |
+--------------------+
2 rows in set (0.00 sec)
mysql> desc pages;
+---------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+-------------------+----------------+
| id | bigint(7) | NO | PRI | NULL | auto_increment |
| tiltle | varchar(200) | YES | | NULL | |
| content | mediumtext | YES | | NULL | |
| created | timestamp | NO | | CURRENT_TIMESTAMP | |
+---------+--------------+------+-----+-------------------+----------------+
4 rows in set (0.00 sec)
顺便,修改数据库的编码方式,以方便存储汉字。这次贴个图吧。
3. 安装PyMySQL库
这个就简单啦,pip install PyMySQL
。
哎哟,真费劲。就先这些吧,下一篇再写用PyMySQL访问MySQL数据库。