加入收藏 | 设为首页 | 会员中心 | 我要投稿 驾考网 (https://www.jiakaowang.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

基础操作以及备份 MySQL数据库

发布时间:2023-05-31 10:59:34 所属栏目:MySql教程 来源:
导读:1.基本查看及登录:

mysql -uroot -p

show databases; ##查看数据库

use MysqL; ##进入数据库MysqL

show tables; ##查看表

desc user; ##查看表的结构,表头

2.表的操作:查,改,删,增
1.基本查看及登录:

mysql -uroot -p

show  databases;  ##查看数据库

use MysqL;   ##进入数据库MysqL

show tables;  ##查看表

desc user;  ##查看表的结构,表头

2.表的操作:查,改,删,增

select * from  user \G;  ##查询user表中的所有数据记录

select host,user,password from user;  ##指定user表的字段进行查询

update MysqL.user set password=password("123123") where user="root"; ##修改root密码

delete from MysqL.user where user="";   ##删除用户为空的数据记录

create database auth;  ##创建库auth

create table auth.users(user_name char(16) not null, user_passwd char(48) default '',primary key (user_name)); ##创建表auth.users

insert into auth.users values('hehe','pwd@123'); ##新增记录

drop table auth.users;  ##删除表users

drop database auth;     ##删除库auth
 
3.MysqL的权限管理

grant all on 库.* to 用户@客户机地址 identified by ‘密码’;

show grants for 用户@客户机地址;

revoke 权限列表 on  库.* from 用户@客户机地址;

grant select,delete on MysqL.user to 'useradm'@'192.168.100.100' identified by '123123';

show grants for 'useradm'@'192.168.100.100';

revoke select,delete on MysqL.user from 'useradm'@'192.168.100.100';

delete from MysqL.user where user='useradm';

flush privileges; 

4.备份与恢复MysqL

登录到MysqL

create  database auth;

Quit

/etc/init.d/MysqLd stop

cd /usr/local/MysqL/data

cp -rf MysqL/user.* auth/

chown MysqL:MysqL auth/ -R

chmod 755 auth

chmod 660 auth/*

/etc/init.d/MysqLd start

登录MysqL

use auth;

show tables;   ##能看到user表,desc能查看结构,select

MysqL的冷备份:

/etc/init.d/MysqLd stop

tar Jcf /opt/MysqL-bak-$(date +%F).tar.xz /usr/local/MysqL/data

模拟故障:

/etc/init.d/MysqLd start

MysqL登录

drop database auth;

quit;

/etc/init.d/MysqLd stop

MysqL恢复:

tar Jxf /opt/MysqL-bak-*.tar.xz -C /root

cd /root/usr/local/MysqL/data

cp -rf auth/  /usr/local/MysqL/data

chown MysqL:MysqL /usr/local/MysqL/data/auth  -R

cd /usr/local/MysqL/data

chmod 755 auth

chmod 660 auth/*

/etc/init.d/MysqLd  start

MysqL登录验证

show databases;  ##数据已经恢复

在线备份;MysqLdump

netstat -utpln |grep 3306 ##确保MysqL启动

MysqLdump -uroot -p123123 --all-databases >/opt/all.sql  #备份

MysqLdump -uroot -p123123 --all-databases --lock-talbes=0 >/opt/all.sql

MysqL -uroot -p123123 </opt/all.sql   ##恢复

在bash中操作MysqL:去交互

vi /root/test.sh

MysqL -uroot -p123123 <<END

create database hehe;

END

:wq

chmod +x /root/test.sh

/root/test.sh

5.MysqL忘记密码的解决方案:

vim /etc/my.cnf

[MysqLd]

skip-grant-tables  ##添加该行,跳过密码验证

:wq

/etc/init.d/MysqLd restart

MysqL  ##登录后操作

update MysqL.user set password=password("123123") where user="root"; ##修改root密码

Exit

vim /etc/my.cnf

[MysqLd]

#skip-grant-tables  ##注释该行

:wq

/etc/init.d/MysqLd restart

6.单独管理用户:

用户管理

MysqL>use MysqL;

MysqL> select host,user,password from user ;

MysqL>create user linuxfan identified by '123123';  ##identified by 会将纯文本密码加密作为/散列值存储

MysqL>rename   user  linuxfan to   fage;##MysqL 5之后可以使用,之前需要使用update 更新user表

MysqL> set password for fage=password('123'); ##:需在MysqL.user 表中使用

MysqL> update  MysqL.user  set  password=password('123')  where user='fage';##

指定表中数据的根位置,无需进入表。

MysqL> show grants for fage;查看用户权限

MysqL> grant select on MysqL.user to fage; ##赋予权限

MysqL> revoke select on MysqL.user from fage;  ##如果权限不存在会报错

MysqL>drop user fage;   ##MysqL5之前删除用户时必须先使用revoke 删除用户权限,然后删除用户,MysqL5之后drop 命令可以删除用户的同时删除用户的相关权限
 
7.设置MysqL5.5显示中文名:

vi /etc/my.cnf

[client]

default-character-set = utf8

[MysqLd]

character-set-server = utf8

init_connect='SET NAMES utf8'

:wq

/etc/init.d/MysqLd restart

(编辑:驾考网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章