[root@localhost ~]# yum install mariadb-server -y
##安裝數據庫服務軟件##安裝過程略
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# netstat -antlpe | grep mysql ? ?##列出數據庫的工作端口
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 27 50330 2327/mysqld
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# vim /etc/my.cnf10 skip-networking=1 ? ? ? ? ? ? ? ? ? ? ##添加第10行,作用直接跳過端口設置,不生成端口
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# netstat -antlpe | grep mysql
[root@localhost ~]# mysql ? ? ? ? ? ? ? ? ? ? ? ? ? ##不需密碼可以直接進入數據庫,不安全
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit
Bye
[root@localhost ~]# mysql_secure_installation ? ?? ##第一次安裝mysql,通過這條命令對mysql進行設置
[root@localhost ~]# mysql -uroot -predhat ? ? ? ? ?
##從本機登錄mysql數據庫(不建議這種方式,密碼可見,不安全)
##進入數據庫,-u是登陸用戶,-p是用戶密碼
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit
Bye
[root@localhost ~]# mysql -uroot -p ##從本機登錄mysql數據庫,此種方式較為安全
Enter password: ? ? ? ? ? ? ? ? ?##輸入密碼
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit
Bye
二、數據庫的基本SQL語句
以單行或多行書寫,以分號結尾
可使用空格和縮進來增強語句的可讀性
書寫不區分大小寫,建議使用大寫,如:SELECT User,Host,Password FROM user;
同樣可以使用/* */的方式完成注釋
1、查詢語句
查詢語句介紹
SHOW DATABASES;
##查看數據庫MySQL服務器中所有的數據庫
USE mysql;
##切換數據庫:USE 數據庫名
SHOW TABLES;
##查看數據庫中的所有表
SELECT * FROM user;
##查詢user表中的所有字段
SELECT User,Host,Password FROM user;
##查詢user表中的host,user,password字段
SELECT User,Host,Password,Select_priv FROM user;
##查詢user表中的host,user,password,Select_priv字段
DESC user;
##查看user表的數據結構
SELECT User,Host,Password,Select_priv FROM user Where User='root';
##查詢user表中的User為root的host,user,password,Select_priv字段
##Where [條件]
SELECT User,Host,Password,Select_priv FROM user Where User='root' AND Host='localhost';
##查詢user表中的User為root、Host為localhost的host,user,password,Select_priv字段
查詢語句效果顯示
SHOW DATABASES; ##查看數據庫MySQL服務器中所有的數據庫
USE mysql; ?? ##切換數據庫:USE 數據庫名
SHOW TABLES; ##查看數據庫中的所有表
SELECT * FROM user; ? ? ? ? ? ? ? ? ? ? ? ?? ? ?##查詢user表中的所有字段(命令執行后內容較多,省略不顯示)
SELECT User,Host,Password FROM user; ? ? ? ? ?? ? ##查詢user表中的host,user,password字段
SELECT User,Host,Password,Select_priv FROM user; ##查詢user表中的host,user,password,Select_priv字段
DESC user; ? ?##查看user表的數據結構
SELECT User,Host,Password,Select_priv FROM user Where User='root';
##查詢user表中的User為root的host,user,password,Select_priv字段
##Where [條件]
SELECT User,Host,Password,Select_priv FROM user Where User='root' AND Host='localhost';
##查詢user表中的User為root、Host為localhost的host,user,password,Select_priv字段
2、數據庫以及表的創建
CREATE DATABASE westos; ?? ##創建數據庫westos
CREATE TABLE linux(-> username varchar(6) not null,-> password varchar(50) not null); ##創建表,username,password字段
CREATE TABLE linux(username varchar(6) not null,password varchar(50) not null);??? ##創建表,username,password字段
##上面兩條命令一樣,第一條換行,第二條沒有換行
INSERT INTO linux value ('tutu','123'); ##在linux表中插入值為username = tutu,password = 123
INSERT INTO linux value ('butter','234'); ##在linux表中插入值為username = butter,password = 234
3、數據庫名的更改
一般不做更改,更改后,可能會影響數據庫的使用
[root@localhost ~]# cd /var/lib/mysql/
[root@localhost mysql]# ls
aria_log.00000001 ibdata1 ib_logfile1 mysql.sock westos
aria_log_control ib_logfile0 mysql performance_schema
[root@localhost mysql]# cd westos/
[root@localhost westos]# ls
db.opt linux.frm
[root@localhost westos]# file linux.frm
linux.frm: MySQL table definition file Version 10
[root@localhost westos]# file db.opt
db.opt: ASCII text
[root@localhost westos]# cd ..
[root@localhost mysql]# mv westos/ hello ? ? ?##將數據庫名稱westos更改為hello
[root@localhost mysql]# ls
aria_log.00000001 hello ib_logfile0 mysql performance_schema
aria_log_control ibdata1 ib_logfile1 mysql.sock
[root@localhost mysql]# systemctl restart mariadb
結果驗證
刷新數據庫信息——FLUSH PRIVILEGES;
[root@localhost mysql]# ls
aria_log.00000001 hello ib_logfile0 mysql performance_schema
aria_log_control ibdata1 ib_logfile1 mysql.sock
[root@localhost mysql]# mv hello/ westos
4、表的修改、添加、刪除及數據庫的刪除
表名稱的修改(一般不做修改)
ALTER TABLE linux RENAME messages; ##將表的名稱linux改為messages
ALTER TABLE messages RENAME linux; ##將表的名稱messages改回為linux
ALTER TABLE linux ADD age varchar(5); ##添加age字段到linux表中(默認加到最后一列)ALTER TABLE linux DROP age; ? ? ? ? ? ##刪除字段age
ALTER TABLE linux ADD age varchar(5) AFTER username; ##在username字段后添加字段age
ALTER TABLE linux DROP age; ? ? ? ? ? ? ? ? ? ? ? ? ? ?##刪除字段age
UPDATE linux SET username='hello' WHERE password='123'; ? ? ##更新linux表中password是123的username信息為hello
DELETE FROM linux WHERE username='hello'; ? ? ? ? ? ? ? ? ? ##將linux表中hello的所有內容
DROP TABLE linux; ##將linux表刪除
DROP DATABASE westos; ##將數據庫westos刪除
4、HTTP 和 PHP 對于數據庫的管理
HTTP 和 PHP 的前期準備
[root@localhost ~]# yum install httpd php -y
##安裝httpd和php,安裝過程略
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
phpMyAdmin-3.4.0-all-languages.tar.bz2 ? ##從網上可以下載該壓縮包
[root@localhost html]# tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2
[root@localhost html]# ls
phpMyAdmin-3.4.0-all-languages phpMyAdmin-3.4.0-all-languages.tar.bz2
[root@localhost html]# rm -fr phpMyAdmin-3.4.0-all-languages.tar.bz2
[root@localhost html]# mv phpMyAdmin-3.4.0-all-languages mysqladmin ? ##更改目錄名稱
[root@localhost html]# ls
mysqladmin
[root@localhost html]# cd mysqladmin/
[root@localhost mysqladmin]# ls | grep config.sample.
config.sample.inc.php
[root@localhost mysqladmin]# cp config.sample.inc.php config.inc.php
[root@localhost mysqladmin]# ls | grep config
config.inc.php
config.sample.inc.php
show_config_errors.php
此時用瀏覽器訪問http://172.25.254.127/mysqladmin?????
##訪問失敗,因為 php 中不存在 mysql
[root@localhost mysqladmin]# php -m | grep mysql
[root@localhost mysqladmin]# yum search php
Loaded plugins: langpacks
=========================================== N/S matched: php ===========================================
php.x86_64 : PHP scripting language for creating dynamic web sites
php-cli.x86_64 : Command-line interface for PHP
php-common.x86_64 : Common files for PHP
php-gd.x86_64 : A module for PHP applications for using the gd graphics library
php-ldap.x86_64 : A module for PHP applications that use LDAP
php-mysql.x86_64 : A module for PHP applications that use MySQL databases
php-odbc.x86_64 : A module for PHP applications that use ODBC databases
php-pdo.x86_64 : A database access abstraction module for PHP applications
php-pear.noarch : PHP Extension and Application Repository framework
php-pgsql.x86_64 : A PostgreSQL database module for PHP
php-process.x86_64 : Modules for PHP script using system process interfaces
php-recode.x86_64 : A module for PHP applications for using the recode library
php-soap.x86_64 : A module for PHP applications that use the SOAP protocol
php-xml.x86_64 : A module for PHP applications which use XML
php-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol
php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemonName and summary matches only, use "search all" for everything.
[root@localhost mysqladmin]# yum install php-mysql.x86_64 -y
##安裝過程略
[root@localhost mysqladmin]# php -m | grep mysql
mysql
mysqli
pdo_mysql
[root@localhost mysqladmin]# systemctl restart httpd
此時用瀏覽器訪問http://172.25.254.127/mysqladmin
MariaDB [(none)]> SHOW DATABASES; ##此時還沒有數據庫westos
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> SHOW DATABASES; ##在瀏覽器中創建數據庫westoos成功后
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| westos |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> USE westos;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
MariaDB [westos]> SHOW TABLES; ##此時還沒有新建數據表
Empty set (0.00 sec)
MariaDB [westos]> SHOW TABLES; ##數據表創建成功后
+------------------+
| Tables_in_westos |
+------------------+
| linux |
+------------------+
1 row in set (0.00 sec)
MariaDB [westos]> SELECT * FROM linux; ##創建字段前
Empty set (0.00 sec)
MariaDB [westos]> SELECT * FROM linux; ##創建字段后
+----------+-----------+-------+
| username | password | class |
+----------+-----------+-------+
| tutu | tutu123 | linux |
| root | redhat123 | linux |
+----------+-----------+-------+
2 rows in set (0.00 sec)
MariaDB [westos]> SELECT * FROM linux; ##修改字段后
+----------+-----------+-------+
| username | password | class |
+----------+-----------+-------+
| tutu | tutu456 | linux |
| root | redhat123 | linux |
+----------+-----------+-------+
2 rows in set (0.00 sec)
5、用戶的創建、授權、刪除等
CREATE USER tutu@'localhost' identified by 'tutu';
##創建用戶tutu,'localhost'代表用戶tutu只能在本地登陸,'%'代表用戶tutu能遠程登陸,identified by 'tutu'設置登陸密碼為tutu
[root@localhost mysqladmin]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 72
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>