拓补图:
数据库的概念:
数据库就是一个运行在系统上的软件,用于存储用户个人信息、用户的游戏资料等
数据库是多个表的集合,是存储数据的仓库,以一定的组织方式存储的相互有关的数据
数据库由多个表组成,多个数据记录组成一张表,类似Excel
数据库结构:由多个库组成,一个库包含多个表,一个表包含多条数据记录
推荐步骤:
- 在centos01上安装Mysql数据库服务,生成服务器及配置文件,添加系统服务优化命令初始化Mysql
,设置访问密码登录Mysql数据库
- 在centos01的Mysql服务器上创建数据库,数据库创建表修改表中数据
- 授权用户访问Mysql数据库增强数据库安全和数据库远程管理
实验步骤:
一、部署mysql数据库系统
1、配置yum仓库安装依赖软件
1)挂载系统盘
2)挂载系统光盘到/mnt/目录
[root@centos01 ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# ls /mnt
CentOS_BuildTag GPL LiveOS RPM-GPG-KEY-CentOS-7
EFI images Packages RPM-GPG-KEY-CentOS-Testing-7
EULA isolinux repodata TRANS.TBL
3)删除系统自带yum仓库配置本地yum仓库
[root@centos01 ~]# rm -rf /etc/yum.repos.d/CentOS-*
[root@centos01 ~]# ls /etc/yum.repos.d/
[root@centos01 ~]# vim /etc/yum.repos.d/local.repo
[local]
name=centos
baseurl=file:///mnt
enabled=1
gpgcheck=0
4)安装依赖程序
[root@centos01 ~]# yum -y install ncurses-devel cmake
2、切换Mysql程序光盘解压源代码程序
1)卸载系统光盘挂载点
[root@centos01 ~]# umount /mnt/
[root@centos01 ~]# ls /mnt
2)切换mysql程序光盘
3)挂载mysql程序光盘
[root@centos01 ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# ls /mnt/mysql-5.5.22.tar.gz
/mnt/mysql-5.5.22.tar.gz
4)解压源代码程序到/usr/src目录
[root@centos01 ~]# tar zxf /mnt/mysql-5.5.22.tar.gz -C /usr/src/
[root@centos01 ~]# ls /usr/src/
debug kernels mysql-5.5.22
3、配置安装mysql
1)配置mysql
[root@centos01 ~]# cd /usr/src/mysql-5.5.22/
[root@centos01 mysql-5.5.22]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all
2)编译安装mysql
[root@centos01 mysql-5.5.22]# make && make install
[root@centos01 mysql-5.5.22]# ls -ld /usr//local/mysql/
drwxr-xr-x 13 root root 213 3月 19 22:53 /usr//local/mysql/
3)创建管理mysql用户组和用户
[root@centos01 ~]# groupadd mysql
[root@centos01 ~]# useradd -M -s /sbin/nologin -g mysql mysql
4)修改目录的所有者
[root@centos01 mysql-5.5.22]# chown -R mysql:mysql /usr/local/mysql/
[root@centos01 mysql-5.5.22]# ls -ld /usr/local/mysql/
drwxr-xr-x 13 mysql mysql 213 3月 19 22:53 /usr/local/mysql/
5)生成mysql主配置文件覆盖原有文件
[root@centos01 mysql-5.5.22]# cp support-files/my-medium.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
6)生成服务添加执行权限添加为系统服务设置开机自动启动
[root@centos01 mysql-5.5.22]# cp support-files/mysql.server /etc/init.d/mysqld
[root@centos01 mysql-5.5.22]# chmod +x /etc/init.d/mysqld
[root@centos01 mysql-5.5.22]# chkconfig --add mysqld
[root@centos01 mysql-5.5.22]# chkconfig --level 35 mysqld on
7)优化mysql执行
[root@centos01 mysql-5.5.22]# echo "PATH=$PATH:/usr/local/mysql/bin/" >> /etc/profile
[root@centos01 mysql-5.5.22]# source /etc/profile
[root@centos01 mysql-5.5.22]# cd
8)初始化mysql
[root@centos01 ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
4、控制MySQL服务MySQL数据库设置密码
1)启动mysql服务并监听服务
[root@centos01 ~]# systemctl start mysqld
[root@centos01 ~]# netstat -anptu | grep mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 12277/mysqld
2)空密码登录mysql数据库和退出
[root@centos01 ~]# mysql -uroot -p
Enter password:
mysql> exit
Bye
3)设置mysql密码使用账户密码管理登录
[root@centos01 ~]# mysqladmin -uroot password
New password:
Confirm new password:
[root@centos01 ~]# mysql -uroot -p123
mysql> exit
Bye
二、mysql数据库表和记录管理
1、数据库管理
1)登录mysql数据库创建数据库名字benet
[root@centos01 ~]# mysql -uroot -p123
mysql> create database benet;
Query OK, 1 row affected (0.00 sec)
2)查看创建的数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| benet |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
3)切换到创建的benet数据库和Mysql数据库
mysql> use benet;
Database changed
mysql> use mysql;
Database changed
4)删除benet数据库查看创建的数据库
mysql> drop database benet;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
2、数据库表的管理
1)创建accps数据库,在accp数据库创建student表,设置表格4列数据三类为字符串1列数据为整数类
mysql> create database accp;
Query OK, 1 row affected (0.00 sec)
mysql> create table accp.student (姓名 char(4),年龄 int,电话 char(11),身份证号码 char(18), primary key(身份证号码));
Query OK, 0 rows affected (0.00 sec)
2)查看创建的表结构
mysql> desc accp.student;
+-----------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+----------+------+-----+---------+-------+
| 姓名 | char(4) | YES | | NULL | |
| 年龄 | int(11) | YES | | NULL | |
| 电话 | char(11) | YES | | NULL | |
| 身份证号码 | char(18) | NO | PRI | | |
+-----------------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)
3)切换到accp数据库,查看创建的表
mysql> use accp;
Database changed
mysql> show tables;
+----------------+
| Tables_in_accp |
+----------------+
| student |
+----------------+
1 row in set (0.00 sec)
4)删除创建的student表
mysql> drop table student;
Query OK, 0 rows affected (0.00 sec)
3、表中记录管理
1)student表中插入连续列数据
mysql> create table accp.student (姓名 char(4),年龄 int,电话 char(11),身份证号码 char(18),primary key(身份证号码));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into accp.student values ('张三',18,'12123451234','111122223333444455');
Query OK, 1 row affected (0.00 sec)
2)student表中插入不连续列数据
mysql> insert into accp.student (姓名,身份证号码) values ('李四','11231233211234679');
Query OK, 1 row affected (0.00 sec)
3)查看student表中
mysql> select * from accp.student;
+--------+--------+-------------+--------------------+
| 姓名 | 年龄 | 电话 | 身份证号码 |
+--------+--------+-------------+--------------------+
| 张三 | 18 | 12123451234 | 111122223333444455 |
| 李四 | NULL | NULL | 11231233211234679 |
+--------+--------+-------------+--------------------+
2 rows in set (0.00 sec)
4)查看student表的姓名和身份证号码列数据
mysql> select 姓名,身份证号码 from accp.student;
+--------+--------------------+
| 姓名 | 身份证号码 |
+--------+--------------------+
| 张三 | 111122223333444455 |
| 李四 | 11231233211234679 |
+--------+--------------------+
2 rows in set (0.00 sec)
5)修改student表中数据给李四添加年龄和电话
mysql> update accp.student set 电话='14212344124',年龄=21 where 姓名='李四';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
6)查看修改的数据
mysql> select * from accp.student;
+--------+--------+-------------+--------------------+
| 姓名 | 年龄 | 电话 | 身份证号码 |
+--------+--------+-------------+--------------------+
| 张三 | 18 | 12123451234 | 111122223333444455 |
| 李四 | 21 | 14212344124 | 11231233211234679 |
+--------+--------+-------------+--------------------+
2 rows in set (0.00 sec)
7)查看姓名是张三的数据
mysql> select 姓名,身份证号码 from accp.student where 姓名='张三';
+--------+--------------------+
| 姓名 | 身份证号码 |
+--------+--------------------+
| 张三 | 111122223333444455 |
+--------+--------------------+
1 row in set (0.01 sec)
8)删除accp数据库的student表中记录名字是李四的记录
mysql> select * from accp.student;
+--------+--------+-------------+--------------------+
| 姓名 | 年龄 | 电话 | 身份证号码 |
+--------+--------+-------------+--------------------+
| 张三 | 18 | 12123451234 | 111122223333444455 |
| 李四 | 21 | 14212344124 | 11231233211234679 |
+--------+--------+-------------+--------------------+
2 rows in set (0.00 sec)
mysql> delete from accp.student where 姓名='李四';
Query OK, 1 row affected (0.01 sec)
mysql> select * from accp.student;
+--------+--------+-------------+--------------------+
| 姓名 | 年龄 | 电话 | 身份证号码 |
+--------+--------+-------------+--------------------+
| 张三 | 18 | 12123451234 | 111122223333444455 |
+--------+--------+-------------+--------------------+
1 row in set (0.00 sec)
三、授权用户管理数据和远程鼓管理数据库修改数据库密码
1、授权和撤销授权的配置
1、授权用户bob对accp数据库下的所有表拥有完全控制权限通过远程计算机192.168.100.20访问
mysql> grant all on accp.* to 'bob'@'192.168.100.20' identified by '123';
Query OK, 0 rows affected (0.00 sec)
2、查看授权的bob用户权限
mysql> show grants for [email protected];
+-----------------------------------------------------------------------------------------------------------------+
| Grants for [email protected] |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'bob'@'192.168.100.20' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
| GRANT ALL PRIVILEGES ON `accp`.* TO 'bob'@'192.168.100.20' |
+-----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
3、撤销授权
mysql> revoke all on accp.* from 'bob'@'192.168.100.20';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for [email protected];
+-----------------------------------------------------------------------------------------------------------------+
| Grants for [email protected] |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'bob'@'192.168.100.20' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
+-----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
2、授权远程Linux的客户端访问mysql数据库
1)挂载Linux系统光盘
[root@centos02 ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# ls /mnt
CentOS_BuildTag GPL LiveOS RPM-GPG-KEY-CentOS-7
EFI images Packages RPM-GPG-KEY-CentOS-Testing-7
EULA isolinux repodata TRANS.TBL
2)配置yum仓库
[root@centos02 ~]# vim /etc/yum.repos.d/local.repo
[local]
name=centos
baseurl=file:///mnt
enabled=1
gpgcheck=0
3)安装mariadb客户
[root@centos02 ~]# yum -y install mariadb
4)在mysql数据库授权tom用户完全控制权限通过主机192.168.100.20访问数据库服务器写入数据
grant all on accp.* to 'tom'@'192.168.100.20' identified by '123';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for [email protected];
+-----------------------------------------------------------------------------------------------------------------+
| Grants for [email protected] |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'192.168.100.20' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
| GRANT ALL PRIVILEGES ON `accp`.* TO 'tom'@'192.168.100.20' |
+-----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
5)Linux系统mysql客户端远程访问MySQL数据库
[root@centos02 ~]# mysql -utom -p123 -h 192.168.100.10 -P 3306
MySQL [(none)]> insert into accp.student values ('王五',20,'12010102344','112233445566777788');
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> select * from accp.student
+--------+--------+-------------+--------------------+
| 姓名 | 年龄 | 电话 | 身份证号码 |
+--------+--------+-------------+--------------------+
| 张三 | 18 | 12123451234 | 111122223333444455 |
| 王五 | 20 | 12010102344 | 112233445566777788 |
+--------+--------+-------------+--------------------+
2 rows in set (0.00 sec)
3、授权远程Windows客户端访问mysql数据库
1)在windows客户端安装Navicat
2)接受协议许可条款
3)指定一下安装位置
4)创建桌面图标安装客户端
5)安装完成客户端
6)找到安装目录可执行文件激活客户端
7)MySQL服务器授权192.168.100.30通过alice用户密码为:123访问任意数据库和表
mysql> grant all on *.* to 'alice'@'192.168.100.30' identified by '123';
Query OK, 0 rows affected (0.00 sec)
8)客户端连接mysql
9)使用客户端查看数据