Linux_数据库的基本操作

一.数据库安装

1.[root@localhost ~]# yum install mariadb-server -y ##安装数据库服务命令

2.[root@localhost ~]# systemctl start mariadb ##开启数据库服务

二.数据库的初始化

1.关闭接口

[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# vim /etc/my.cnf

##skip-networking=1 ###跳过数据库开启网络接

[root@localhost ~]# systemctl restart mariadb

[root@localhost ~]# 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] ##是否禁止超级用户通过远程登陆
 ... 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] ##是否删除test数据库
 - 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!

[root@localhost ~]# mysql -uroot -p ##-u表示指定登陆用户,-p表示指定此用户密码

三.数据库的基本命令

1.MariaDB [(none)]> SHOW DATABASES;  ##显示数据库


2.MariaDB [(none)]> USE mysql; ##进入mysql库

3.MariaDB [mysql]> SHOW TABLES; ##显示当前库中表的名称


4.MariaDB [mysql]> SELECT User,Host,Password,Select_priv FROM user;##查询user表中带有User,Host,Password,Select_priv的内容


5.MariaDB [mysql]> DESC user; ##查询user表的结构


6.MariaDB [mysql]> SELECT User,Host,Password,Select_priv FROM user Where User='root';##加上了查询条件,查询user表中User,Host,Password,Select_priv带有root的内容

四.数据库管理

MariaDB [mysql]> CREATE DATABASE westos ;##建立westos库

MariaDB [mysql]> USE westos;##进入westos库

MariaDB [westos]> CREATE TABLE linux ;# 建立linux表

MariaDB [westos]> DESC linux;##查询linux表的结构

MariaDB [westos]> INSERT INTO linux values ('123','456');##添加表中信息

MariaDB [westos]> INSERT INTO linux values ('tom','567');

MariaDB [westos]> SELECT * FROM linux;

MariaDB [westos]> ALTER TABLE linux RENAME messages; ##修改数据库表名

MariaDB [westos]> ALTER TABLE linux ADD AGE varchar(4);##在表中添加ACE列

MariaDB [westos]> ALTER TABLE linux DROP AGE;删除linux表中的AGE


MariaDB [westos]> ALTER TABLE linux ADD AGE varchar(4) after username; ##在username后添加ACG列

MariaDB [westos]> UPDATE linux SET password='123' WHERE username='123'; ##将密码是123的那一行的username改为123


MariaDB [westos]> DELETE FROM linux WHERE username='123';##删除表中123的数据


MariaDB [westos]> DROP TABLE linux;##删除linux表

MariaDB [westos]> DROP DATABASE westos;##删除westos库

五.数据库用户管理和授权

MariaDB [(none)]> CREATE USER gf@'localhost' identified by 'westos';##建立gf用户,密码westos,此用户只能通过本机登陆


MariaDB [(none)]> GRANT SELECT on westos.* to gf@localhost;##用户授权


MariaDB [(none)]> SHOW GRANTS FOR gf@localhost;##查看用户授权


MariaDB [(none)]> REVOKE SELECT on westos.* from gf@localhost;##撤销用户权限

MariaDB [(none)]> drop USER gf@localhost;##删除用户



1.创建用户
#CREATE USER whx@localhost identified by 'westos';
#CREATE USER lee@'%' identified by 'redhat';
2.用户授权
#GRANT INSERT,UPDATE,DELETE,SELECT on mariadb.* wxh@localhost;
#GRANT SELECT on mariadb.* lee@'%';
3.重载授权表
#FLUSH PRIVILEGES;
4.查看用户授权
#SHOW GRANTS FOR wxh@localhost;
5.撤销用户权限
#REVOKE DELETE,UPDATE,INSERT on mariadb.*from wxh@localhost;
6.删除用户
#DROP USER wxh@localhost;

六.数据库密码

[root@localhost mysqladmin]# systemctl stop mariadb ##关闭数据库服务

[root@localhost mysqladmin]# mysqld_safe --skip-grant-tables & ##开启mysql登陆接口并忽略授权表


[root@localhost ~]# mysql##直接登陆不需要密码


MariaDB [(none)]> select * from mysql.user;##查看user表所有信息


MariaDB [(none)]> update mysql.user set Password=password('123') where User='root';##更新超级用户密码信息(加密)

[root@localhost ~]# killall -9 mysqld_safe

[root@localhost ~]# ps aux | grep mysql##过滤mysql的所有过程

[root@localhost ~]# kill -9 2769##结束这些进程

[root@localhost ~]# ps aux | grep mysql


[root@localhost ~]# systemctl start mariadb##重新开启

[root@localhost ~]# mysql -uroot -p123##登陆测试

七.数据库备份

[root@localhost ~]# mysqldump -uroot -pwestos --all-database##备份所有表中的所有数据

[root@localhost ~]# mysqldump -uroot -pwestos --all-database --no-data##备份所有骨架,但不备份数据

[root@localhost ~]# mysqldump -uroot -pwestos  westos > /mnt/westos.sql##备份westos库并把数据保存到westos.sql

[root@localhost ~]# mysql -uroot -pwestos -e "drop database westos;"##删除westos库

恢复westos库

方法一:

[root@localhost ~]# ls /mnt/

[root@localhost ~]# vim /mnt/westos.sql

mysql -uroot -pwestos < /mnt/westos.sql ##将数据导入

方法二:

[root@localhost mysqladmin]# mysql -uroot -pwestos -e "CREATE DATABASE westos;"##建立westos库

[root@localhost mysqladmin]# mysql -uroot -pwestos westos  < /mnt/westos.sql##把数据导入westos库

八.数据库的图形管理

[root@localhost ~]# yum install httpd php -y

[root@localhost ~]# systemctl start httpd

[root@localhost ~]# systemctl stop firewalld

下载安装包phpMyAsmin-3.4.0压缩文件

解压重命名为mysqladmin

[root@localhost html]# tar jxf mysqladmin.tar.bz2

[root@localhost html]# mv phpMyAdmin-3.4.0-all-languages/ mysqladmin

[root@localhost html]# cd mysqladmin/

[root@localhost mysqladmin]# cp config.sample.inc.php config.inc.php

[root@localhost mysqladmin]# yum install php-mysql.x86_64 -y

[root@localhost mysqladmin]# systemctl restart httpd

在firefox输入ip/mysqladmin测试

登陆即可进行数据库的图形管理


九.论坛的搭建

1.下载安装包,并解压

unzip Discuz.zip

2.cp -r upload/ /var/www/html

3.chmod 777 /var/www/html/*n -R

4. systemctl start mariadb

5.输入ip/upload进入安装论坛界面


猜你喜欢

转载自blog.csdn.net/g_541243027/article/details/80522730