1、安装MariaDB数据库主程序和服务端
[root@fudanwuxi ~]
[root@fudanwuxi ~]
[root@fudanwuxi ~]
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
2、初始化
[root@fudanwuxi ~]
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!
3、登陆数据库(修改密码、创建用户、授权、取消授权)
[root@fudanwuxi ~]
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, 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 |
+
3 rows in set (0.00 sec)
MariaDB [(none)]> set password = PASSWORD('fudanwuxi');
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> exit
Bye
[root@fudanwuxi ~]
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@fudanwuxi ~]
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
[root@fudanwuxi ~]
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create user ajie@localhost identified by 'ajie123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
[root@fudanwuxi ~]
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, 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 |
+
1 row in set (0.00 sec)
MariaDB [(none)]>
[root@fudanwuxi ~]# mysql -u root -p #使用数据库root用户登陆
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant select,update,delete,insert ON mysql.* to ajie@localhost; #授予ajie用户查询、更新、删除、插入权限。mysql.*代表授予ajie的权限对数据库mysql下的所有表单有效
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
[root@fudanwuxi ~]# mysql -u ajie -p #退出root,使用ajie来登陆
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases; #此时查看所有数据库的时候,可以看到有mysql了
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
+--------------------+
2 rows in set (0.00 sec)
MariaDB [(none)]> use mysql #进入使用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; #查看数据库mysql中的所有表单
+---------------------------+
| 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.01 sec)
MariaDB [mysql]>
[root@fudanwuxi ~]
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> revoke select,update,delete,insert on mysql.* from ajie@localhost;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show grants for ajie@localhost;
+
| Grants for ajie@localhost |
+
| GRANT USAGE ON *.* TO 'ajie'@'localhost' IDENTIFIED BY PASSWORD '*A65FBC245EF7CC4346000B7CFA058E0D5A234219' |
+
1 row in set (0.00 sec)
MariaDB [(none)]> exit
Bye
[root@fudanwuxi ~]
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, 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 |
+
1 row in set (0.00 sec)
MariaDB [(none)]>
4、创建数据库及表单(查找、修改、删除)
[root@fudanwuxi ~]# mysql -u root -p #以root登陆
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 22
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> create database wechat; #新增一个数据库,名称为wechat。删除使用drop。
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases; #再查看数据库,多了一个wechat
+--------------------+
| information_schema |
| mysql |
| performance_schema |
4 rows in set (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]> use wechat; #进入使用数据库wechat
Database changed
MariaDB [wechat]> create table myid(name char(15),address char(15),price int);
#创建一个表单,名称为myid。
定义存储数据内容的结构,三个字段,
其中长度为15个字符的字符型字段name存放微信名;
长度为15个字符的字符型字段address存放使用地区;
整型字段price存放售卖价格。
Query OK, 0 rows affected (0.01 sec)
MariaDB [wechat]> show tables; #再查看表单,多了一个刚刚新建的myid表单
+------------------+
1 row in set (0.00 sec)
MariaDB [wechat]> describe myid; #初始化新建的表单myid
+---------+----------+------+-----+---------+-------+
| name | char(15) | YES | | NULL | |
| address | char(15) | YES | | NULL | |
3 rows in set (0.00 sec)
MariaDB [wechat]>
MariaDB [wechat]> insert INTO myid(name,address,price) VALUES('jschinamobile','jiangsu','999'); #向表单myid中插入一条信息,name,address,price它们的值分别对应jschinamobile,jiangsu,999
Query OK, 1 row affected (0.00 sec)
MariaDB [wechat]> select * from myid; #查询myid表单中的所有数据
+---------------+---------+-------+
1 row in set (0.00 sec)
MariaDB [wechat]> select name,price from myid; #仅查询myid表单中的name和price数据
+---------------+-------+
1 row in set (0.00 sec)
MariaDB [wechat]> update myid set price='666666' where name='jschinamobile'; #将name值为jschinamobile的price值修改为666666
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [wechat]> select * from myid; #查看表单,price值变为了666666
+---------------+---------+--------+
1 row in set (0.00 sec)
MariaDB [wechat]> delete from myid; #删除myid表单中的所有内容
Query OK, 1 row affected (0.00 sec)
MariaDB [wechat]> select * from myid; #查询myid表单,提示空的
Empty set (0.00 sec)
MariaDB [wechat]>
MariaDB [wechat]> insert INTO myid(name,address,price) VALUES('jschinamobile','jiangsu','999');
Query OK, 1 row affected (0.00 sec)
MariaDB [wechat]> insert INTO myid(name,address,price) VALUES('www.wuxier.cn','jiangsu','555');
Query OK, 1 row affected (0.00 sec)
MariaDB [wechat]> insert INTO myid(name,address,price) VALUES('fudan','shanghai','1000');
Query OK, 1 row affected (0.00 sec)
MariaDB [wechat]> insert INTO myid(name,address,price) VALUES('ajie','dongtai','666');
Query OK, 1 row affected (0.00 sec)
MariaDB [wechat]> select * from myid;
+---------------+----------+-------+
| jschinamobile | jiangsu | 999 |
| www.wuxier.cn | jiangsu | 555 |
| fudan | shanghai | 1000 |
4 rows in set (0.00 sec)
MariaDB [wechat]> select * from myid where name='ajie'; #查找表单myid中name值为ajie的
+------+---------+-------+
1 row in set (0.00 sec)
MariaDB [wechat]> select name,price from myid where name='ajie'; #查找值为ajie,只看name和price两个信息
+------+-------+
1 row in set (0.00 sec)
MariaDB [wechat]> select * from myid where price='555'; #查找表单myid中price值为555的
+---------------+---------+-------+
1 row in set (0.00 sec)
MariaDB [wechat]> select * from myid where price>'600'; #查找表单myid中price值大于600的
+---------------+----------+-------+
| jschinamobile | jiangsu | 999 |
| fudan | shanghai | 1000 |
3 rows in set (0.00 sec)
5、数据库的备份
[root@fudanwuxi ~]
Enter password:
[root@fudanwuxi ~]
total 12
-rw-------. 1 root root 1783 May 14 12:46 anaconda-ks.cfg
-rw-r--r--. 1 root root 1972 Aug 6 14:35 backup_wechat.dump
drwxr-xr-x. 2 root root 6 Jul 28 15:21 Desktop
drwxr-xr-x. 2 root root 6 Jul 28 15:21 Documents
drwxr-xr-x. 2 root root 6 Jul 28 15:21 Downloads
-rw-r--r--. 1 root root 1831 May 14 12:53 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 Jul 28 15:21 Music
drwxr-xr-x. 2 root root 6 Jul 28 15:21 Pictures
drwxr-xr-x. 2 root root 6 Jul 28 15:21 Public
drwxr-xr-x. 2 root root 6 Jul 28 15:21 Templates
drwxr-xr-x. 2 root root 6 Jul 28 15:21 Videos
[root@fudanwuxi ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 26
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| information_schema |
| mysql |
| performance_schema |
4 rows in set (0.00 sec)
MariaDB [(none)]> use wechat
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 [wechat]> show tables;
+------------------+
1 row in set (0.00 sec)
MariaDB [wechat]> select * from myid;
+---------------+----------+-------+
| jschinamobile | jiangsu | 999 |
| www.wuxier.cn | jiangsu | 555 |
| fudan | shanghai | 1000 |
4 rows in set (0.00 sec)
MariaDB [wechat]> delete from myid; #删除
Query OK, 4 rows affected (0.01 sec)
MariaDB [wechat]> select * from myid; #再查询表单myid中数据为空
Empty set (0.00 sec)
[root@fudanwuxi ~]# mysql -u root -p wechat < /root/backup_wechat.dump #输入重定向来导入数据wechat进行还原
Enter password:
[root@fudanwuxi ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 29
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| information_schema |
| mysql |
| performance_schema |
4 rows in set (0.00 sec)
MariaDB [(none)]> use wechat;
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 [wechat]> show tables;
+------------------+
1 row in set (0.00 sec)
MariaDB [wechat]> select * from myid; #查看表单myid中的信息,还原成功
+---------------+----------+-------+
| jschinamobile | jiangsu | 999 |
| www.wuxier.cn | jiangsu | 555 |
| fudan | shanghai | 1000 |
4 rows in set (0.02 sec)