Liunx之redhat中mysql的管理

1.msqly是什么

MySQL是一个小型关系型数据库管理系统,被广泛地应用在Internet上的中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,

2.msyql环境的设定

进行软件的下载

[[email protected] ~]# yum install mariadb-server.x86_64  -y

Liunx之redhat中mysql的管理

进行服务的启动

[[email protected] ~]# systemctl start mariadb

查看服务是否启动

[[email protected] ~]# systemctl status mariadb

Liunx之redhat中mysql的管理

设置开机自启

[[email protected] ~]# systemctl enable mariadb

Liunx之redhat中mysql的管理

编辑配置文件

在文件中加入 skip-networking=1,

不要监听 TCP/IP 连接。所有与 mysqld 的交互必须使用命名管道或共享内存(在 Windows 上)或 Unix socket 文件(在 Unix 上)。强烈建议对只允许本地客户端的系统使用

[[email protected] ~]# vim /etc/my.cnf

Liunx之redhat中mysql的管理

修改完配置进行重启

[[email protected] ~]#  systemctl restart mariadb
测试:

不加skip-networking=1时输入[[email protected] ~]# netstat  -antlupe | grep mysql

Liunx之redhat中mysql的管理

修改文件[[email protected] ~]# vim /etc/my.cnf加上skip-networking=1并重启服务

此时输入[[email protected] ~]# netstat  -antlupe | grep mysql

查看msyql的网络端口发现已经没有显示了

设置登陆时的环境

[[email protected] ~]# mysql_secure_installation

 

初始化的mysql管理是没有密码 敲回车即可

Liunx之redhat中mysql的管理

设定root用户密码

必须设置否则无法使用,2次密码输入

第一次为密码输入,第二次为确认

Liunx之redhat中mysql的管理

是否删除匿名用户:确保数据库的安全,我选择删除

Liunx之redhat中mysql的管理

不允许远程root用户登路,我们设定的为本地数据库,所以选y

Liunx之redhat中mysql的管理

删除测试数据库并访问它 我选择是Y,编辑一个自己的数据库

Liunx之redhat中mysql的管理

现在重新加载权限表:选择y

Liunx之redhat中mysql的管理

登陆mysql数据库

现在在使用mysql发现不能登陆[[email protected] ~]# mysql

Liunx之redhat中mysql的管理

因为此时mysql已经禁止匿名用户登陆,只能使用root用户进行的登陆,

[[email protected] ~]# mysql -uroot -p进行登陆输入所设密码

Liunx之redhat中mysql的管理

mysql超级用户密码的管理

(1)用户知道密码

[[email protected] ~]# mysqladmin -uroot -pwyx password wyxl

                                                                     ^ (此为原密码)    ^ (修改为的密码)

此时输入原来的密码[[email protected] ~]# mysql -uroot -pwyx发现会产生报错密码错误
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[[email protected] ~]# mysql -uroot -pwyxl
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.44-MariaDB MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

输入修改后的密码可以登陆mysql :表示密码修改成功

(2)当超级用户密码忘记时

停止mariadb.service服务

[[email protected] ~]# systemctl stop mariadb.service

输入进行密码的重新设定

[[email protected] ~]# mysqld_safe --skip-grant-tables &

Liunx之redhat中mysql的管理

此时登陆mysql就不需要密码

Liunx之redhat中mysql的管理
 

输入mysql进入系统输入MariaDB [(none)]> update mysql.user set password='123'where user='root';更改密码

Liunx之redhat中mysql的管理

输入MariaDB [(none)]> select * from mysql.user;查看用户信息

发现更改密码成功但是此时密码是明文

Liunx之redhat中mysql的管理

改密码时要给密码加密应输入

MariaDB [(none)]> update mysql.user set password=password('123')where user='root';

然后输入MariaDB [(none)]> select * from mysql.user;进行查看此时密码以进行过加密

Liunx之redhat中mysql的管理

但是你会发现旧的密码也可以继续登陆

Liunx之redhat中mysql的管理

此时需要结束mysql进程在进行登陆

找到mysql进程结束到所有mysql进程清空系统对于mysql的缓存

[[email protected] ~]# ps aux |grep mysql

[[email protected] ~]# kill -9 4787
[[email protected] ~]# kill -9 4942

还有1个为mysql特殊块进程不需要结束

Liunx之redhat中mysql的管理

更改完毕打开mariadb服务

[[email protected] ~]# systemctl start mariadb.service

数据库的管理

查询:)

show  databases;             数据库查询

use     库名称       ;            

show    tables       ;           显示列表

desc     表民称     ;

selsct      *   form    表民称 :   查看表内所有信息;

