一、数据库的安装及初始安全配置
[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.cnf
10 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 对于数据库的管理
[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 daemon
Name 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 -A
Database 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)]>




GRANT SELECT on westos.* to tutu@localhost;
##添加用户tutu对于数据库westos的查看权限





GRANT UPDATE on westos.* to tutu@localhost;
##添加用户tutu对于数据库westos的写权限(此处不进行效果演示)
##此权限是让用户tutu可以对数据库westos进行编辑


6、密码修改
mysqladmin -uroot -predhat password 'tutu' ##修改本地 mysql root 密码 redhat 为 westos
[root@localhost mysqladmin]# mysql -uroot -predhat password 'tutu'
[root@localhost mysqladmin]# mysql -uroot -predhat
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
mysqladmin -uroot -predhat -h ip password westos ##修改远程ip mysql服务器 root密码
###当超级用户密码忘记时###
[root@localhost mysqladmin]# systemctl stop mariadb
[root@localhost mysqladmin]# mysqld_safe --skip-grant-tables & ##跳过grant-tables授权表 不需要认证登录本地mysql数据库
[1] 6141
[root@localhost mysqladmin]# 180526 02:42:44 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
180526 02:42:44 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@localhost mysqladmin]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1
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)]> SELECT Host,User,Password FROM mysql.user;
+-----------+------+-------------------------------------------+
| Host | User | Password |
+-----------+------+-------------------------------------------+
| localhost | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| 127.0.0.1 | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| ::1 | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+-----------+------+-------------------------------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> update mysql.user set Password=password('123') where User='root';
##更新mysql.user 表中条件为root用户的密码为加密tutu
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
MariaDB [(none)]> SELECT Host,User,Password FROM mysql.user;
+-----------+------+-------------------------------------------+
| Host | User | Password |
+-----------+------+-------------------------------------------+
| localhost | root | *6D80C637B84CD462FED1542FD0425E3ABF536C90 |
| 127.0.0.1 | root | *6D80C637B84CD462FED1542FD0425E3ABF536C90 |
| ::1 | root | *6D80C637B84CD462FED1542FD0425E3ABF536C90 |
+-----------+------+-------------------------------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> quit
Bye
[root@localhost mysqladmin]# fg
mysqld_safe --skip-grant-tables
^Z
[1]+ Stopped mysqld_safe --skip-grant-tables
[root@localhost mysqladmin]# killall -9 mysqld_safe
[1]+ Killed mysqld_safe --skip-grant-tables
[root@localhost mysqladmin]# ps aux | grep mysql
mysql 6296 0.0 10.1 859068 98256 pts/1 Sl 02:42 0:00 /usr/libexec/mysqld···(比较长,删除了一些)
root 6371 0.0 0.0 112640 936 pts/1 R+ 02:48 0:00 grep --color=auto mysql
[root@localhost mysqladmin]# kill -9 6296
[root@localhost mysqladmin]# ps aux | grep mysql
root 6373 0.0 0.0 112640 940 pts/1 R+ 02:48 0:00 grep --color=auto mysql
[root@localhost mysqladmin]# systemctl start mariadb
[root@localhost mysqladmin]# mysql -uroot -ptutu
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 mysqladmin]#
7、数据库的备份
[root@localhost mysqladmin]# mysql -uroot -ptutu -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| westos |
+--------------------+
[root@localhost mysqladmin]# mysql -uroot -ptutu
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 107
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 mysqladmin]# mysqldump -uroot -ptutu --all-database
##内容略,备份框架以及内容
[root@localhost mysqladmin]# mysqldump -uroot -ptutu --all-database --no-data
##内容略,只备份框架
[root@localhost mysqladmin]# mysqldump -uroot -ptutu westos > /mnt/westos.sql ##备份数据库westos到/mnt/下,命名为westos.sql
[root@localhost mysqladmin]# mysql -uroot -ptutu -e "drop database westos;" ##删除数据库westos
[root@localhost mysqladmin]# mysql -uroot -ptutu -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
[root@localhost mysqladmin]# ls /mnt/
westos.sql
[root@localhost mysqladmin]# vim /mnt/westos.sql
21 CREATE DATABASE westos; ##添加第21、22行
22 USE westos;
[root@localhost mysqladmin]# mysql -uroot -ptutu < /mnt/westos.sql
[root@localhost mysqladmin]# mysql -uroot -ptutu -e "select * from westos.linux;"
+----------+-----------+-------+
| username | password | class |
+----------+-----------+-------+
| tutu | tutu456 | linux |
| root | redhat123 | linux |
+----------+-----------+-------+
[root@localhost mysqladmin]# mysql -uroot -ptutu -e "drop database westos;"
[root@localhost mysqladmin]# mysql -uroot -ptutu -e "select * from westos.linux;"
ERROR 1146 (42S02) at line 1: Table 'westos.linux' doesn't exist
[root@localhost mysqladmin]# vim /mnt/westos.sql
21 /*CREATE DATABASE westos;*/ ##注释掉第21、22行
22 /*USE westos;*/
[root@localhost mysqladmin]# mysql -uroot -ptutu -e "CREATE DATABASE westos;"
[root@localhost mysqladmin]# mysql -uroot -ptutu westos < /mnt/westos.sql
[root@localhost mysqladmin]# mysql -uroot -ptutu -e "select * from westos.linux;"
+----------+-----------+-------+
| username | password | class |
+----------+-----------+-------+
| tutu | tutu456 | linux |
| root | redhat123 | linux |
+----------+-----------+-------+
[root@localhost mysqladmin]#