赞
踩
实验环境:yum仓库搭建好
mariadb 是当前流行的Mysql数据库的分支
Mysql原先SUN公司(java)被Orical收购了
Mysql开源免费,所以企业当中的核心数据库是Orical,其余是Mysql
mariadb是Mysql数据库的一个分支
1.数据库的安装
[root@server15 ~]# yum serach mariadb
[root@server15 yum.repos.d]# yum install mariadb-server.x86_64 -y
[root@server15 yum.repos.d]# rpm -ql mariadb-server
查看它在系统里面安装的东西
/var/lib/mysql 其中这个为它的数据目录
/etc/my.cnf.d/server.cnf 主配置文件
/usr/lib/systemd/system/mariadb.service 服务的启动脚本
[root@server15 yum.repos.d]# systemctl enable --now mariadb.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
启动服务
[root@server15 yum.repos.d]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.60-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) MariaDB [(none)]> quit Bye
由于数据库的登陆不需要密码所以不安全,我们要安全初始化。
[root@server15 yum.repos.d]# mysql_secure_installation 初始化脚本
Enter current password for root (enter for none): 当前数据库管理员的密码,无密码直接回车
Set root password? [Y/n] Y 设定数据库管理员密码
New password:
Re-enter new password:
Password updated successfully!
... Success!
Remove anonymous users? [Y/n] y
... Success! 移除匿名用户登陆
Disallow root login remotely? [Y/n] y
... Success! 移除管理员远程登陆
Remove test database and access to it? [Y/n] Y
... Success! 移除测试库
Reload privilege tables now? [Y/n] y
... Success! 刷新数据库
密码登陆
[root@server15 yum.repos.d]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit
Bye
默认情况下数据库对外开放端口,开放了,容易被攻击。一般在企业中要关闭
[root@server15 yum.repos.d]# netstat -antlupe | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 27 31978 3768/mysqld
[root@server15 yum.repos.d]# vim /etc/my.cnf.d/server.cnf
编辑主配置文件
关闭数据库网络端口
当设定完成后端口关闭
[root@server15 yum.repos.d]# systemctl restart mariadb.service
[root@server15 yum.repos.d]# netstat -antlupe | grep mysql
END
[root@server15 mnt]# mysql -uroot -p
MariaDB [(none)]> SHOW DATABASES; 查看有什么库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> USE mysql; 进入这个库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysql]> SHOW TABLES; 查看表 +---------------------------+ | Tables_in_mysql | +---------------------------+ | columns_priv | | db | | event | | func | | general_log | | help_category | | help_keyword | | help_relation | | help_topic | | host | | ndb_binlog_index | | plugin | | proc | | procs_priv | | proxies_priv | | servers | | slow_log | | tables_priv | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | +---------------------------+ 24 rows in set (0.00 sec)
MariaDB [mysql]> SHOW TABLES FROM mysql;
查看mysql库里面所有表的名字
MariaDB [mysql]> SELECT * FROM user;
查看user表里面的所有内容
表里面的字段
MariaDB [mysql]> SELECT Host,User,Password FROM user;
+-----------+------+-------------------------------------------+
| Host | User | Password |
+-----------+------+-------------------------------------------+
| localhost | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
| 127.0.0.1 | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
| ::1 | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
+-----------+------+-------------------------------------------+
3 rows in set (0.00 sec)
查看表里面的字段可以接条件
MariaDB [mysql]> SELECT Host,User,Password FROM user WHERE User='root' and Host='localhost';
+-----------+------+-------------------------------------------+
| Host | User | Password |
+-----------+------+-------------------------------------------+
| localhost | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
+-----------+------+-------------------------------------------+
1 row in set (0.00 sec)
.表示分级
MariaDB [(none)]> SELECT Host,User,Password FROM mysql.user WHERE User='root' and Host='localhost';
+-----------+------+-------------------------------------------+
| Host | User | Password |
+-----------+------+-------------------------------------------+
| localhost | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
+-----------+------+-------------------------------------------+
1 row in set (0.00 sec)
END
[root@server15 mnt]# mysql -uroot -p MariaDB [(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec) MariaDB [(none)]> CREATE DATABASE westos; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | westos | +--------------------+ 4 rows in set (0.00 sec)
MariaDB [(none)]> USE westos; Database changed MariaDB [westos]> SHOW TABLES; Empty set (0.00 sec) MariaDB [westos]> CREATE TABLE westos.linux ( -> username varchar(6) not null, -> password varchar(30) not null -> ); Query OK, 0 rows affected (0.44 sec) MariaDB [westos]> SHOW TABLES; +------------------+ | Tables_in_westos | +------------------+ | linux | +------------------+ 1 row in set (0.00 sec)
MariaDB [westos]> DESC westos.linux;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(6) | NO | | NULL | |
| password | varchar(30) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
MariaDB [westos]> INSERT INTO westos.linux VALUES ('lee',123) -> ; Query OK, 1 row affected (0.04 sec) MariaDB [westos]> DESC westos.linux; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | username | varchar(6) | NO | | NULL | | | password | varchar(30) | NO | | NULL | | +----------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) MariaDB [westos]> SELECT * FROM westos.linux -> ; +----------+----------+ | username | password | +----------+----------+ | lee | 123 | +----------+----------+ 1 row in set (0.00 sec)
MariaDB [westos]> INSERT INTO westos.linux VALUES ('lee1','123'),('lee2','123');
Query OK, 2 rows affected (0.40 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [westos]> SELECT * FROM westos.linux
-> ;
+----------+----------+
| username | password |
+----------+----------+
| lee | 123 |
| lee1 | 123 |
| lee2 | 123 |
+----------+----------+
3 rows in set (0.00 sec)
END
[root@server15 mysql]# mysqladmin -uroot -p password
Enter password:
New password:
Confirm new password:
1.跳过授权表进入数据库
[root@server15 mysql]# mysqld_safe --skip-grant-tables &
跳过授权表打开数据库 &后台运行
[2] 5479
[root@server15 mysql]# 210726 18:12:13 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
210726 18:12:13 mysqld_safe A mysqld process already exists
[root@server15 mysql]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
2.修改密码
3.更改里面的这个字段
MariaDB [(none)]> UPDATE mysql.user SET authentication_string=password('123') WHERE User='root';rhel8
MariaDB [(none)]> UPDATE mysql.user SET Password=password('123') WHERE User='root'; rhel7
4.查看mysql的所有进程并且关闭掉
[root@server15 mysql]# ps aux | grep mysql
root 5297 0.0 0.1 113312 1632 pts/0 S 18:10 0:00 /bin/sh /usr/bin/mysqld_safe --skip-grant-tables
mysql 5455 0.0 8.6 970848 88272 pts/0 Sl 18:10 0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --skip-grant-tables --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 5620 0.0 0.0 112708 972 pts/0 R+ 18:20 0:00 grep --color=auto mysql
[root@server15 mysql]# kill -9 5297
[root@server15 mysql]# kill -9 5455
ps aux | grep mysql
root 5624 0.0 0.0 112708 976 pts/0 R+ 18:22 0:00 grep --color=auto mysql 这个是grep的进程
5.更改完成
[root@server15 mysql]# systemctl start mariadb.service
[root@server15 mysql]# mysql -uroot -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
END
[root@server15 mysql]# mysqladmin -uroot -p password
Enter password:
New password:
Confirm new password:
1.跳过授权表进入数据库
[root@server15 mysql]# mysqld_safe --skip-grant-tables &
跳过授权表打开数据库 &后台运行
[2] 5479
[root@server15 mysql]# 210726 18:12:13 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
210726 18:12:13 mysqld_safe A mysqld process already exists
[root@server15 mysql]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
2.修改密码
3.更改里面的这个字段
MariaDB [(none)]> UPDATE mysql.user SET authentication_string=password('123') WHERE User='root';rhel8
MariaDB [(none)]> UPDATE mysql.user SET Password=password('123') WHERE User='root'; rhel7
4.查看mysql的所有进程并且关闭掉
[root@server15 mysql]# ps aux | grep mysql
root 5297 0.0 0.1 113312 1632 pts/0 S 18:10 0:00 /bin/sh /usr/bin/mysqld_safe --skip-grant-tables
mysql 5455 0.0 8.6 970848 88272 pts/0 Sl 18:10 0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --skip-grant-tables --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 5620 0.0 0.0 112708 972 pts/0 R+ 18:20 0:00 grep --color=auto mysql
[root@server15 mysql]# kill -9 5297
[root@server15 mysql]# kill -9 5455
ps aux | grep mysql
root 5624 0.0 0.0 112708 976 pts/0 R+ 18:22 0:00 grep --color=auto mysql 这个是grep的进程
5.更改完成
[root@server15 mysql]# systemctl start mariadb.service
[root@server15 mysql]# mysql -uroot -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
END
企业中不能重新安装数据库
[root@server15 mysql]# systemctl stop mariadb.service
[root@server15 mysql]# rm -fr /var/lib/mysql/
[root@server15 mysql]# yum reinstall mariadb-server.x86_64 -y
[root@server15 mysql]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
END
MariaDB [(none)]> SELECT Host,User FROM mysql.user;
+-----------+------+
| Host | User |
+-----------+------+
| 127.0.0.1 | root |
| ::1 | root |
| localhost | root |
+-----------+------+
只有管理员一个用户
MariaDB [(none)]> CREATE USER westos@localhost identified by 'westos';
建立一个用户只能在数据库所在的主机上使用(此用户只能本机登陆数据库),‘’引号引起来的为密码
MariaDB [(none)]> CREATE USER westo@'%'identified by 'westos';
建立一个可以远程登陆的用户(此用户可以通过网络登陆数据库),能否使用要看数据库远程登陆端口打开没
建立成功
MariaDB [(none)]> SELECT Host,User FROM mysql.user;
+-----------+--------+
| Host | User |
+-----------+--------+
| % | westo |
| 127.0.0.1 | root |
| ::1 | root |
| localhost | root |
| localhost | westos |
+-----------+--------+
删除用户
MariaDB [(none)]> DROP USER westo@'%';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> SELECT Host,User FROM mysql.user;
+-----------+--------+
| Host | User |
+-----------+--------+
| 127.0.0.1 | root |
| ::1 | root |
| localhost | root |
| localhost | westos |
+-----------+--------+
MariaDB [(none)]>
[root@server15 ~]# mysql -uwestos -pwestos
MariaDB [(none)]> quit
[root@server15 ~]# mysql -uwestos -pwestos -h172.25.138.15
ERROR 2003 (HY000): Can't connect to MySQL server on '172.25.138.15' (111)
[root@server15 ~]# mysql -ulee -pwestos -h172.25.138.15
ERROR 2003 (HY000): Can't connect to MySQL server on '172.25.138.15' (111)
我建立了一个远程登陆的用户lee
[root@server15 ~]# mysql -uroot -p
Enter password:
MariaDB [(none)]> CREATE DATABASE westostest ;
MariaDB [(none)]> CREATE TABLE westostest.userlist (
-> username varchar(10) not null,
-> password varchar(30) not null
-> );
MariaDB [(none)]> INSERT INTO westostest.userlist VALUES('lee','123'),('lee1','123');
MariaDB [(none)]> SELECT * FROM westostest.userlist;
+----------+----------+
| username | password |
+----------+----------+
| lee | 123 |
| lee1 | 123 |
+----------+----------+
开启远程登陆接口
[root@server15 ~]# vim /etc/my.cnf.d/server.cnf
注释掉这个
远程登陆
[root@server15 ~]# systemctl restart mariadb.service
[root@server15 ~]# mysql -ulee -pwestos -h172.25.138.15
MariaDB [(none)]>
[root@server15 ~]# mysql -uwestos -pwestos -h172.25.138.15
ERROR 1045 (28000): Access denied for user 'westos'@'server15' (using password: YES)
用westos用户登陆没有权限
看不见表格
[root@server15 ~]# mysql -uwestos -pwestos
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
MariaDB [(none)]>
[root@server15 ~]# mysql -uroot -p
MariaDB [(none)]> GRANT SELECT ON westostest.* TO westos@localhost; westostest库里面所有表都可以查询.*
TO给westos@localhost这个用户
MariaDB [(none)]> SHOW GRANTS FOR westos@localhost;查看授权成功没
+---------------------------------------------------------------------------------------------------------------+
| Grants for westos@localhost |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'westos'@'localhost' IDENTIFIED BY PASSWORD '*28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96' |
| GRANT SELECT ON `westostest`.* TO 'westos'@'localhost' |
+---------------------------------------------------------------------------------------------------------------+
GRANT SELECT ON `westostest`.* TO 'westos'@'localhost'
授权成功
授权能够查看但无法插入
[root@server15 ~]# mysql -uwestos -pwestos MariaDB [(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | westostest | +--------------------+ MariaDB [(none)]> SELECT * FROM westostest.userlist; +----------+----------+ | username | password | +----------+----------+ | lee | 123 | | lee1 | 123 | +----------+----------+ 2 rows in set (0.00 sec) MariaDB [(none)]> INSERT INTO westostest.userlist VALUES('test','123'); ERROR 1142 (42000): INSERT command denied to user 'westos'@'localhost' for table 'userlist'
[root@server15 ~]# mysql -uroot -p
MariaDB [(none)]> GRANT INSERT ON westostest.* TO westos@localhost ;
MariaDB [(none)]> SHOW GRANTS FOR westos@localhost;
+---------------------------------------------------------------------------------------------------------------+
| Grants for westos@localhost |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'westos'@'localhost' IDENTIFIED BY PASSWORD '*28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96' |
| GRANT SELECT, INSERT ON `westostest`.* TO 'westos'@'localhost' |
+---------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
插入和查看权限都有了
[root@server15 ~]# mysql -uwestos -pwestos
MariaDB [(none)]> INSERT INTO westostest.userlist VALUES('test','123');
Query OK, 1 row affected (0.04 sec)
[root@server15 ~]# mysql -uroot -p
MariaDB [(none)]> REVOKE INSERT ON westostest.* FROM westos@localhost;
MariaDB [(none)]> SHOW GRANTS FOR westos@localhost;
+---------------------------------------------------------------------------------------------------------------+
| Grants for westos@localhost |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'westos'@'localhost' IDENTIFIED BY PASSWORD '*28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96' |
| GRANT SELECT ON `westostest`.* TO 'westos'@'localhost' |
+---------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
[root@server15 ~]# mysql -uwestos -pwestos
MariaDB [(none)]> INSERT INTO westostest.userlist VALUES('test1','123');
ERROR 1142 (42000): INSERT command denied to user 'westos'@'localhost' for table 'userlist'
[root@server15 ~]# mysql -uroot -p
MariaDB [(none)]> SELECT * FROM mysql.user ;
这个是所有权限比如:Select Insert Update …
可以看到 root全是Y,所有权限都有
lee全是N没有任何权限
删掉用户
[root@server15 ~]# mysql -uroot -p
MariaDB [(none)]> DROP user westos@localhost;
END
[root@server15 ~]# mysql -uroot -p
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| westostest |
+--------------------+
[root@server15 ~]# mysqldump -uroot -pwestos mysql
它便会把mysql的库的内容用SQL语句输出到我们的屏幕上
节选很长一段sql语句
所以把它重定向,便可以保存下来
[root@server15 ~]# mysqldump -uroot -pwestos mysql > /mnt/mysql.sql
备份所有的库
[root@server15 ~]# mysqldump -uroot -pwestos --all-databases > /mnt/all.sql
只要备份库的结构不备份库中的数据
[root@server15 ~]# mysqldump -uroot -pwestos --all-databases --no-data > /mnt/all.sql
[root@server15 ~]# mysqldump -uroot -pwestos westostest > /mnt/westostest.sql
[root@server15 ~]# mysql -uroot -pwestos
MariaDB [(none)]> DROP DATABASE westostest;
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
使用备份的库时先得建立库,才能导入
使用备份的库时先得建立库,才能导入
[root@server15 ~]# mysql -uroot -pwestos -e "CREATE DATABASE westostest"
[root@server15 ~]# mysql -uroot -pwestos westostest < /mnt/westostest.sql
数据库还原成功
[root@server15 ~]# mysql -uroot -pwestos MariaDB [(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | westostest | +--------------------+ MariaDB [(none)]> SELECT * FROM westostest.userlist; +----------+----------+ | username | password | +----------+----------+ | lee | 123 | | lee1 | 123 | | test | 123 | +----------+----------+
END
可视化管理需要下载一个插件phpmyadmin
进入官网下载
我用的是3.4的版本,个人觉得好用
[root@foundation38 ~]# scp /var/run/media/kiosk/C6E6206CE6205EC5/phpMyAdmin-3.4.0-all-languages.tar.bz2 root@172.25.138.15:/mnt
[root@server15 ~]# cd /mnt
[root@server15 mnt]# ls
all_nodata.sql all.sql mysql.sql phpMyAdmin-3.4.0-all-languages.tar.bz2 westostest.sql
安装该插件
[root@server15 yum.repos.d]# yum install httpd php php-mysql.x86_64 -y
因为基于httpd发布,php默认不支持数据库,所以还得安装相应的插件让php支持数据库
[root@server15 yum.repos.d]# systemctl start firewalld.service
[root@server15 yum.repos.d]# systemctl enable --now httpd
[root@server15 yum.repos.d]# systemctl restart httpd
[root@server15 yum.repos.d]# firewall-cmd --permanent --add-service=http
[root@server15 yum.repos.d]# firewall-cmd --reload
[root@server15 mnt]# yum install bzip2 -y
[root@server15 mnt]# tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 -C /var/www/html/
[root@server15 mnt]# cd /var/www/html/
[root@server15 html]# ls
phpMyAdmin-3.4.0-all-languages
[root@server15 html]# mv phpMyAdmin-3.4.0-all-languages mysqladmin
改个名字要不然名字太长了,不好访问
[root@server15 html]# cd mysqladmin/
[root@server15 mysqladmin]# ls
软件的安装文档
快速安装步骤
[root@server15 mysqladmin]# cp config.sample.inc.php config.sample.php
END
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。