vim /etc/sysconfig/network-scripts/ifcfg-eth0 配置IP
hostnamectl set-hostname testdb.westos.com 修改主机名字
vim /etc/yum.repos.d/rhel_dvd.repo 配置yum源
若不知道mariadb的安装包:
[root@testdb ~]# yum search mariadb
安装数据库:
yum install mariadb-server.x86_64
[root@testdb ~]# systemctl start mariadb 开启数据库服务
[root@testdb ~]# systemctl enable mariadb 开机自启动
[root@testdb ~]# systemctl stop firewalld 关闭防火墙
[root@testdb ~]# systemctl disable firewalld 开机自动关闭
查看数据库接口mysql 3306接口打开不安全
[root@testdb ~]# netstat -antuple | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 27 68511 3272/mysqld
关闭数据库接口(修改配置文件/etc/my.cnf)
[root@testdb ~]# vim /etc/my.cnf 写入
skip-networking=1
重启后再次查看数据库接口
[root@testdb ~]# systemctl restart mariadb
[root@testdb ~]# netstat -antuple | grep mysql 已经关闭
安装数据库安全初始化脚本 mysql_secure_installation
[root@localhost ~]# mysql_secure_installation 安装数据库安全初始化脚本
Enter current password for root (enter for none): 输入当前root用户的密码(是否不输入)
Set root password? [Y/n] 询问是否设置密码
New password:
Re-enter new password:
Password updated successfully! 密码配置成功
Reloading privilege tables..
... Success!
Remove anonymous users? [Y/n] 询问是否不允许匿名用户登录
Disallow root login remotely? [Y/n] 询问是否不允许远程连接
Remove test database and access to it? [Y/n] 询问是否删除测试数据库并访问它
Reload privilege tables now? [Y/n] 询问是否重新加载权限表
登陆数据库 注意:密码不要写在-p后在企业中不安全(quit退出)
[root@testdb ~]# mysql -uroot -p
Enter password:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> use mysql; 切换到mysql数据库中
MariaDB [mysql]> show tables;
MariaDB [mysql]> select host,user,password from user;
MariaDB [mysql]> desc user;
MariaDB [(none)]> create database westos; 创建数据库
MariaDB [mysql]> show databases; 查看所有数据库
MariaDB [(none)]> use westos;
MariaDB [westos]> show tables;
MariaDB [westos]> create table linux (
-> username varchar(50) not null, 字符类型为char,不能为空
-> password varchar(50) not null 字符类型为char,不能为空
-> );
MariaDB [westos]> show tables;
MariaDB [westos]> insert into linux values('hello','123');
MariaDB [westos]> insert into linux values('hi','1234');
MariaDB [westos]> select * from linux; 查看linux表格中的所有信息
MariaDB [westos]> update linux set password='01230' where username='hi'; 加引号表示这是一个字符串
MariaDB [westos]> select * from linux;
MariaDB [westos]> alter table linux add age varchar(4) after username;
MariaDB [westos]> select * from linux;
MariaDB [westos]> alter table linux drop age;
MariaDB [westos]> select * from linux;
MariaDB [westos]> alter table linux rename mydata;
MariaDB [westos]> show tables;
MariaDB [westos]> delete from mydata where username='hi';
MariaDB [westos]> select * from mydata;
MariaDB [westos]> drop table mydata;
MariaDB [westos]> show tables;
MariaDB [westos]> drop database westos;
MariaDB [(none)]> show databases;
[root@testdb ~]# yum install httpd php php-mysql -y 安装需要的服务
[root@testdb ~]# systemctl start httpd 开启阿帕奇
[root@testdb ~]# systemctl stop firewalld 关闭防火墙
[root@testdb ~]# cd /var/www/html
[root@testdb html]# ls
phpMyAdmin-3.4.0-all-languages.tar.bz2 准备的安装包
[root@testdb html]# tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 解压bz2的文件
[root@testdb html]# ls
phpMyAdmin-3.4.0-all-languages phpMyAdmin-3.4.0-all-languages.tar.bz2
[root@testdb html]# rm -fr phpMyAdmin-3.4.0-all-languages.tar.bz2 删除安装包
[root@testdb html]# ls
phpMyAdmin-3.4.0-all-languages
[root@testdb html]# mv phpMyAdmin-3.4.0-all-languages/ mysqladmin移动到指定路径
[root@testdb html]# ls
mysqladmin
##在浏览器中输入172.25.254.121/mysqladmin/进入登录界面:##
可以设置语言,用设置的用户名密码就可以登陆:
进入登陆界面点击数据库可以看到所有的数据库
MariaDB [(none)]> create user hate@localhost identified by 'redhat';密码为redhat
Query OK, 0 rows affected (0.00 sec)
##hate@localhost 本地用户,仅仅在本地登录 ##hate@‘%’ 远程用户均可以登录
MariaDB [(none)]> select user from mysql.user; 查看
+------+
| user |
+------+
| root |
| root |
| hate |
| root |
+------+
4 rows in set (0.00 sec)
MariaDB [(none)]> grant INSERT,SELECT on westos.* to hate@localhost;
Query OK, 0 rows affected (0.01 sec)
查看用户权限
MariaDB [(none)]> show grants for hate@localhost;
+-------------------------------------------------------------------------------------------------------------+
| Grants for hate@localhost |
+-------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'hate'@'localhost' IDENTIFIED BY PASSWORD '*84BB5DF4823DA319BBF86C99624479A198E6EEE9' |
| GRANT SELECT, INSERT ON `westos`.* TO 'hate'@'localhost' |
+-------------------------------------------------------------------------------------------------------------+
具备select权限,可以查看westos数据库:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| westos |
+--------------------+
具备insert权限,可以在westos.linux表格中插入:
MariaDB [(none)]> insert into westos.linux values('hi','123');
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> select * from westos.linux;
+----------+----------+
| username | password |
+----------+----------+
| hello | 123 |
| hi | 123 |
+----------+----------+
不具备delete权限,不能删除:
MariaDB [(none)]> delete from westos.linux where username='hi';
ERROR 1142 (42000): DELETE command denied to user 'hate'@'localhost' for table 'linux'
MariaDB [(none)]> revoke INSERT on westos.* from hate@localhost;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show grants for hate@localhost;
+-------------------------------------------------------------------------------------------------------------+
| Grants for hate@localhost |
+-------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'hate'@'localhost' IDENTIFIED BY PASSWORD '*84BB5DF4823DA319BBF86C99624479A198E6EEE9' |
| GRANT SELECT ON `westos`.* TO 'hate'@'localhost'
+-------------------------------------------------------------------------------------------------------------+
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> drop user hate@localhost;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user from mysql.user;查看用户
四、用户密码更改
1.若用户密码忘记,关闭数据库服务,跳过认证列表
[root@testdb mnt]# systemctl stop mariadb 停止服务
[root@testdb mnt]# mysqld_safe --skip-grant-tables & 跳过认证
[root@testdb mnt]# mysql -u root 直接回车就可以登陆
2.进入数据库修改
MariaDB [(none)]> update mysql.user set Password='redhat' where User='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
查看密码 select * from mysql.user 以上修改密码为明文
MariaDB [(none)]> select * from mysql.user;查询密码为明文不安全
| localhost | root | redhat | Y
以加密方式修改密码:
MariaDB [(none)]> update mysql.user set Password=password('123') where User='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
MariaDB [(none)]> select * from mysql.user;再次查看密码
| localhost | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | Y 变成加密字符
3.查看mysql相关进程,全部关闭
[root@testdb mnt]# ps aux | grep mysql 查看进程
[root@testdb mnt]# killall -9 mysqld_safe 结束进程
[root@testdb mnt]# ps aux | grep mysql 再次查看进程
[root@testdb mnt]# kill -9 9582 杀死
[root@testdb mnt]# ps aux | grep mysql 已经全部关闭
关闭后重启mysql服务,新密码登陆即可
[root@testdb ~]# systemctl restart mariadb
[root@testdb ~]# mysql -uroot -p123 直接输入密码可以登陆
[root@testdb ~]# mysql -uroot -p123
MariaDB [(none)]> show databases;
MariaDB [(none)]> use westos;
MariaDB [westos]> show tables;
MariaDB [westos]> select * from linux;
mysqldump -uroot -p123 --all-database > /mnt/westos.all 所有数据库资料备份,此处为自己设置的密码
mysqldump -uroot -p123 --all-database --no-data > /mnt/westos.err 只备份数据结构,不备份数据
mysqldump -uroot -p123 westos > /mnt/westos.sql 指定westos数据库的资料备份
[root@testdb ~]# mysqldump -uroot -p123 --all-database > /mnt/westos.all 这里我做了两种备份方式
[root@testdb ~]# mysqldump -uroot -p123 westos > /mnt/westos.sql
[root@testdb ~]# ls /mnt/
westos.all westos.sql
[root@testdb ~]# mysql -uroot -p123 -e "drop database westos"
[root@testdb ~]# mysql -uroot -p123 -e "show databases"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
注意:当备份的数据库不存在时,会报错(例:删除westos数据库)
[root@testdb ~]# mysql -uroot -p123 < /mnt/westos.sql
ERROR 1046 (3D000) at line 22: No database selected
[root@testdb ~]# vim /mnt/westos.sql
CREATE DATABASE westos;
USE westos;
DROP TABLE IF EXISTS `linux`;
[root@testdb ~]# mysql -uroot -p123 < /mnt/westos.sql 备份恢复
恢复后,登陆数据库查看
[root@testdb ~]# mysql -uroot -p123
MariaDB [(none)]> show databases; 已经恢复与原来一样
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| westos |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> select * from westos.linux;
+----------+----------+
| username | password |
+----------+----------+
| hello | 123 |
| hi | 1234 |
+----------+----------+
[root@testdb ~]# mysql -uroot -p123 -e "show databases;"
[root@testdb ~]# mysql -uroot -p123 -e "drop database westos;"
[root@testdb ~]# mysql -uroot -p123 -e "create database westos;" 建立数据库
[root@testdb ~]# mysql -uroot -p123 westos < /mnt/westos.sql 备份恢复
登陆数据库查看
[root@testdb ~]# mysql -uroot -p123
MariaDB [(none)]> show databases; 内容与原来一样已经恢复
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| westos |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> select * from westos.linux;
+----------+----------+
| username | password |
+----------+----------+
| hello | 123 |
| hi | 1234 |
+----------+----------+
2 rows in set (0.01 sec)
MariaDB [(none)]> quit
Bye