1.登录
[[email protected] ~]# yum install mariadb-server -y         ##安装mysql服务器
[[email protected] ~]# vim /etc/my.cnf
10 skip-networking=1
[[email protected] ~]# systemctl restart mariadb
[[email protected] ~]# mysql_secure_installation             ##第一次安装mysql以后通过这条命令可以对mysql进行设置
/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    ##设置密码(westos)
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##刷新数据库
  -   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!

linux学习之mariadb

2.查询
[[email protected] ~]# systemctl start mariadb               ##开启服务
[[email protected] ~]# mysql -uroot -pwestos                 ##从本机登录mysql数据库
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 17
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)]> show databases;                        ##显示数据库
  +--------------------+
  | Database           |
  +--------------------+
  | information_schema |
  | mysql              |
  | performance_schema |
  | test               |
  +--------------------+

  4 rows in set (0.00 sec)

linux学习之mariadb

  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

linux学习之mariadb

  MariaDB [mysql]> select * from user;                   ##查询user表中的所有字段(*可以用此表中的任何字段来代替)

linux学习之mariadb

  MariaDB [mysql]> select  Host from user;               ##查询user表中的host字段
  +---------------------+
 | Host                |
 +---------------------+
 | 127.0.0.1           |
 | ::1                 |
 | localhost           |
 | localhost           |
 | mariadb.example.com |
 | mariadb.example.com |
 +---------------------+

 6 rows in set (0.00 sec)

linux学习之mariadb

 MariaDB [mysql]> select Host,User from user;          ##查询user表中的host,user字段
 +---------------------+------+
 | Host                | User |
 +---------------------+------+
 | 127.0.0.1           | root |
 | ::1                 | root |
 | localhost           |      |
 | localhost           | root |
 | mariadb.example.com |      |
 | mariadb.example.com | root |
 +---------------------+------+

 6 rows in set (0.00 sec)

linux学习之mariadb

 MariaDB [mysql]> select Host,User,password from user; ##查询user表中的host,user,password,from user
 +---------------------+------+----------+
 | Host                | User | password |
  +---------------------+------+----------+
 | localhost           | root |          |
 | mariadb.example.com | root |          |
 | 127.0.0.1           | root |    
 | ::1                 | root |          |
 | localhost           |      |          |
 | mariadb.example.com |      |          |
  +---------------------+------+----------+
  6 rows in set (0.00 sec)
 
  MariaDB [mysql]> select Host,User,password,Select_priv from user;
  +---------------------+------+----------+-------------+
  | Host                | User | password | Select_priv |
  +---------------------+------+----------+-------------+
  | localhost           | root |          | Y           |
  | mariadb.example.com | root |          | Y           |
  | 127.0.0.1           | root |          | Y           |
  | ::1                 | root |          | Y           |
  | localhost           |      |          | N           |
  | mariadb.example.com |      |          | N           |
 +---------------------+------+----------+-------------+

  6 rows in set (0.00 sec)

linux学习之mariadb

  MariaDB [mysql]> desc user;                  ##查询user表中的结构,显示所有字段的名称

linux学习之mariadb

  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)

linux学习之mariadb

3.数据库及表的建立
[[email protected] mysql]# mysql -uroot -pwestos        ##登录
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)]> create database westos;  ##创建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)

linux学习之mariadb

MariaDB [(none)]> use westos;         ##进入westos库
Database changed
MariaDB [westos]> show tables;        ##显示westos库中的表
Empty set (0.00 sec)
MariaDB [westos]> create table linux( username varchar(15) not null, password varchar(50) not null );##创建linux表,并且linux表含有两个字段,username,password,username字段,字符长度最大为15个,并且不能为空
Query OK, 0 rows affected (0.41 sec)
MariaDB [westos]> desc linux;     ##查看linux表中的数据结构
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(15) | NO   |     | NULL    |       |
| password | varchar(50) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

linux学习之mariadb

MariaDB [westos]> insert into linux values ('user1','123');  ##向linux表中插入数据,username字段的数据为user1,密码为123
Query OK, 1 row affected (0.09 sec)

MariaDB [westos]> select * from linux;    ##查询inux表中的所有字段
+----------+----------+
| username | password |
+----------+----------+
| user1    | 123      |
+----------+----------+

