一、安装MySQL数据库
1、在 Centos01 上安装 Mysql 数据库服务,生成服务器其配置文件,添加系统服务优化命令初始化 mysql,设置访问密码登录 mysql 数据库
挂载系统光盘
2.挂载系统光盘到/mnt目录
[root@centos01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# ls /mnt/
CentOS_BuildTag
images repodata
EFI
isolinux RPM-GPG-KEY-CentOS-7
EULA
LiveOS
RPM-GPG-KEY-CentOS-Testing-7
GPL
Packages 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 ~]# cat /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
已加载插件:fastestmirror, langpacks
local | 3.6 kB 00:00
(1/2): local/group_gz | 156 kB 00:00
(2/2): local/primary_db | 3.1 MB 00:00
Determining fastest mirrors
正在解决依赖关系
--> 正在检查事务
---> 软件包
cmake.x86_64.0.2.8.12.2-2.el7 将被 安装
---> 软件包
ncurses-devel.x86_64.0.5.9-13.20130511.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
=========================================================
Package 架构 版本 源 大小
=========================================================
正在安装:
cmake x86_64 2.8.12.2-2.el7 local 7.1 M
ncurses-devel x86_64 5.9-13.20130511.el7 local 713 k
事务概要
=========================================================
安装 2 软件包
总下载量:7.7 M
安装大小:29 M
Downloading packages:
---------------------------------------------------------
总计 31
MB/s | 7.7 MB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 :
cmake-2.8.12.2-2.el7.x86_64 1/2
正在安装 :
ncurses-devel-5.9-13.20130511.el7. 2/2
验证中 :
ncurses-devel-5.9-13.20130511.el7. 1/2
验证中 :
cmake-2.8.12.2-2.el7.x86_64 2/2
已安装:
cmake.x86_64 0:2.8.12.2-2.el7
ncurses-devel.x86_64 0:5.9-13.20130511.el7
完毕!
2.切换MySQL程序光盘解压
代码程序
1.卸载系统光盘
[root@centos01 ~]# umount /mnt/
[root@centos01 ~]# ls /mnt/
2.切换MySQL程序光盘
3.挂载MySQL系统光盘
[root@centos01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
4.解压源代码到/usr/src目录
[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
[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、配置安装
1)配置
[root@centos01 ~]# cd /usr/src/mysql-5.5.22/
[root@centos01 mysql-5.5.22]# cmake //使用 cmake 工具配置
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql //指定安装位置
-DSYSCONFDIR=/etc //初始化参数
-DDEFAULT_CHARSET=utf8 //默认字符编码
-DDEFAULT_COLLATION=utf8_general_ci //校验字符编码
-DWITH_EXTRA_CHARSETS=all //支持更多字符编码
2)编译安装
[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月 12 18:05 /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 3月 12 18:05 /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]# echo
"PATH=$PATH:/usr/local/mysql/bin/" >> /etc/profile
[root@centos01 mysql-5.5.22]# source /etc/profile
8)初始化
[root@centos01 mysql-5.5.22]#
/usr/local/mysql/scripts/mysql_install_db --user=mysql
--basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
Installing MySQL system tables...
OK
Filling help tables...
OK
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 12033/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> exit
Bye
二、mysql 数据库表和记录管理
1、数据库管理
1)登录 mysql 创建数据库名字xxx
mysql> create database xxx;
Query OK, 1 row affected (0.01 sec)
2)查看创建的数据库
mysql> show databases;
+--------------------+
| Database
|
+--------------------+
| information_schema |
| mysql
|
| performance_schema |
| test
|
| xxx
|
+--------------------+
5 rows in set (0.00 sec)
3)切换到创建的xxx数据库和 mysql 数据库
mysql> use xxx;
Database changed
mysql> use mysql
Database changed
4)删除xxx数据库查看创建的数据库
mysql> drop database xxx;
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)创建 xxx数据库,在 xxx 数据库创建 aaa 表,设置表格 4 列数据三类为字符串 1 列数据为整数类
mysql> create database xxx;
Query OK, 1 row affected (0.00 sec)
mysql> create table xxx.aaa (姓名 char(4),年龄 int,电话 char(11),身份证号码 char(18),primary key(身份证号码));
Query OK, 0 rows affected (0.00 sec)
2)查看创建的表结构
mysql> desc xxx.aaa;
+-----------------+----------+------+-----+---------+-------+
| 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)切换到 xxx 数据库,查看创建的表
mysql> use xxx;
Database changed
mysql> show tables;
+---------------+
| Tables_in_xxx |
+---------------+
| aaa
|
+---------------+
1 row in set (0.00 sec)
4)删除创建的 aaa 表
mysql> drop table aaa;
Query OK, 0 rows affected (0.00 sec)
3、表中记录管理
1)aaa表中插入连续列数据
mysql> insert into xxx.aaa values
('tom',18,'12345678901','123456789012345678');
2)aaa 表中插入不连续列数据
mysql> insert into xxx.aaa (姓名,身份证号码) values
('bob','111111111111111111');
3)查看 aaa 表中所有数据
mysql> select * from xxx.aaa;
4)查看 aaa 表的姓名和身份号码列数据
mysql> select 姓名,身份证号码
+--------+--------------------+
| 姓名
| 身份证号码 |
+--------+--------------------+
| bob |
111111111111111111 |
| tom |
123456789012345678 |
+--------+--------------------+
2 rows in set (0.00 sec)
5)修改 aaa 表中数据给alice添加年龄和电话
mysql> update xxx.aaa set 电话='12345678986',年龄=12 whare 姓名='bob';
6)查看修改的数据
mysql> select * from xxx.aaa;
+--------+--------+-------------+--------------------+
| 姓名
| 年龄
| 电话
| 身份证号码 |
+--------+--------+-------------+--------------------+
| bob
| 12 | 12345678986 |
111111111111111111 |
| tom
| 18 | 12345678901 |
123456789012345678 |
+--------+--------+-------------+--------------------+
2 rows in set (0.00 sec)
7)查看姓名是alice的数据
mysql> select 姓名,身份证号码 from xxx.aaa where 姓名='bob';
+--------+--------------------+
| 姓名
| 身份证号码 |
+--------+--------------------+
| bob |
111111111111111111 |
+--------+--------------------+
1 row in set (0.00 sec)
8)删除 xxx 数据库的 aaa 中记录名字是alice的记录
mysql> delete from xxx.aaa where 姓名='bob';
Query OK, 1 row affected (0.00 sec)
mysql> select * from xxx.aaa;
+--------+--------+-------------+--------------------+
| 姓名
| 年龄
| 电话
| 身份证号码 |
+--------+--------+-------------+--------------------+
| tom
| 18 | 12345678901 |
123456789012345678 |
+--------+--------+-------------+--------------------+
1 row in set (0.00 sec)
三、授权用户管理数据和远程管理数据库修改数据库密码
1、授权和撤销授权的配置
1)授权用户 bob 对 accp 数据库下的所有表拥有完全控制权限通过远程计算机 192.168.100.20 访问
grant all on xxx.* to 'bob'@'192.168.100.20'
identified by 'pwd@123';
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 `xxx`.* TO
'bob'@'192.168.100.20'
|
+-----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
2、授权远程 Linux 的客户端访问 mysql 数据库
1)挂载 Linux 系统光盘
[root@centos02 ~]# ls /mnt/
CentOS_BuildTag
images repodata
EFI
isolinux RPM-GPG-KEY-CentOS-7
EULA
LiveOS
RPM-GPG-KEY-CentOS-Testing-7
GPL
Packages TRANS.TBL
2)配置 yum 仓库
[root@centos02 ~]# rm -rf /etc/yum.repos.d/CentOS-*
[root@centos02 ~]# ls /etc/yum.repos.d/
local.repo
[root@centos02 ~]# cat /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 xxx.* to
'tom'@'192.168.100.20' identified by 'pwd@123';
5)Linux 系统 Mysql 客户端远程访问 mysql 数据库
[root@centos02 ~]# mysql -h 192.168.100.10 -P 3306
-u tom -ppwd@123
MySQL [(none)]> insert into xxx.aaa values
('nnn',20,'12345654321','123456789098765432');
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> select * from xxx.aaa;
+--------+--------+-------------+--------------------+
| 姓名
| 年龄
| 电话
| 身份证号码 |
+--------+--------+-------------+--------------------+
| tom
| 18 | 12345678901 |
123456789012345678 |
| nnn
| 20 | 12345654321 |
123456789098765432 |
+--------+--------+-------------+--------------------+
2 rows in set (0.00 sec)
MySQL [(none)]> Ctrl-C -- exit!
Aborted
3、授权远程 Windows 客户端访问 Mysql 数据库
1)在 Windows10 客户端安装
2)接收协议许可条款
3)指定一下安装位置
4)创建桌面图标安装客户端
5)安装完成客户端
6)mysql 服务器授权 192.168.100.30 通过 alice 用户密码为 pwd@1234 访问任意数据库和表
mysql> grant all on *.* to
'alice'@'192.168.100.40' identified by 'pwd@1234';
mysql> show grants for alice@192.168.100.40;
+----------------------------------------------------------------------------------------------------------------------------+
| Grants for alice@192.168.100.40
|
+----------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO
'alice'@'192.168.100.40' IDENTIFIED BY PASSWORD
'*1012A2D81646DAEC166C0ACA539426FEECA3F616' |
+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
7)客户端连接
8)测试连接
9)查看数据