select     字段1,字段2,.......form  表民称 where 字段=‘字段值’;

 

数据库的管理

数据库建立

MariaDB [mysql]> create databases wyx ;  建立一个 wyx的数据库

输入MariaDB [mysql]> show databases查看是否已经建立

Liunx之redhat中mysql的管理

建立)

use  wyx  进入wyx库中

输入

MariaDB [wyx]> create table linux(
    -> username varchar(40) not null,
    -> useryear varchar(40) not null
    -> );

在wyx库中建立一个名字为linux的表

show  tables ; 查看liunx表是否建立

Liunx之redhat中mysql的管理

     MariaDB [wyx]> desc linux;   查看linux表结构

Liunx之redhat中mysql的管理

MariaDB [wyx]> insert into linux values('lww','20' );  添加数据到linux表中

Liunx之redhat中mysql的管理

MariaDB [wyx]> select username  from linux; 查询指定字段

Liunx之redhat中mysql的管理

更改)

MariaDB [wyx]> ALTER TABLE linux add class varchar(40); 添加表内文件

Liunx之redhat中mysql的管理

MariaDB [wyx]> update linux SET useryear=('28') where username=('lww');更改表内信息

Liunx之redhat中mysql的管理

删除class文件MariaDB [wyx]> ALTER TABLE linux drop class;

Liunx之redhat中mysql的管理

MariaDB [wyx]> ALTER TABLE linux add age varchar(10)  after username;在username后添加一个

age

Liunx之redhat中mysql的管理

MariaDB [wyx]> ALTER TABLE liunx rename data ;更改表的名字为data ,

Liunx之redhat中mysql的管理

更改后表中数据没有发生变化

Liunx之redhat中mysql的管理

删除)

MariaDB [wyx]> delete from data where username='lw'; 删除表中username=lw的数据

Liunx之redhat中mysql的管理

删除数据表 MariaDB [wyx]> drop table data;

Liunx之redhat中mysql的管理

MariaDB [wyx]> drop database wyx;删除wyx库

Liunx之redhat中mysql的管理

用户授权)

MariaDB [(none)]> create user [email protected] identified by 'wyx'; 建立一个新用户

wyx并且设定他的登陆密码为wyx

Liunx之redhat中mysql的管理

MariaDB [(none)]> grant insert,select on wyx.* to [email protected];授予wyx用户添加和查看的权力

Liunx之redhat中mysql的管理

MariaDB [(none)]> show grants for [email protected];查看用户wyx具备的权限

Liunx之redhat中mysql的管理

MariaDB [(none)]>  FLUSH PRIVILEGES;使所作更改生效,quit退出

在用wyx用户进行登陆就行参看权限是否赋予

Liunx之redhat中mysql的管理

能登陆可见用户建立成功

如图所示,可见wyx用户具有insert和select权限但是用户不具备alter和drop权限

Liunx之redhat中mysql的管理

Liunx之redhat中mysql的管理

MariaDB [(none)]> revoke insert on wyx.* from [email protected];删除用户wyx的insert权限

Liunx之redhat中mysql的管理

继续进行上面的演示,再次对 lk 表进行插入,发现已经没有权限进行插入,而对于 select权限还有

Liunx之redhat中mysql的管理

进行对于用户的删除

MariaDB [wyx]> drop user [email protected];

在用wyx用户去进行登陆发现登录失败,表示用户已不存在

Liunx之redhat中mysql的管理

数据库的备份

[[email protected] mnt]# mysqldump -uroot -pwyxl wyx > /mnt/wyx.sql 先把文件备份到/mnt/wyx.sql中

 

测试:

[[email protected] mnt]# mysql -uroot -pwyxl  -e "drop database wyx;"删除数据库 wyx ;

[[email protected] mnt]# mysql -uroot -pwyxl  -e "show databases;;"显示数据库列表

Liunx之redhat中mysql的管理

[email protected] mnt]# mysql -uroot -pwyxl  < /mnt/wyx.sql把备份中的数据库中

ERROR 1046 (3D000) at line 22: No database selected

备份恢复(1)

出现这个问题需要把备份文件中22行添加

CREATE DATABASE wyx;
use wyx;

Liunx之redhat中mysql的管理

然后再进行[email protected] mnt]# mysql -uroot -pwyxl  < /mnt/wyx.sql 导入备份文件

wyx数据库又出现

Liunx之redhat中mysql的管理

备份恢复(2)

[[email protected] mnt]# mysql -uroot -pwyxl  -e "create database wyx;"先建立一个数据库名字为wyx;

在进行备份的导入

[[email protected] mnt]# mysql -uroot -pwyxl  < /mnt/wyx.sql

Liunx之redhat中mysql的管理

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

[