1 row in set (0.00 sec)

linux学习之mariadb

MariaDB [westos]> insert into westos.linux  values ('user4',password('123') );##插入password字段的数据,密码为加密性123
Query OK, 1 row affected (0.34 sec)

MariaDB [westos]> select * from linux;   
+----------+-------------------------------------------+
| username | password                                  |
+----------+-------------------------------------------+
| user1    | 123                                       |
| user2    | 123                                       |
| user3    | 123                                       |
| user4    | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |

+----------+-------------------------------------------+


4 rows in set (0.00 sec)

linux学习之mariadb

4.更新数据库信息

MariaDB [westos]> update linux set password=password('123') where username='user1';##更新user1的密码,将user1密码改成加密型
Query OK, 1 row affected (0.10 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [westos]> select * from linux;
+----------+-------------------------------------------+
| username | password                                  |
+----------+-------------------------------------------+
| user1    | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| user2    | 123                                       |
| user3    | 123                                       |
| user4    | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+----------+-------------------------------------------+

4 rows in set (0.00 sec)

linux学习之mariadb

 
MariaDB [westos]> delete from linux where username='user1';##删除user1的信息
Query OK, 1 row affected (0.34 sec)

MariaDB [westos]> select * from linux;
+----------+-------------------------------------------+
| username | password                                  |
+----------+-------------------------------------------+
| user2    | 123                                       |
| user3    | 123                                       |
| user4    | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+----------+-------------------------------------------+

3 rows in set (0.00 sec)

linux学习之mariadb

MariaDB [westos]> alter table linux add age varchar(4);##在linux表格中添加age字段,最大字符数为4,默认为在最后一列后添加
Query OK, 3 rows affected (0.48 sec)               
Records: 3  Duplicates: 0  Warnings: 0

MariaDB [westos]> select * from linux;##查询linux表格中的数据
+----------+-------------------------------------------+------+
| username | password                                  | age  |
+----------+-------------------------------------------+------+
| user2    | 123                                       | NULL |
| user3    | 123                                       | NULL |
| user4    | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | NULL |
+----------+-------------------------------------------+------+

3 rows in set (0.00 sec)

linux学习之mariadb

MariaDB [westos]> alter table linux add class varchar(20)  not null after name;##在linux表的name字段后面加入class字段(not null)
Query OK, 3 rows affected (0.55 sec)               
Records: 3  Duplicates: 0  Warnings: 0
MariaDB [westos]> select * from linux;
+----------+-------------------------------------------+------+-------+------+
| username | password                                  | name | class | age  |
+----------+-------------------------------------------+------+-------+------+
| user2    | 123                                       | NULL |       | NULL |
| user3    | 123                                       | NULL |       | NULL |
| user4    | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | NULL |       | NULL |
+----------+-------------------------------------------+------+-------+------+

3 rows in set (0.00 sec)

linux学习之mariadb

MariaDB [westos]> alter table linux drop age;##删除linux表中的age字段
Query OK, 3 rows affected (0.41 sec)               

Records: 3  Duplicates: 0  Warnings: 0



MariaDB [westos]> select * from linux;
+----------+-------------------------------------------+------+-------+
| username | password                                  | name | class |
+----------+-------------------------------------------+------+-------+
| user2    | 123                                       | NULL |       |
| user3    | 123                                       | NULL |       |
| user4    | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | NULL |       |
+----------+-------------------------------------------+------+-------+

3 rows in set (0.00 sec)

linux学习之mariadb


5.删除数据库
MariaDB [westos]> delete from linux where username='user2';##从linux表中删除user1数据
Query OK, 1 row affected (0.33 sec)

MariaDB [westos]> select * from linux;
+----------+-------------------------------------------+------+-------+
| username | password                                  | name | class |
+----------+-------------------------------------------+------+-------+
| user3    | 123                                       | NULL |       |
| user4    | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | NULL |       |
+----------+-------------------------------------------+------+-------+
2 rows in set (0.00 sec)
MariaDB [westos]> select * from linux;
+----------+-------------------------------------------+------+-------+
| username | password                                  | name | class |
+----------+-------------------------------------------+------+-------+
| user3    | 123                                       | NULL |       |
| user4    | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | NULL |       |
+----------+-------------------------------------------+------+-------+

2 rows in set (0.00 sec)

linux学习之mariadb


MariaDB [westos]> drop table linux;##删除linux表
Query OK, 0 rows affected (0.35 sec)
MariaDB [westos]> select * from linux;

ERROR 1146 (42S02): Table 'westos.linux' doesn't exist

linux学习之mariadb

MariaDB [(none)]> drop database westos;##删除westos库
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> use  westos

ERROR 1049 (42000): Unknown database 'westos'

linux学习之mariadb

6.数据库的备份(先重建库)

[[email protected] ~]# mysqldump  -u root -pwestos   --all-database##备份所有表中的所有数据

linux学习之mariadb

[[email protected] ~]# mysqldump  -u root -pwestos   --all-database  --no-data##备份所有表但不备份数据


[[email protected] ~]# mysqldump  -u root -pwestos   westos##备份westos库
[[email protected] ~]# mysqldump  -u root -pwestos   westos >  /mnt/westos.sql##备份westos库并把数据导到/mnt/westos.sql中
[[email protected] ~]# mysql -uroot -pwestos -e "drop database  westos;"##删除westos数据库
[[email protected] ~]# mysql -uroot -pwestos -e "show databases;"##显示数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |

+--------------------+

linux学习之mariadb

[[email protected] ~]# mysql -uroot -pwestos -e "create database  westos;"##重新建立westos库

[[email protected] ~]# mysqldump  -u root -pwestos   westos <  /mnt/westos.sql##将数据导入westos库

linux学习之mariadb

[[email protected] ~]# mysql -uroot -pwestos -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| westos             |
+--------------------+
[[email protected] ~]# mysql -uroot -pwestos -e "show tables from westos;"(没有内容)

[[email protected] ~]# mysql -uroot -pwestos
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 30
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)]> use westos
Database changed
MariaDB [westos]> desc linux;
ERROR 1146 (42S02): Table 'westos.linux' doesn't exist
MariaDB [westos]> create table linux( username varchar(15) not null, password varchar(50) not null );##新建linux表格
Query OK, 0 rows affected (0.38 sec)

