拓扑图:

Mysql数据库系统部署用户授权远程访问Mysql_centos

推荐步骤:

  • 在Centos01上安装Mysql数据库服务,生成服务器其配置文件,添加系统服务优化命令初始化mysql,设置访问密码登录mysql数据库
  • 在Centos01的mysql服务器上创建数据库,数据库创建表修改表中数据
  • 授权用户访问Mysql数据库增强数据库安全和数据库远程管理

实验步骤:

一、部署Mysql数据库系统

1、配置yum仓库安装依赖软件

1) 挂载系统盘

Mysql数据库系统部署用户授权远程访问Mysql_mysql_02

2)挂载系统光盘到/mnt目录

[root@centos01 ~]# mount /dev/cdrom /mnt/
lmount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# ls /mnt/
CentOS_BuildTag 
EULA  images    LiveOS   
repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI             
GPL   isolinux  Packages 
RPM-GPG-KEY-CentOS-7  TRANS.TBL

3)删除系统自带yum仓库配置本地yum仓库

[root@centos01 ~]# rm -rf /etc/yum.repos.d/Centos-*
[root@centos01 ~]# ls /etc/yum.repos.d/
local.repo
[root@centos01 ~]# vim /etc/yum.repos.d/local.repo
[local]
name=centos7
baseurl=file:///mnt
enabled=1
gpgcheck=0

4)安装依赖程序

[root@centos01 ~]# yum -y install ncurses-devel

2、切换mysql程序光盘解压源代码程序

1)卸载系统光盘挂载点

[root@centos01 ~]# umount /mnt/
[root@centos01 ~]# ls /mnt/

2) 切换mysql程序光盘

Mysql数据库系统部署用户授权远程访问Mysql_数据库_03

3)挂载mysql程序光盘

[root@centos01 ~]# mount /dev/cdrom /mnt/
lmount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# ls -ld /mnt/mysql-5.5.22.tar.gz 
-r-xr-xr-x 1 root root 24475686 6月  26 2014
/mnt/mysql-5.5.22.tar.gz

4)解压源代码程序到/usr/src目录

[root@centos01 ~]# tar zxvf
/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/
cmake \                                        				    //使用cmake工具配置mysql
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql  \               //指定安装位置
-DSYSCONFDIR=/etc 	\                             		   //初始化参数
-DDEFAULT_CHARSET=utf8   \                               //默认字符编码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 7月  25 18:33 /usr/local/mysql/

3)创建管理mysql组和用户

[root@centos01 mysql-5.5.22]# groupadd mysql
[root@centos01 mysql-5.5.22]# useradd -M -s
/sbin/nologin mysql -g 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 7月  25 18:33 /usr/local/mysql/

5)生成mysql主配置文件覆盖原有文件

[root@centos01 mysql-5.5.22]# cp
support-files/my-medium.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"?

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]# vim /etc/profile
# /etc/profile
PATH=$PATH:/usr/local/mysql/bin/
[root@centos01 mysql-5.5.22]# source /etc/profile

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      11581/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 -ppwd@123

二、mysql数据库表和记录管理

1、数据库管理

1)登录mysql创建数据库名字HB3035

[root@centos01 ~]# mysql -uroot -ppwd@123
mysql> create database HB3035;
Query OK, 1 row affected (0.00 sec)

2)查看创建的数据库

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

3)切换到创建的HB3035数据库和mysql数据库

mysql> use HB3035;
Database changed
mysql> use mysql;
Database changed

4)删除HB3035数据库查看创建的数据库

mysql> drop database HB3035;
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)创建accp数据库,在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(身份证号码));

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         |
+-----------------+
2 rows in set (0.00 sec)

4)删除创建的student表

mysql> drop table student;
Query OK, 0 rows affected (0.01 sec)

3、表中记录管理

1)student表中插入连续列数据

mysql> insert into accp.student values ('高恒涛',18,'13161295986','111345679124689018');
Query OK, 1 row affected (0.01 sec)

2)student表中插入不连续列数据

mysql> insert into accp.student (姓名,身份证号码) values ('胡炎','110678654108765416');
Query OK, 1 row affected (0.01 sec)

