Linux系统中对MySQL数据库的管理

本文详细介绍MariaDB数据库的安装步骤、安全性配置、用户管理、数据库操作与备份恢复流程,以及如何通过phpMyAdmin实现图形化管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MySQL数据库的管理:(使用的软件:mariadb)

 

1.安装软件包
[root@shareserver westos]# yum install mariadb-server -y

 

2.开启mariadb
systemctl start mariadb

 

3.输入mysql_secure_installation

[root@shareserver westos]# mysql_secure_installation  
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
 
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
 
Enter current password for root (enter for none):  
OK, successfully used password, moving on...
 
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
 
Set root password? [Y/n]  
New password:                                   ##此时需要我们设置登陆密码  
Re-enter new password:                  ##再次确认密码
Password updated successfully!
Reloading privilege tables..
 ... Success!
 
 
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
 
Remove anonymous users? [Y/n]      #清除匿名用户
 ... Success!
 
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
 
Disallow root login remotely? [Y/n]    #是否允许远程root用户登陆
 ... Success!
 
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
 
Remove test database and access to it? [Y/n]   #是否允许进入
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
 
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
 
Reload privilege tables now? [Y/n]   
 ... Success!
 
Cleaning up...
 
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
 
Thanks for using MariaDB!

 

 

4.设置完毕开始登陆:

 

输入 netstat -antlupe |grep mysql 查看数据库在网络中的对外接口:

编辑/etc/my.cnf文件:

[root@shareserver westos]# vim /etc/my.cnf

在第十行写入下面内容:(关闭接口的作用:安全!)

 

[root@shareserver westos]# systemctl restart mariadb
[root@shareserver westos]# netstat -antlupe |grep mysql    

#会发现没有 tcp ..... 那些东西了!

再次登陆:

#########对数据库的管理###########

 

1.显示列出数据库
MariaDB [westos]> SHOW DATADASE; (标准为 大写英文字符, 以 ; 结尾表示输入完毕!)

可以看到我们有 information_schema  mysql  performance_schema 三个数据库可用:

2.进入库(以mysql为例):
MariaDB [mysql]> USE mysql;
Database changed

显示mysql数据库里的数据表格:

MariaDB [mysql]> SHOW TABLES;

 

@@更改数据库信息@@:

建立一个数据库:

进入westos库:

USE westos;

新建一个表:( 名称代号,多少字符,not null )

DESC linux: 查看linux表的结构

插入数据到linux表:(插入一个 cool,456)

查看linux表中的所有字段:(SELECT *FROM linux)

再插入一个(hello,456),选择linux表中的username字段:

修改表名:(改linux为haha):

@向表中添加信息:(增加一个age)

@删除表中的某个属性:(删除age):

@默认添加属性都是添加到最后,我们指定位置添加属性:(比如放在username后面)

@修改表中属性的属性值(age里的俩属性还没有(null)): (设置为33):

@上面修改全部修改了,单独修改一个:(将 hello那行的改为22):

@删除表中的某一行:(指定username=cool的那一行删除)

@删除表:

@删除库:(可以看到westos被删除掉了)

 

########管理数据库的用户#########

创建一个westos库用于测试:

@创建一个数据库用户asuka,登陆密码为asdfg,并查看数据库中的所有用户:

可以看到以有的用户:

创建asuka@localhost 用户:密码为asdfg

@用创建的数据库用户登录:(此时有没有什么库信息)

@显示用户对数据库所有的权限:

@给asuka授予权限:

GRANT SELECT,INSERT,DROP,DELETE,CREATE ON  westos.* TO asuka@localhost;

@删除用户:

 

######数据库的备份######

mysqldump -uroot -plee --all-databases > /mnt/alldata.sql ##备份全部数据到/mnt/allgata.sql文件
mysqldump -uroot -plee --all-databases  --no-data > /mnt/nodata.sql ##备份数据库结构
mysqldump -uroot -plee westos > /mnt/westos.sql   ##备份数据库westos的内容

 

@删除数据库
 mysql -uroot -plee -e "DROP DATABASE westos;"
 mysql -uroot -plee -e "SHOW DATABASES;"

@恢复数据库:
1. mysql -uroot -plee -e "CREATE DATABASE westos;"
    mysql -uroot -plee westos < /mnt/westsos.sql
 
2. vim /mnt/westos.sql
  26 CREATE DATABASE westos;
  27 USE westos;   
 
mysql -uroot -plee  < /mnt/westos.sql
mysql -uroot -plee -e "SHOW DATABASES;"
 

 

######使用phpMyAdmin来管理MySQL数据库#####

安装httpd服务:

启动httpd服务:(systemctl start httpd ):

 

测试:(在网页地址栏输入开启了httod服务的IP地址)

下载phpMyAdmin...-all-languages 软件:(下载到html目录下)

重命名php...为 mysqladmin:

在网页上查看:

 

 

 

这样很不安全,也不是我们想要的结果;

安装php软件:

 

再安装php-mysql:

重启httpd服务:

查看网页:

此时我们需要:

进入mysqladmin目录下,根据模板,创建config.inc.php

再 vim config.inc.php: 更改17行:

重启httpd和mariadb服务:

然后查看网页:

登录 root 及 密码后:(可以利用图形界面进行很多操作!)

 

 

OVER!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值