MariaDB [westos]> desc linux;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(15) | NO   |     | NULL    |       |
| password | varchar(50) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

MariaDB [westos]> quit
[[email protected] ~]# mysqldump  -u root -pwestos   westos linux >  /mnt/linux.sql##备份westos库中的linux表格
[[email protected] ~]# mysql -uroot -pwestos -e "drop database  westos;"           ##删除westos库
[[email protected] ~]# mysql -uroot -pwestos -e "create database  westos;"         ##创建westos库

[[email protected] ~]# mysql  -u root -pwestos   westos  <  /mnt/linux.sql         ##恢复linux表格

linux学习之mariadb

[[email protected] ~]# mysql -uroot -pwestos -e "show tables from westos;"         ##恢复完成
+------------------+
| Tables_in_westos |
+------------------+
| linux            |
+------------------+

7.用户授权

MariaDB [(none)]> create user [email protected] identified by 'lee';##建立用户lee,此用户只能通过本机登录

linux学习之mariadb

MariaDB [(none)]> create user [email protected]'%' identified by 'lee';      ##建立用户lee,从用户可以通过网络登录linux学习之mariadb

[[email protected] ~]# mysql -ulee -plee

linux学习之mariadb

[[email protected] ~]# mysql -ulee -plee -h localhost                ##本地登录

linux学习之mariadb

[[email protected] ~]# mysql -ulee -plee -h 172.25.254.234           ##配置文件写入跳过网络则网络无法登录

ERROR 2003 (HY000): Can't connect to MySQL server on '172.25.254.234' (111)


[[email protected] ~]# vim /etc/my.cnf                     ##跳过网络改为0(否)
10 skip-networking=0
[[email protected] ~]# systemctl restart mariadb           ##重启服务
[[email protected] ~]# mysql -ulee -plee -h 172.25.254.234 ##通过网络登录
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

linux学习之mariadb

MariaDB [(none)]> grant insert,update,delete,select on westos.* to [email protected];##用户授权

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show grants for [email protected];##查看用户授权
+------------------------------------------------------------------------------------------------------------+
| Grants for [email protected]                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'lee'@'localhost' IDENTIFIED BY PASSWORD '*9BB439A3A652A9DAD3718215F77A7AA06108A267' |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `westos`.* TO 'lee'@'localhost'                                    |

