初始化MariaDB服务
[root@localhost ~]# yum -y install mariadb mariadb-server
[root@localhost ~]# systemctl start mariadb.service
[root@localhost ~]# systemctl enable mariadb.service
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
//再确认mariadb数据库软件程序安装完毕并成功启动后请不要立即使用。为了确保数据库的安全性和正常运转,需要做以下5个操作
1.设置root管理员在数据库中的密码值(该密码并非root管理员在系统中的密码,这里的密码值默认应该为空)
2.设置root管理员在数据库中的专有密码
3.删除匿名用户,并使用root管理员从远程登陆数据库,以确保数据库上的运行的业务安全性
4.删除默认的测试数据库,取消测试数据库的一些访问权限
5.刷新授权列表,初始化的设定立即生效
[root@localhost ~]# mysql_secure_installation
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] y(设置管理员密码)
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] y(删除匿名用户)
... 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] y(禁止管理员从远程登陆)
... 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] y(删除测试数据库及其权限)
- 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] y(刷新授权表,让初始化设定立即生效)
... 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!
实际生产环境中都是库站分离技术,需要修改防火墙放通服务(mysql)
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=mysql
success
[root@localhost ~]# firewall-cmd --reload
success
//使用刚才设置好的密码进行数据库登录
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.3.28-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)]>
//修改root密码
MariaDB [(none)]> set password = password('linuxprobe');
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> exit
Bye
[root@localhost ~]# mysql -uroot -p
Enter password:(输入旧密码提示密码错误)
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
//创建数据库普通用户
MariaDB [(none)]> create user luke@localhost identified by 'linuxprobe';
Query OK, 0 rows affected (0.000 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]> select host,user,password from user where user="luke";
+-----------+------+-------------------------------------------+
| host | user | password |
+-----------+------+-------------------------------------------+
| localhost | luke | *55D9962586BE75F4B7D421E6655973DB07D6869F |
+-----------+------+-------------------------------------------+
1 row in set (0.000 sec)
//普通用户无太多数据库操作权限,需要对用户进行授权
grant 权限 on 数据库.表单名称 to 用户名@主机名(对某个特定数据库中的特定表单给予授权)
grant 权限 on 数据库.* to 用户名@主机名(对某个特定数据库中的所有表单给予授权)
grant 权限 on . to 用户名@主机名(对所有数据库及所有表单给予授权)
grant 权限1,权限2 on 数据库.* to 用户名@主机名(对某个数据库中的所有表单给予多个授权)
grant privileges on . to 用户名@主机名(对所有数据库中的所有表单给予全部授权,需谨慎操作)
//查看数据库用户权限
MariaDB [mysql]> show grants for luke@localhost;
+-------------------------------------------------------------------------------------------------------------+
| Grants for luke@localhost |
+-------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON . TO luke@localhost IDENTIFIED BY PASSWORD '*55D9962586BE75F4B7D421E6655973DB07D6869F' |
+-------------------------------------------------------------------------------------------------------------+
1 row in set (0.000 sec)
//创建数据库
MariaDB [mysql]> create database linuxprobe;
Query OK, 1 row affected (0.001 sec)
MariaDB [mysql]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linuxprobe |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)
//创建表单
MariaDB [linuxprobe]> create table mybook (name char(15),price int,pages int);
Query OK, 0 rows affected (0.004 sec)
MariaDB [linuxprobe]> describe mybook;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(15) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| pages | int(11) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.001 sec)
//插入数据
MariaDB [linuxprobe]> insert into mybook(name,price,pages) values('linuxprobe','60','518');
Query OK, 1 row affected (0.001 sec)
MariaDB [linuxprobe]> select * from mybook;
+------------+-------+-------+
| name | price | pages |
+------------+-------+-------+
| linuxprobe | 60 | 518 |
+------------+-------+-------+
1 row in set (0.000 sec)
//查找语句
Where命令中使用的参数及其作用
=:相等
<>或!=:不相等
>:大于
<:小于
>=:大于或等于
<=:小于或等于
BETWEEN:在某个范围内
LIKE:搜索一个例子
IN:在列表中搜索多个值
//数据库的备份与回复
[root@localhost ~]# mysqldump -u root -p linuxprobe > /root/linuxprobeDB.dump
Enter password:
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 21
Server version: 10.3.28-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)]> drop database linuxprobe;
Query OK, 1 row affected (0.002 sec)
MariaDB [(none)]> create database linuxprobe;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> exit
Bye
[root@localhost ~]# mysql -u root -p linuxprobe < /root/linuxprobeDB.dump
Enter password:
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 23
Server version: 10.3.28-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)]> use linuxprobe;
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 [linuxprobe]> show tables;
+----------------------+
| Tables_in_linuxprobe |
+----------------------+
| mybook |
+----------------------+
1 row in set (0.000 sec)
MariaDB [linuxprobe]> describe mybook;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(15) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| pages | int(11) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.000 sec)