实验:配置安装mysql数据库和数据库和表的管理】
一. 配置安装数据库
- 使用系统光盘安装依赖程序
准备
安装依赖程序
完成 - 创建管理mysql账户和组
[[email protected] ~]# groupadd mysql 创建mysql组
[[email protected] ~]# useradd -M -s /sbin/nologin -g mysql mysql 创建mysql账户-g加入mysql组-s /sbin/nologin (/bin/bash允许)不允许登录 -M不指定宿主目录 - 配置mysql数据库安装
切换成服务程序光盘解压mysql数据库的安装文件到/usr/src
[[email protected] ~]# tar zxvf /mnt/mysql-5.5.22.tar.gz -C /usr/src/
进入安装文件所在的位置
[[email protected] ~]# cd /usr/src/mysql-5.5.22/
编译mysql数据库
[[email protected] mysql-5.5.22]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci –DWITH_EXTRA_CHARSETS=all –DSYSCONFDIR=/etc
[[email protected] mysql-5.5.22]# cmake -dcmake_install_prefix=/usr/local/mysql –ddefault_charset=utf8 -ddefault_collation=utf8_general_ci –dwith_extra_charsets=all –dsysconfdir=/etc (小写方便记)
-DCMAKE_INSTALL_PREFIX=-dcmake_install_prefix : 指定数据库安装位置
-DDEFAULT_CHARSET=utf8 = –ddefault_charset=utf8 :指定默认字符集编码
-DDEFAULT_COLLATION=utf8_general_ci = -ddefault_collation=utf8_general_ci :指定默认使用字符集校对规则
–DSYSCONFDIR=/etc = –dsysconfdir=/etc:指定初始化参数文件目录
–DWITH_EXTRA_CHARSETS=all = –dwith_extra_charsets=all : 指定额外支持的其他字符集编码
安装mysql数据库
[[email protected] mysql-5.5.22]#make && make install
查看安装的位置是否有mysql文件
[[email protected] mysql-5.5.22]# ls -ld /usr/local/mysql/
生成mysql主配置文件
[[email protected] mysql-5.5.22]# cp support-files/my-medium.cnf /etc/my.cnf
生成mysql服务控制文件
[[email protected] mysql-5.5.22]# cp support-files/mysql.server /etc/init.d/mysqld
生成控制文件
[[email protected] mysql-5.5.22]# chmod +x /etc/init.d/mysqld
加权限 执行
添加服务开机自启
[[email protected] ~]# chkconfig --add mysqld
[[email protected] ~]# chkconfig --level 35 mysqld on
优化mysql命令
vim /etc/profile
#/etc/profile 下添加
PATH=$PATH:/usr/local/mysql/bin/ 优化mysql命令
[[email protected] ~]#source /etc/profile 立即执行
验证是否优化成功
Mysql加table验证是否能补齐
设置目录所有者
[[email protected] ~]# chown -R mysql:mysql /usr/local/mysql/
初始化mysql服务
[[email protected] ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
添加开机自启并监听端口
[[email protected] ~]# systemctl start mysqld
[[email protected] ~]# systemctl enable mysqld
配置初始化密码
[[email protected] ~]# mysqladmin -u root password -u指定账户
登录mysql服务器
[[email protected] ~]# mysql -u root [email protected]
二. 数据库和表的管理 - 登录mysql数据库中
- 数据库的基本管理
查看数据库
mysql>show databases;
切换登录数据库mysql中
use mysql;
创建数据库benet
create database benet;
查看创建的数据库
show databases;
删除创建的数据库accp
drop database accp; - 表的基本管理
创建表
mysql> create table benet.student (姓名 char(10),性别 char(3),成绩 int,身份证号码 char(16),primary key (身份证号码)); 创建表
查看数据库中的表
use benet; 切换数据库
show tables; 查看创建的表
查看student表的结构
desc benet.student;
删除表
drop table benet.student; - 表记录管理
mysql> insert into benet.student (姓名,成绩,身份证号码)values (bob,100,111111111111111); 表中插入不连续数据
mysql> insert into benet.student values(‘bob’,‘男’,‘100’,‘1234567891234567’); 插入数据
查看表中内容
mysql>select * from benet.student;
查看姓名和成绩列
mysql> select 姓名,性别 from benet.student;
修改指定身份证号码的性别为女
mysql> update benet.student set 性别=‘女’ where 身份证号码=‘1234567891234567’;
mysql> select * from benet.student; 查看表中内容
删除指定表记录和所有表记录
先创建三个记录
删除指定的bob记录
mysql> delete from benet.student where 姓名=‘bob’;
验证
一次删除所有记录
mysql> delete from benet.student;
验证
三. 数据库的权限管理
all 完全控制权限 select 允许查询 insert 允许插入
update修改 delete删除
授权bob对benet数据库所有表具有完全控制权限
mysql> grant all on benet.* to ‘bob’@‘localhost’ identified by ‘[email protected]’;
查看授权权限
mysql>show grants for ‘bob’@’localhost’;
使用bob登录数据库验证权限
在bob上查看benet.student表中内容
撤销权限
mysql> revoke all on benet.* from ‘bob’@‘localhost’; - 配置远程主机登录mysql服务器
系统盘安装mysql客户端
yum -y install mysql
使用管理员授权登录权限
mysql> grant select on . to ‘root’@‘192.168.100.20’ identified by ‘[email protected]’;
客户端登录验证
[[email protected] ~]# mysql -h 192.168.100.10 -u root [email protected] -P 3306