MariaDb數據庫的入門使用--學習筆記

MariaDb簡介,因爲Oracle公司在2009年收購了myql的母公司Sun,所以mysql數據項目也隨之歸入了Oracle公司,逐漸演變爲保持着開源軟件的身份,可是又申請了多項商業專利的軟件系統。因此mysql項目創始者從新研發了一款爲maridb的全新數據庫管理系統,幾乎徹底兼容mysql,在使用上幾乎一致。mysql

1.1 安裝mariadb服務

# yum安裝
[root@localhost ~]# yum install mariadb mariadb-server -y
# 設置開機自啓動
[root@localhost ~]# systemctl enable mariadb
ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'
[root@localhost ~]# systemctl start mariadb

1.2 設置防火牆

[root@localhost Desktop]# firewall-cmd --permanent --add-service=mysql
success
[root@localhost Desktop]# firewall-cmd --reload
success

1.3 初始化mariadb

# 初始化數據庫
[root@localhost Desktop]# 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] y 【設置數據庫root用戶密碼】
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【禁止root用戶遠程登陸】
 ... 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【刪除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] 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!

1.4 數據庫的基本操做命令

# 登陸數據庫
[root@localhost Desktop]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
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)]> 

# 重置root用戶密碼爲‘redhat’
MariaDB [(none)]> set password=password('redhat');
Query OK, 0 rows affected (0.00 sec)

# 建立用戶developer,並受權developer對mysql.user表的select,insert,update,delete權限
MariaDB [(none)]> create user developer@localhost identified by 'redhat';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> grant select,insert,update,delete on mysql.user to developer@localhost;
Query OK, 0 rows affected (0.00 sec)

# 移除developer用戶對mysql.user表的select,insert,update,delete權限
MariaDB [(none)]> revoke select,insert,update,delete on mysql.user from developer@localhost;
Query OK, 0 rows affected (0.00 sec)

# 建立一個名爲db的數據庫
MariaDB [(none)]> create database db;
Query OK, 1 row affected (0.00 sec)
# 進入db數據庫
MariaDB [(none)]> use db;
Database changed

# 建立一個表單
MariaDB [db]> create table user(name char(15),id int,age int);
Query OK, 0 rows affected (0.01 sec)

# 查看錶結構
MariaDB [db]> desc user;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name  | char(15) | YES  |     | NULL    |       |
| id    | int(11)  | YES  |     | NULL    |       |
| age   | int(11)  | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.01 sec)

# 列出全部表單
MariaDB [db]> show tables;
+--------------+
| Tables_in_db |
+--------------+
| user         |
+--------------+
1 row in set (0.00 sec)

# insert 命令,插入數據
MariaDB [db]> insert into user(name,id,age) values('xiaosong',1,18);
Query OK, 1 row affected (0.01 sec)
MariaDB [db]> insert into user(name,id,age) values('zhangwu',2,24);
Query OK, 1 row affected (0.00 sec)
MariaDB [db]> insert into user(name,id,age) values('wangliu',3,31);
Query OK, 1 row affected (0.00 sec)

# 查詢user表單的全部數據
MariaDB [db]> select * from user;
+----------+------+------+
| name     | id   | age  |
+----------+------+------+
| xiaosong |    1 |   18 |
| zhangwu  |    2 |   24 |
| wangliu  |    3 |   31 |
+----------+------+------+
3 rows in set (0.00 sec)

# update命令,更新小松的年齡爲15
MariaDB [db]> update user set age=15 where name='xiaosong';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
MariaDB [db]> select * from user;
+----------+------+------+
| name     | id   | age  |
+----------+------+------+
| xiaosong |    1 |   15 |
| zhangwu  |    2 |   24 |
| wangliu  |    3 |   31 |
+----------+------+------+
3 rows in set (0.00 sec)

# delete命令,刪除wangliu用戶信息
MariaDB [db]> delete from user where name='wangliu';
Query OK, 1 row affected (0.00 sec)

MariaDB [db]> select * from user;
+----------+------+------+
| name     | id   | age  |
+----------+------+------+
| xiaosong |    1 |   15 |
| zhangwu  |    2 |   24 |
+----------+------+------+
2 rows in set (0.00 sec)

# 刪除user表
MariaDB [db]> drop table user;
# 刪除db庫
MariaDB [db]> drop database db;

1.5 數據庫的備份和恢復

備份db庫到當前目錄的db.dump文件sql

[root@localhost ~]# mysqldump -uroot -p db >db.dump
Enter password:  【root密碼】

恢復數據庫

[root@localhost ~]# mysq -uroot -p db <db.dump
Enter password:  【root密碼】
相關文章
相關標籤/搜索