本來(lái)要做的是用Python的pymysql庫(kù)連接MySQL,結(jié)果安裝就折騰了好久。
自己挖的坑,哭著也要往深里挖。
準(zhǔn)備工作
1. 下載安裝MySQL(使用版本5.7)
官網(wǎng)下載對(duì)應(yīng)版本。解壓到C:/Program Files/mysql。
添加環(huán)境變量:
C:/Program Files/mysql/bin/。在【我的電腦】--【屬性】--【高級(jí)系統(tǒng)設(shè)置】--【環(huán)境變量...】--【PATH】。-
修改mysql主目錄下的my-default.ini文件。名字改為my.ini。簡(jiǎn)單配置:
[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 = .....去掉注釋,添加路徑。
-
安裝:使用管理員身份打開(kāi)cmd窗口。找到
提示命令符應(yīng)用,右鍵以管理員身份運(yùn)行。切換到安裝文件夾的bin目錄下cd C:\Program Files\mysql\bin,據(jù)說(shuō)必須的,不然會(huì)出錯(cuò)。然后執(zhí)行如下命令:mysqld install mysql --defaults-file="C:\Program Files\mysql\my.ini"顯示:
Service successfully installed.說(shuō)明安裝成功。 初始化:
mysqld --initialize。還有傳各種參數(shù)的形式,等我回家再M(fèi)ac系統(tǒng)再試試,這里先不折騰了。-
啟動(dòng)服務(wù):
net start mysql mysql 服務(wù)正在啟動(dòng) . mysql 服務(wù)已經(jīng)啟動(dòng)成功。停止服務(wù)是
net stop mysql,不知道是不是因?yàn)樵诠芾韱T權(quán)限下安裝的緣故,我這里啟動(dòng)服務(wù)的命令必須在管理員權(quán)限下操作才行。(下次試一下普通權(quán)限安裝)或者也可以在【服務(wù)】中啟動(dòng)和停止,【我的電腦】--【管理】--【服務(wù)和應(yīng)用程序】--【服務(wù)】中找到mysql。
-
關(guān)于密碼:
默認(rèn)無(wú)密碼:使用
mysql -u root進(jìn)入初次使用為root用戶更改密碼:使用
mysqladmin -u root password用密碼登陸:
mysql -u root -p,它會(huì)讓你Enter password:,輸入之后回車即可。-
【1045錯(cuò)誤】密碼出錯(cuò)的修改方法:(是啦,我特么一上來(lái)就不知道為什么說(shuō)我密碼不對(duì)。)
在my.ini文件中[mysqld]個(gè)條目下加入
skip-grant-tables。然后重啟mysql服務(wù)。使用
mysql -u root -p進(jìn)入,這次就不需要密碼了。-
執(zhí)行
use mysql;。然后是修改密碼的語(yǔ)句:update user set authentication_string=password("...") where user="root" -
改完之后退出。把my.ini中的
skip-grant-tables刪掉。重啟服務(wù)。在進(jìn)入就OK了。整個(gè)過(guò)程如下:
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,執(zhí)行命令:
ALTER USER 'root'@'localhost' IDENTIFIED BY '你的密碼'
-
至此,mysql可以用了。測(cè)試一下:
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中創(chuàng)建存放爬蟲(chóng)數(shù)據(jù)的數(shù)據(jù)表
直接貼上操作的結(jié)果吧,不需要說(shuō)明了:
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)
順便,修改數(shù)據(jù)庫(kù)的編碼方式,以方便存儲(chǔ)漢字。這次貼個(gè)圖吧。

3. 安裝PyMySQL庫(kù)
這個(gè)就簡(jiǎn)單啦,pip install PyMySQL。
哎喲,真費(fèi)勁。就先這些吧,下一篇再寫(xiě)用PyMySQL訪問(wèn)MySQL數(shù)據(jù)庫(kù)。