MariaDB 数据库管理系统是Mysql的一个分支,主要由开源社区在维护。
数据库
一、安装—安全初始化
安装
[root@shareserver ~]# systemctl start mariadb
安全初始化
(tu19)(tu20)
2、查看端口是否开启
[root@shareserver ~]# netstat -antlupe | grep mysql ##端口开启不安全
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 27 83346 8498/mysqld
[root@shareserver ~]#
关闭端口
[root@shareserver ~]# vim /etc/my.cnf
[root@shareserver ~]# systemctl restart mariadb
[root@shareserver ~]# netstat -antlupe | grep mysql
二、数据库使用
1、进入数据库
[root@shareserver ~]# mysql -uroot -p ##进入数据库
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.44-MariaDB MariaDB Server
Copyright © 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statemen
2、显示数据库
3、使用数据库
4、查看所有表
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)
5、数据查询
MariaDB [mysql]> select Delete_priv,Drop_priv,Shutdown_priv from user; ##查看user表的部分数据
6、创建数据库
MariaDB [mysql]> create database westos; ##创建数据库
Query OK, 1 row affected (0.00 sec)
MariaDB [mysql]> show databases ##显示数据库
-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| westos |
+--------------------+
4 rows in set (0.00 sec)
7、创建表
MariaDB [mysql]> use westos ##使用数据库
Database changed
MariaDB [westos]> create table linux ( ##创建一个表
->username varchar(10) not null,
->password varchar(50) not null );
Query OK, 0 rows affected (0.05 sec)
MariaDB [westos]> desc linux; ##显示表格式
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(10) | NO | | NULL | |
| password | varchar(50) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
8、编辑表内容
MariaDB [westos]> insert into linux values('lee','123'); ##给表里添加内容
Query OK, 1 row affected (0.02 sec)
MariaDB [westos]> desc linux;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(10) | NO | | NULL | |
| password | varchar(50) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
MariaDB [westos]> select * from linux;
+----------+----------+
| username | password |
+----------+----------+
| lee | 123 |
+----------+----------+
1 row in set (0.00 sec)
MariaDB [westos]>
9、更改
(1)更改表里数据信息
MariaDB [westos]> update linux set password=('lee') where username='lee';##修改表里用户密码
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [westos]> select * from linux;
+----------+----------+
| username | password |
+----------+----------+
| lee | lee |
+----------+----------+
1 row in set (0.00 sec)
MariaDB [westos]>
(2)删除数据
MariaDB [westos]> update linux set password=('lee') where username=lee;
ERROR 1054 (42S22): Unknown column 'lee' in 'where clause'
MariaDB [westos]> update linux set password=('lee') where username='lee';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [westos]> select * from linux;
+----------+----------+
| username | password |
+----------+----------+
| lee | lee |
+----------+----------+
1 row in set (0.00 sec)
(3)给表里添加信息
MariaDB [westos]> alter table linux add class varchar(20); ##给表里添加class行
Query OK, 1 row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0
MariaDB [westos]> desc linux;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(10) | NO | | NULL | |
| password | varchar(50) | NO | | NULL | |
| class | varchar(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
(4)删除表结构
MariaDB [westos]> alter table linux drop class; #删除表里的class行
Query OK, 1 row affected (0.06 sec)
Records: 1 Duplicates: 0 Warnings: 0
MariaDB [westos]> desc linux;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(10) | NO | | NULL | |
| password | varchar(50) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
(5)添加表结构
MariaDB [westos]> alter table linux add age varchar(20) after password; ##给表里添加add并且密码不为空
Query OK, 1 row affected (0.05 sec)
Records: 1 Duplicates: 0 Warnings: 0
MariaDB [westos]> desc linux;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(10) | NO | | NULL | |
| password | varchar(50) | NO | | NULL | |
| age | varchar(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
(6)更改表名称
MariaDB [westos]> alter table linux rename redhat; ##将表名改为redhat
Query OK, 0 rows affected (0.01 sec)
MariaDB [westos]> desc linux;
ERROR 1146 (42S02): Table 'westos.linux' doesn't exist
MariaDB [westos]> desc redhat;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(10) | NO | | NULL | |
| password | varchar(50) | NO | | NULL | |
| age | varchar(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
MariaDB [westos]>
10、删除
MariaDB [westos]> delete from redhat where username='lee';
Query OK, 1 row affected (0.01 sec)
MariaDB [westos]> select * from redhat;
+----------+----------+------+
| username | password | age |
+----------+----------+------+
| lee1 | 5 | 123 |
+----------+----------+------+
1 row in set (0.00 sec)
MariaDB [westos]> drop table redhat;
Query OK, 0 rows affected (0.00 sec)
MariaDB [westos]> show tables;
Empty set (0.00 sec)
11、用户授权
MariaDB [westos]> create user lee@'localhost' identified by 'westos'; ##设置用户lee为本地登陆密码为westos
Query OK, 0 rows affected (0.00 sec)
MariaDB [westos]>
12、数据备份
[root@shareserver ~]# mysqldump -uroot -predhat westos > /mnt/westos.sql ##将数据库westos备份到/mnt/westos.sql
[root@shareserver ~]# vim /mnt/westos.sql
[root@shareserver ~]# mysql -uroot -predhat -e "DROP DATABASE westos"
[root@shareserver ~]# mysql -uroot -predhat -e "SHOW DATABASES;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
[root@shareserver ~]#
[root@shareserver ~]# mysql -uroot -predhat -e "SHOW DATABASES;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
[root@shareserver ~]# mysql -uroot -predhat -e "CREATE DATABASE westos"
[root@shareserver ~]# mysql -uroot -predhat -e "SHOW DATABASES;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| westos |
+--------------------+
[root@share-server ~]#
13、数据库管理
当数据库密码忘记时
(1)模拟密码改变
[root@shareserver ~]# mysqladmin -uroot -predhat password lee ##修改root用户的登陆密码为lee
[root@shareserver ~]# mysql -uroot -plee
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> exit
Bye
[root@share-server ~]#
(2)关闭数据库服务
[root@shareserver ~]# systemctl stop mysql ##关闭数据库服务
Failed to issue method call: Unit mysql.service not loaded.
[root@shareserver ~]# mysqld_safe --skip-grant-tables &##进去安全模式不读取授权文件
[1] 7671
[root@shareserver ~]# 181125 03:01:02 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
181125 03:01:02 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
^C
[root@shareserver ~]#
(3)直接登陆数据库
[root@shareserver ~]# mysql##可直接登陆
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select User,Password from mysql.user; ##查看用户名密码
+------+-------------------------------------------+
| User | Password |
+------+-------------------------------------------+
| root | *9BB439A3A652A9DAD3718215F77A7AA06108A267 |
| root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| lee | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
+------+-------------------------------------------+
4 rows in set (0.00 sec)
(4)修改密码
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
MariaDB [(none)]> select User,Password from mysql.user;
+------+-------------------------------------------+
| User | Password |
+------+-------------------------------------------+
| root | redhat |
| root | redhat |
| root | redhat |
| lee | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
+------+-------------------------------------------+
4 rows in set (0.00 sec)
(5)密码加密
此时改变的密码为明文密码,不安全
MariaDB [(none)]> UPDATE mysql.user SET Password=password('redhat') WHERE User='root'; ##密码加密
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
MariaDB [(none)]> select User,Password from mysql.user;
+------+-------------------------------------------+
| User | Password |
+------+-------------------------------------------+
| root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| lee | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
+------+-------------------------------------------+
4 rows in set (0.01 sec)
MariaDB [(none)]>
(6)结束数据库进程
[root@shareserver ~]# ps aux | grep mysql
root 7671 0.0 0.1 113252 1560 pts/0 S 03:01 0:00 /bin/sh /usr/bin/mysqld_safe --skip-grant-tables
mysql 7826 0.0 9.2 859064 89480 pts/0 Sl 03:01 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 7908 0.0 0.0 112640 936 pts/0 S+ 03:07 0:00 grep --color=auto mysql
[root@shareserver ~]# kill -9 7671
[root@shareserver ~]# kill -9 7826
[1]+ Killed mysqld_safe --skip-grant-tables
[root@shareserver ~]# ps aux | grep mysql
root 7919 0.0 0.0 112640 936 pts/0 S+ 03:08 0:00 grep --color=auto mysql
[root@shareserver ~]#
打开服务
[root@shareserver ~]# systemctl start maridb
[root@shareserver ~]# mysql -uroot -predhat
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.35-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>