[CentOS Python系列] 三.阿里雲MySQL資料庫開啟配置及SQL語句基礎知識

Eastmount發表於2018-02-18

從2014年開始,作者主要寫了三個Python系列文章,分別是基礎知識、網路爬蟲和資料分析。

這裡寫圖片描述 這裡寫圖片描述 這裡寫圖片描述

隨著人工智慧和深度學習的風暴來臨,Python變得越來越火熱,作者也準備從零學習這些知識,寫相關文章。本篇文章講解阿里雲伺服器CentOS系統下的MySQL資料庫開啟及配置過程,同時教大家如何編寫Python操作MySQL資料庫的基礎程式碼,為後面的網路爬蟲並儲存至伺服器打下基礎。

文章非常基礎,希望這系列文章對您有所幫助,如果有錯誤或不足之處,還請海涵~

系列文章:
[CentOS Python系列] 一.阿里雲伺服器安裝部署及第一個Python爬蟲程式碼實現
[CentOS Python系列] 二.pscp上傳下載伺服器檔案及phantomjs安裝詳解

參考文獻:
基於CentOS的Mysql的使用說明 - chisj專欄



一. MySQL資料庫開啟

1.檢查資料庫是否安裝

命令:rpm -qa | grep mysql



2.檢查MySQL服務是否開啟

命令:service mysqld status



3.開啟MySQL服務

命令:service mysqld start


可以看到 /usr/bin 目錄下存在mysqladmin命令。





4.使用root使用者登入mysql資料

命令:mysqladmin -u root -p password 123456


但是報如下錯,這是連線MySQL資料庫最常見的一個錯誤,怎麼解決呢?
mysqladmin: connect to server at 'localhost' failed
e
rror: 'Access denied for user 'root'@'localhost' (using password: NO)'


5.更新root密碼登入

命令如下:

--關閉服務
service mysqld stop

--安裝賦許可權
mysqld_safe --skip-grant-tables &

--root使用者登入
mysql -u root -p 

--輸入密碼
123456

--使用資料庫
use mysql;

--更新密碼
update user set password=PASSWORD("123456") where user="root";

--更新許可權
flush privileges; 

--退出
quit

--伺服器重啟
service mysqld restart

--root使用者登入
mysql -u root -p 
新密碼進入
如下圖所示:


然後輸入“use mysql;”使用資料庫,嘗試“show databases;”顯示所有資料庫。



接下來就是更新root使用者的密碼:


6.重啟服務本地連線mysql資料庫

命令:service mysqld restart
          mysql -u root -p






二. MySQL資料庫增加新使用者

1.使用mysql資料庫

命令:use mysql;



2.顯示所有表

命令:show tables;

這裡我們使用user表,定義mysql資料庫的使用者。



3.檢視錶結構

命令:describe user;

| Host                  | char(60)                          | NO   | PRI |                            |       |
| User                  | char(16)                          | NO   | PRI |                            |       |
| Password              | char(41)                          | NO   |     |                            |       |

4.新增一個新使用者yxz,密碼為123456

命令如下:

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> insert into mysql.user(Host,User,Password) value ("localhost","yxz",password("123456"));
Query OK, 1 row affected, 3 warnings (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> create database Eastmount;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| Eastmount          |
| junyun             |
| mysql              |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> grant all privileges on Eastmount.* to yxz@localhost identified by "yxz";
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>quit;
如下圖所示:

但是同樣報錯:error: 'Access denied for user 'root'@'localhost' (using password: NO)',需要像前面一樣修訂密碼,程式碼如下:

[root@iZ2ze9134z8zlqupc9t6mzZ ~]# service mysqld stop
Stopping mysqld:                                           [  OK  ]
[root@iZ2ze9134z8zlqupc9t6mzZ ~]# mysqld_safe --skip-grant-tables &
[1] 24403
[root@iZ2ze9134z8zlqupc9t6mzZ ~]# 180217 13:50:37 mysqld_safe Logging to '/var/log/mysqld.log'.
180217 13:50:37 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
mysql -u yxz -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| Eastmount          |
| junyun             |
| mysql              |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=PASSWORD("123456") where user="yxz";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;



三. SQL語句

1.root登入並進入Eastmount資料庫

命令如下:

[root@iZ2ze9134z8zlqupc9t6mzZ ~]# mysql -u yxz -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| Eastmount          |
| test               |
+--------------------+
3 rows in set (0.00 sec)

mysql> use Eastmount;
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql>


2.建立表
命令如下:
create table student(
    id int not null primary key,
    name varchar(16) not null,
    pwd varchar(20) not null
);


3.顯示錶結構

命令:describe student;



4.插入資料

命令:insert into student(id,name,pwd) values(1,'yxz','111111');


5.查詢資料

命令:select * from student;



6.更新資料

命令:update student set pwd='123456' where name='yxz';



7.刪除資料

命令:delete from student where id='1';



8.刪除表

命令:drop table studentl;




總之,希望這篇基礎文章對您有所幫助,尤其是剛接觸雲伺服器的新手,如果您是高手,還請多提意見,共同提高。祝大家新年快樂,又一年過去了,娜我們來年一起進步加油。 
( By:Eastmount CSDN 2018-02-13 中午12點 http://blog.csdn.net/Eastmount )



相關文章