3)查看student表中所有数据

mysql> select * from accp.student;
+-----------+--------+-------------+--------------------+
| 姓名      | 年龄   | 电话        | 身份证号码         |
+-----------+--------+-------------+--------------------+
| 胡炎      |  
NULL | NULL        |
110678654108765416 |
| 高恒涛    |    
18 | 13161295986 | 111345679124689018 |
+-----------+--------+-------------+--------------------+
2 rows in set (0.00 sec)

4)查看student表的姓名和身份号码列数据

mysql> select 姓名,身份证号码
+-----------+--------------------+
| 姓名      | 身份证号码         |
+-----------+--------------------+
| 胡炎      | 110678654108765416 |
| 高恒涛    | 111345679124689018 |
+-----------+--------------------+
2 rows in set (0.00 sec)

5)修改stdeunt表中数据给胡炎添加年龄和电话

mysql> update accp.student set 电话='15810350733',年龄=21 where 姓名='胡炎';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 
Changed: 1  Warnings: 0

6)查看修改的数据

mysql> select * from accp.student;
+-----------+--------+-------------+--------------------+
| 姓名      | 年龄   | 电话        | 身份证号码         |
+-----------+--------+-------------+--------------------+
| 胡炎      |    
21 | 15810350733 | 110678654108765416 |
| 高恒涛    |    
18 | 13161295986 | 111345679124689018 |
+-----------+--------+-------------+--------------------+
2 rows in set (0.00 sec)

7)查看姓名是胡炎的数据

mysql> select 姓名,身份证号码 from accp.student where 姓名='胡炎';
+--------+--------------------+
| 姓名   | 身份证号码         |
+--------+--------------------+
| 胡炎   | 110678654108765416 |
+--------+--------------------+
1 row in set (0.00 sec)

8)删除accp数据库的student中记录名字是胡炎的记录

mysql> select * from accp.student;
+-----------+--------+-------------+--------------------+
| 姓名      | 年龄   | 电话        | 身份证号码         |
+-----------+--------+-------------+--------------------+
| 胡炎      |    
21 | 15810350733 | 110678654108765416 |
| 高恒涛    |    
18 | 13161295986 | 111345679124689018 |
+-----------+--------+-------------+--------------------+
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 | 13161295986 | 111345679124689018 |
+-----------+--------+-------------+--------------------+
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 'pwd@123';
Query OK, 0 rows affected (0.00 sec)

2)查看授权的bob用户权限

mysql> show grants for bob@192.168.100.20;
+-----------------------------------------------------------------------------------------------------------------+
| Grants for bob@192.168.100.20                                                                                  
|
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'bob'@'192.168.100.20'
IDENTIFIED BY PASSWORD '*760F60073FD235571A5260444301DB22136ED604' |
| 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 bob@192.168.100.20;
+-----------------------------------------------------------------------------------------------------------------+
| Grants for bob@192.168.100.20                                                                               
|
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'bob'@'192.168.100.20'
IDENTIFIED BY PASSWORD '*760F60073FD235571A5260444301DB22136ED604' |
+-----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
 
mysql>

2、授权远程Linux的客户端访问mysql数据库

1)挂载Linux系统光盘

[root@centos02 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# ls /mnt/
CentOS_BuildTag 
EULA  images    LiveOS   
repodata             
RPM-GPG-KEY-CentOS-Testing-7
EFI             
GPL   isolinux  Packages 
RPM-GPG-KEY-CentOS-7  TRANS.TBL

2)配置yum仓库

[root@centos02 ~]# rm -rf /etc/yum.repos.d/CentOS-*
[root@centos02 ~]# ls /etc/yum.repos.d/
local.repo
[root@centos02 ~]# vim /etc/yum.repos.d/local.repo
[local]
name=centos7
baseurl=file:///mnt
enabled=1
gpgcheck=0

3) 安装mariadb客户

[root@centos02 ~]# yum -y install mariadb

4) 在mysql数据库授权tom用户完全控制权限通过主机192.168.100.20访问数据库服务器写入数据