+------------------------------------------------------------------------------------------------------------+

linux学习之mariadb

MariaDB [(none)]> show grants for [email protected]'%';
+----------------------------------------------------------------------------------------------------+
| Grants for [email protected]%                                                                                   |
+----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'lee'@'%' IDENTIFIED BY PASSWORD '*9BB439A3A652A9DAD3718215F77A7AA06108A267' |
| GRANT SELECT ON `westos`.* TO 'lee'@'%'                                                            |
+----------------------------------------------------------------------------------------------------+

2 rows in set (0.00 sec)

linux学习之mariadb


MariaDB [(none)]> quit
Bye
MariaDB [(none)]> revoke delete on westos.linux from [email protected];##去掉用户授权权力
MariaDB [(none)]> drop user [email protected]'%';                               ##删除用户

Query OK, 0 rows affected (0.00 sec)

linux学习之mariadb


8.超级用户忘记密码
[[email protected] ~]# mysqladmin  -uroot -pwestos password lee         ##修改密码
[[email protected] ~]# mysql -uroot -pwestos                            ##密码错误无法登录

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

linux学习之mariadb

[[email protected] ~]# systemctl stop mariadb                          ##关闭服务
[[email protected] ~]# mysqld_safe  --skip-grant-tables &              ##开启mysql接口并忽略授权表
[1] 5074
[[email protected] ~]# 170515 07:55:02 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
170515 07:55:02 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql ##回车

[[email protected] ~]# 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)]> update mysql.user set Password=password('westos') where User='root';##更改超级用户密码信息
Query OK, 1 row affected (0.00 sec)
Rows matched: 3  Changed: 1  Warnings: 0

MariaDB [(none)]> quit

Bye

linux学习之mariadb

[[email protected] ~]# killall -9 mysqld_safe                       ##结束进程
[1]+  Killed                  mysqld_safe --skip-grant-tables
[[email protected] ~]# ps aux | grep mysql                          ##过滤mysql所有进程
mysql     5229  0.0  9.6 859068 98172 pts/1    Sl   07:55   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      5528  0.0  0.0 112640   940 pts/1    R+   08:05   0:00 grep --color=auto mysql
[[email protected] ~]# kill -9 5229                                  ##结束进程
[[email protected] ~]# ps aux | grep mysql
root      5543  0.0  0.0 112640   936 pts/1    R+   08:06   0:00 grep --color=auto mysql
[[email protected] ~]# systemctl start mariadb                      ##开启服务
[[email protected] ~]# mysql -uroot -pwestos                        ##修改后的密码登录
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)]> quit

Bye

linux学习之mariadb

9.数据库的网页管理工具

[[email protected] ~]# yum install httpd php php-mysql.x86_64 -y##安装相关服务,php网页工具,php-mysql连接数据库与php

linux学习之mariadb

[[email protected] ~]# systemctl start httpd
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
[[email protected] ~]# systemctl disable firewalld
rm '/etc/systemd/system/basic.target.wants/firewalld.service'
rm '/etc/systemd/system/

dbus-org.fedoraproject.FirewallD1.service'

linux学习之mariadb

[[email protected] mnt]# tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 -C /var/www/html/ ##安装
[[email protected] mnt]# cd /var/www/html/
[[email protected] html]# ls
phpMyAdmin-3.4.0-all-languages

[[email protected] html]# mv phpMyAdmin-3.4.0-all-languages/ mysqladmin##重命名

linux学习之mariadb

[[email protected] html]# cd mysqladmin/
[[email protected] mysqladmin]# cp -p config.sample.inc.php   config.inc.php##复制配置文件模版
[[email protected] mysqladmin]# vim config.inc.php

 17 $cfg['blowfish_secret'] = 'mysql'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

linux学习之mariadb

[[email protected] mysqladmin]# systemctl restart httpd.service
测试

访问http://172.25.254.234/mysqladmin0

linux学习之mariadb


linux学习之mariadb


linux学习之mariadb

linux学习之mariadb

linux学习之mariadblinux学习之mariadb

相关文章: