linux中与mysql兼容的数据库:mariadb
#####################################mysql数据库基本语句操作及用网页管理数据库
1.数据库的安装
linux中数据库名为mariadb,和mysql兼容
yum install mariadb-server -y ##安装mysql
systemctl start mariadb
vim /etc/my.cnf ##mysql数据库主配置文件
skip-networking=1 ##关闭通过网络访问数据库
systemctl restart mariadb
mysql ##直接进入mysql数据库系统
mysql_secure_installation ##安全初始化数据库
设置root密码
删除匿名用户登陆
限制root远程登陆
删除测试数据库
更新数据库
linux中数据库名为mariadb,和mysql兼容
yum install mariadb-server -y ##安装mysql
systemctl start mariadb
vim /etc/my.cnf ##mysql数据库主配置文件
skip-networking=1 ##关闭通过网络访问数据库
systemctl restart mariadb
mysql ##直接进入mysql数据库系统
mysql_secure_installation ##安全初始化数据库
设置root密码
删除匿名用户登陆
限制root远程登陆
删除测试数据库
更新数据库
2.登陆数据库
mysql -uroot -predhat ##-u 用户,-p 密码,但是为了安全,-p之后直接回车输入密码无回显
mysql -uroot -predhat ##-u 用户,-p 密码,但是为了安全,-p之后直接回车输入密码无回显
3.查询
show databases; ##显示数据库
use mysql; ##使用mysql数据库
show tables; ##查询数据库中的表
select * from user; ##查询user表的所有信息(*可以用表中的字段代替,多个字段中间用“,”隔开)
desc user; ##查询user表结构
show databases; ##显示数据库
use mysql; ##使用mysql数据库
show tables; ##查询数据库中的表
select * from user; ##查询user表的所有信息(*可以用表中的字段代替,多个字段中间用“,”隔开)
desc user; ##查询user表结构
4.数据库及表的建立
create database westos; ##新建westos数据库
create table linux( ##建立linux表
username varchar(15) not null, ##字段username,字符长度最多为15且不为空
passwd varchar(15) not null ##字段passwd,字符长度最多为15且不为空
);
create database westos; ##新建westos数据库
create table linux( ##建立linux表
username varchar(15) not null, ##字段username,字符长度最多为15且不为空
passwd varchar(15) not null ##字段passwd,字符长度最多为15且不为空
);
5.更新数据信息
insert into linux values ('user1','passwd1'); ##向linux表中插入数据
insert into linux values ('user2',password('123')); ##passwd加密
update linux set passwd=123 where username='user1'; ##修改user1的密码为123
update linux set passwd=456 where (username='user1' or username='user2'); ##修改user1,user2的密码为456
delete from linux where username='user1'; ##删除user1的所有信息
alter table linux add class varchar(4) not null; ##向linux表中添加class字段
alter table linux drop age; ##删除linux表中的age字段
alter table linux add date varchar(4) not null after passwd; ##在passwd之后添加date字段
insert into linux values ('user1','passwd1'); ##向linux表中插入数据
insert into linux values ('user2',password('123')); ##passwd加密
update linux set passwd=123 where username='user1'; ##修改user1的密码为123
update linux set passwd=456 where (username='user1' or username='user2'); ##修改user1,user2的密码为456
delete from linux where username='user1'; ##删除user1的所有信息
alter table linux add class varchar(4) not null; ##向linux表中添加class字段
alter table linux drop age; ##删除linux表中的age字段
alter table linux add date varchar(4) not null after passwd; ##在passwd之后添加date字段
6.删除数据库
drop table linux; ##删除linux表
drop database westos; ##删除westos数据库
drop table linux; ##删除linux表
drop database westos; ##删除westos数据库
7.数据库的备份
mysqldump -uroot -predhat --all-database ##备份所有表中的所有数据
mysqldump -uroot -predhat --all-database --no-data ##备份所有表,但不备份数据
mysqldump -uroot -predhat westos ##备份westos表
mysqldump -uroot -predhat westos > /mnt/westos.sql ##备份westos表并保存到/mnt/westos.sql中
mysqldump -uroot -predhat westos linux > /mnt/linux.sql ##备份westos数据库中的linux表到/mnt/linux.sql中
mysqldump -uroot -predhat --all-database ##备份所有表中的所有数据
mysqldump -uroot -predhat --all-database --no-data ##备份所有表,但不备份数据
mysqldump -uroot -predhat westos ##备份westos表
mysqldump -uroot -predhat westos > /mnt/westos.sql ##备份westos表并保存到/mnt/westos.sql中
mysqldump -uroot -predhat westos linux > /mnt/linux.sql ##备份westos数据库中的linux表到/mnt/linux.sql中
8.数据库恢复
mysql -uroot -predhat -e "create database westos;" ##恢复之前建立westos数据库
mysql -uroot -predhat westos < /mnt/westos.sql ##恢复westos数据库
mysql -uroot -predhat westos linux < /mnt/linux.sql ##恢复linxu表
mysql -uroot -predhat -e "create database westos;" ##恢复之前建立westos数据库
mysql -uroot -predhat westos < /mnt/westos.sql ##恢复westos数据库
mysql -uroot -predhat westos linux < /mnt/linux.sql ##恢复linxu表
9.用户授权
create user [email protected] identified by '123'; ##创建tom用户,密码为123,只能本机登陆
create user [email protected]'%' identified by '123'; ##创建tom用户,密码为123,可以通过网络访问
grant insert,update,delete,select on westos.linux to [email protected]; ##给[email protected]授权
grant select on westos.* to [email protected]'%';
show grants for [email protected]'%'; ##显示[email protected]'%'权限
show grants for [email protected];
revoke delete on westos.linux from [email protected]; ##去除[email protected]的delete权限
drop user [email protected]'%'; ##删除用户
create user [email protected] identified by '123'; ##创建tom用户,密码为123,只能本机登陆
create user [email protected]'%' identified by '123'; ##创建tom用户,密码为123,可以通过网络访问
grant insert,update,delete,select on westos.linux to [email protected]; ##给[email protected]授权
grant select on westos.* to [email protected]'%';
show grants for [email protected]'%'; ##显示[email protected]'%'权限
show grants for [email protected];
revoke delete on westos.linux from [email protected]; ##去除[email protected]的delete权限
drop user [email protected]'%'; ##删除用户
10.密码修改
已知密码修改密码
mysqladmin -uroot -predhat password 123 ##已知密码为redhat修改密码为123
忘记密码修改密码
systemctl stop mysql
mysqld_safe --skip-grant-tables & ##开启mysql登陆接口并忽略授权表
mysql ##mysql可以直接登陆
update mysql.user set Password=password('456') where User='root'; ##修改root密码
ps aux | grep mysql ##过滤mysql所有进程并结束
kill -9 mysqlpid
systemctl restart mariadb ##重启mysql
mysql -uroot -p456 ##使用新密码登陆测试



11.网页管理数据库
1)安装
yum install httpd php php-mysql -y ##安装有关服务
systemctl start httpd
systemctl stop firewalld
systemctl disable firewalld
systemctl enable httpd
2)下载
phpMyAdmin-xxx-all-languages ##数据库管理工具
tar jxf phpMyAdmin-xxx-all-languages -C /var/www/html ##解压工具包到/var/www/html
cd /var/www/html
mv phpMyAdmin-xxx-all-languages mysqladmin ##重命名
cd mysqladmin
cp -p config.sample.inc.php config.inc.php ##修改配置文件
vim config.inc.php
$cfg['blowfish_secret'] = 'mysql'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
systemctl restart httpd ##重启apache
3)测试
访问http://172.25.254.123/mysqladmin ##成功登陆
已知密码修改密码
mysqladmin -uroot -predhat password 123 ##已知密码为redhat修改密码为123
忘记密码修改密码
systemctl stop mysql
mysqld_safe --skip-grant-tables & ##开启mysql登陆接口并忽略授权表
mysql ##mysql可以直接登陆
update mysql.user set Password=password('456') where User='root'; ##修改root密码
ps aux | grep mysql ##过滤mysql所有进程并结束
kill -9 mysqlpid
systemctl restart mariadb ##重启mysql
mysql -uroot -p456 ##使用新密码登陆测试
11.网页管理数据库
1)安装
yum install httpd php php-mysql -y ##安装有关服务
systemctl start httpd
systemctl stop firewalld
systemctl disable firewalld
systemctl enable httpd
2)下载
phpMyAdmin-xxx-all-languages ##数据库管理工具
tar jxf phpMyAdmin-xxx-all-languages -C /var/www/html ##解压工具包到/var/www/html
cd /var/www/html
mv phpMyAdmin-xxx-all-languages mysqladmin ##重命名
cd mysqladmin
cp -p config.sample.inc.php config.inc.php ##修改配置文件
vim config.inc.php
$cfg['blowfish_secret'] = 'mysql'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
systemctl restart httpd ##重启apache
3)测试
访问http://172.25.254.123/mysqladmin ##成功登陆