mysql> grant all on accp.* to
'tom'@'192.168.100.20' identified by 'pwd@123';
Query OK, 0 rows affected (0.00 sec)
 
mysql> show grants for tom@192.168.100.20;
+-----------------------------------------------------------------------------------------------------------------+
| Grants for tom@192.168.100.20                                                                                  
|
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'192.168.100.20'
IDENTIFIED BY PASSWORD '*760F60073FD235571A5260444301DB22136ED604' |
| GRANT ALL PRIVILEGES ON `accp`.* TO
'tom'@'192.168.100.20'                                                     
|
+-----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
 
mysql>

5)Linux系统Mysql客户端远程访问mysql数据库

[root@centos02 ~]# mysql -h 192.168.100.10 -P 3306
-u tom -ppwd@123
MySQL [(none)]> insert into accp.student values
('李佳诚',20,'11012099911','111188889099997777');
Query OK, 1 row affected (0.00 sec)
 
MySQL [(none)]> select * from accp.student;
+-----------+--------+-------------+--------------------+
| 姓名      | 年龄   | 电话        | 身份证号码         |
+-----------+--------+-------------+--------------------+
| 李佳诚    |    
20 | 11012099911 | 111188889099997777 |
| 高恒涛    |    
18 | 13161295986 | 111345679124689018 |
+-----------+--------+-------------+--------------------+
2 rows in set (0.00 sec)
 
MySQL [(none)]>

3、授权远程Windows客户端访问Mysql数据库

1)在Windows10客户端安装Navicat

Mysql数据库系统部署用户授权远程访问Mysql_数据库_04

2)接收协议许可条款

Mysql数据库系统部署用户授权远程访问Mysql_数据库_05

3)指定一下安装位置

Mysql数据库系统部署用户授权远程访问Mysql_数据库_06

4)创建桌面图标安装客户端

Mysql数据库系统部署用户授权远程访问Mysql_数据库_07

5)安装完成客户端

Mysql数据库系统部署用户授权远程访问Mysql_centos_08

6)找到安装目录可执行文件激活客户端

Mysql数据库系统部署用户授权远程访问Mysql_mysql_09

7)mysql服务器授权192.168.100.30通过alice用户密码为pwd@1234访问任意数据库和表

mysql> grant all on *.* to
'alice'@'192.168.100.30' identified by 'pwd@1234';
Query OK, 0 rows affected (0.00 sec)
 
mysql> show grants for alice@192.168.100.30;
+----------------------------------------------------------------------------------------------------------------------------+
| Grants for alice@192.168.100.30                                                                                           
|
+----------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO
'alice'@'192.168.100.30' IDENTIFIED BY PASSWORD
'*1012A2D81646DAEC166C0ACA539426FEECA3F616' |
+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

8)客户端连接mysql

Mysql数据库系统部署用户授权远程访问Mysql_centos_10

9)测试连接

Mysql数据库系统部署用户授权远程访问Mysql_mysql_11

10)使用客户端查看数据

Mysql数据库系统部署用户授权远程访问Mysql_数据库_12

9)查看用户信息表

mysql> show processlist;

10)用户管理设置

mysql> select user,host from mysql.user;
mysql> alter user 'benet'@'localhost' identified
by 'pwd@123';
mysql> drop user 'benet'@'localhost';
[root@centos01 ~]# mysql -uroot -p -S /tmp/mysql.sock    									 //免交互式登录

3、修改密码

1)关闭验证和连接功能

[root@centos01 ~]# systemctl stop mysqld                                   //停止服务
[root@centos01 ~]# mysqld_safe --skip-grant-tables --skip-networking &     //进入安全模式
[root@centos01 ~]# mysql -uroot -p                                         //登录数据
mysql> flush privileges;                                                   //手工加载授权表
mysql> alter user root@'localhost' identified by
'pwd@1234';                //修改密码
[root@centos01 ~]# /etc/init.d/mysqld stop                                 //停止服务
[root@centos01 ~]# /etc/init.d/mysqld start                                //启动服务
[root@centos01 ~]# mysql -uroot -ppwd@1234                          	     //修改密码后登录