RH134-第二十节-Mariadb数据库

安装数据库

yum install mariadb-server.x86_64 -y
systemctl start mariadb
systemctl enable mariadb
vim /etc/my.cnf
skip-networking=1
mysql_secure_installation
Enter

进入数据库

mysql -uroot -p ##登陆数据库
show databases; ##显示所有数据库
mysql -uroot -phubo -e ”show databases;“ ##直接查看所有数据库
use westos; ##使用westos数据库
show tables; ##显示表格
select * from linux; ##从linux表格里获取所有信息


[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
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 |
| linux |
| mysql |
| performance_schema |
+——————–+
4 rows in set (0.00 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]> 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)

MariaDB [mysql]> select * from user

创建数据库

create database westos;
use westos;
careat table linux ( ## (表示换行
name varchar(50) not null, ##表示not null不能为空
age varchar(50) not null,
school varchar(50) not null,
sex varchar(20) ); ## 没有not null 表示录入的时候这一项可以为空
insert into linux values (‘lee’,’25’,’xupt’,’man’);
select * from linux;


[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 14
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 linux;
MariaDB [linux]> show tables ;
Empty set (0.00 sec)

MariaDB [linux]> create table mn (
-> age varchar(50),
-> sex varchar(50),
-> school varchar(20));
Query OK, 0 rows affected (0.36 sec)

MariaDB [linux]> show tables;
+—————–+
| Tables_in_linux |
+—————–+
| mn |
+—————–+

MariaDB [linux]> insert into mn (‘20’,’man’,’XUPT’); ##少了一个values报错
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ”20’,’man’,’XUPT’)’ at line 1

MariaDB [linux]> insert into mn values (‘20’,’man’,’XUPT’); ##重新导入数据
Query OK, 1 row affected (0.34 sec)

MariaDB [linux]> select * from mn;
+——+——+——–+
| age | sex | school |
+——+——+——–+
| 20 | man | XUPT |
+——+——+——–+
1 row in set (0.00 sec)

MariaDB [linux]>

修改数据库相关

alter table mn rename info; ##重命名表格mn为info
alter table mn add tele varchar(50) after sex; ##在表格里面添加tele信息并排在sex后面
update info set tele=’12345678’; ##更新tele里面的信息为123456(所有)
update info set tele=’1234151’ where username=’sarah’; ##更新sarah的tele信息为1234151


MariaDB [linux]> show tables;
+—————–+
| Tables_in_linux |
+—————–+
| mn |
+—————–+
1 row in set (0.00 sec)

MariaDB [linux]> alter table mn rename info;
Query OK, 0 rows affected (0.34 sec)

MariaDB [linux]> show tables;
+—————–+
| Tables_in_linux |
+—————–+
| info |
+—————–+
1 row in set (0.00 sec)

MariaDB [linux]> select * from info;
+——+——+——–+
| age | sex | school |
+——+——+——–+
| 20 | man | XUPT |
+——+——+——–+
1 row in set (0.00 sec)

MariaDB [linux]> alter table info add tele varchar(50) after sex;
Query OK, 1 row affected (0.39 sec)
Records: 1 Duplicates: 0 Warnings: 0

MariaDB [linux]> select * from info;
+——+——+——+——–+
| age | sex | tele | school |
+——+——+——+——–+
| 20 | man | NULL | XUPT |
+——+——+——+——–+
1 row in set (0.00 sec)

MariaDB [linux]> update info set tele=’12345678’;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0

MariaDB [linux]> select * from info;
+——+——+———-+——–+
| age | sex | tele | school |
+——+——+———-+——–+
| 20 | man | 12345678 | XUPT |
+——+——+———-+——–+
1 row in set (0.00 sec)

MariaDB [linux]>

备份数据库相关

mysqldump -uroot -phubo –all-database >/mnt/database.qsl ##备份数据库所有的数据到指定目录的指定文件
mysqldump -uroot -phubo linux >/mnt/linux.sql ##备份linux数据库到指定目录指定文件
mysqldump -uroot -phubo –no-databases >/mnt/database.qsl ##只备份数据库的表格,不备份数据
mysql -uroot -phubo -e ‘create database linux;’ ##新建数据库linux
mysql -uroot -phubo linux < /mnt/linux.sql ##将备份的导入到新建的linux数据库里


[root@localhost ~]# mysqldump -uroot -phubo –all-database >/mnt/database.qsl ##备份数据库所有的数据到指定目录的指定文件
[root@localhost ~]# ls /mnt/
database.qsl linux.sql
[root@localhost ~]# vim /mnt/database.qsl
[root@localhost ~]# mysqldump -uroot -phubo linux >/mnt/linux.sql
[root@localhost ~]# ls /mnt/
database.qsl linux.sql
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
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)]> drop linux;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘linux’ at line 1
MariaDB [(none)]> drop database linux;
Query OK, 1 row affected (0.04 sec)

MariaDB [(none)]> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
+——————–+
3 rows in set (0.00 sec)

MariaDB [(none)]> create database linux; ##这一步可以直接在shell里完成(mysqldump -uroot -phubo -e ’ create database linux;’)
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| linux |
| mysql |
| performance_schema |
+——————–+
4 rows in set (0.00 sec)

MariaDB [(none)]> Bye
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
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 linux;
Database changed
MariaDB [linux]> show tables;
Empty set (0.00 sec)

MariaDB [linux]> Bye
[root@localhost ~]# mysql -uroot -phubo linux < /mnt/linux.sql
[root@localhost ~]# 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)]> use linux;
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 [linux]> show tables;
+—————–+
| Tables_in_linux |
+—————–+
| info |
+—————–+
1 row in set (0.00 sec)

MariaDB [linux]> select * from info;
+——+——+———-+——–+
| age | sex | tele | school |
+——+——+———-+——–+
| 20 | man | 12345678 | XUPT |
+——+——+———-+——–+
1 row in set (0.00 sec)

MariaDB [linux]>

删除数据库相关

drop database linux; ##删除linux的数据库(包含里面的tables)
drop table info ; ##删除info表格
delate from info where age=’20’; ##删除info表格里age=20的数据
delate from info where age=’20’ and name=’peter’ ##删除info表格里age=20和name=peter的数据


[root@localhost ~]# mysql -uroot -phubo
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)]> use linux;
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 [linux]> select * from info;
+——+——+———-+——–+
| age | sex | tele | school |
+——+——+———-+——–+
| 20 | man | 12345678 | XUPT |
+——+——+———-+——–+
1 row in set (0.00 sec)

MariaDB [linux]> alter table info add name varchar(50);
Query OK, 1 row affected (0.45 sec)
Records: 1 Duplicates: 0 Warnings: 0

MariaDB [linux]> select * from info;
+——+——+———-+——–+——+
| age | sex | tele | school | name |
+——+——+———-+——–+——+
| 20 | man | 12345678 | XUPT | NULL |
+——+——+———-+——–+——+
1 row in set (0.00 sec)

MariaDB [linux]> insert into info values (”,”,”,”,’peter’);
Query OK, 1 row affected (0.34 sec)

MariaDB [linux]> select * from info;
+——+——+———-+——–+——-+
| age | sex | tele | school | name |
+——+——+———-+——–+——-+
| 20 | man | 12345678 | XUPT | NULL |
| | | | | peter |
+——+——+———-+——–+——-+
2 rows in set (0.00 sec)

MariaDB [linux]> delete from info where name=’peter’;
Query OK, 1 row affected (0.04 sec)

MariaDB [linux]> select * from info;
+——+——+———-+——–+——+
| age | sex | tele | school | name |
+——+——+———-+——–+——+
| 20 | man | 12345678 | XUPT | NULL |
+——+——+———-+——–+——+
1 row in set (0.00 sec)

MariaDB [linux]> insert into info values (‘23’,’men’,’147258233’,’Normal University’,’peter’);
Query OK, 1 row affected (0.34 sec)

MariaDB [linux]> select * from info;
+——+——+———–+——————-+——-+
| age | sex | tele | school | name |
+——+——+———–+——————-+——-+
| 20 | man | 12345678 | XUPT | NULL |
| 23 | men | 147258233 | Normal University | peter |
+——+——+———–+——————-+——-+
2 rows in set (0.00 sec)

MariaDB [linux]> delete from info where age=’20’;
Query OK, 1 row affected (0.34 sec)

MariaDB [linux]> select * from info;
+——+——+———–+——————-+——-+
| age | sex | tele | school | name |
+——+——+———–+——————-+——-+
| 23 | men | 147258233 | Normal University | peter |
+——+——+———–+——————-+——-+

MariaDB [linux]> drop table info;
Query OK, 0 rows affected (0.34 sec)

MariaDB [linux]> show tables;
Empty set (0.00 sec)

MariaDB [linux]> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| linux |
| mysql |
| performance_schema |
+——————–+
4 rows in set (0.00 sec)

MariaDB [linux]> drop database linux;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
+——————–+
3 rows in set (0.00 sec)

MariaDB [(none)]>

重置mysql root密码

systemctl stop mariadb ##停止数据库
mysqld_safe –skip-grant-tables & ##开启数据库不启动授权表
kill -9 进程号 ##结束数据库进程
mysql ->use mysql->update user set Password=password(‘redhat’) where User=’root’;[update user set Password=’redhat’ where User=’root’;这种设置是为加密的密码,密码会裸露出来,不安全,故采用第一种] ##进入数据路的操作
systemctl restart maiadb ##重启数据库


ot@localhost ~]# systemctl stop mariadb
[root@localhost ~]# ps -aux |grep mysql
root 3905 0.0 0.0 112640 976 pts/1 S+ 02:51 0:00 grep –color=auto mysql
[root@localhost ~]# mysqld_safe –skip-grant-tables &
[1] 3906
[root@localhost ~]# 170812 02:51:36 mysqld_safe Logging to ‘/var/log/mariadb/mariadb.log’.
170812 02:51:36 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql ##这里会停住,按一下enter,让其继续运行

[root@localhost ~]# 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)]> 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]> update user set Password=password(‘westos’) where User=’root’;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0

MariaDB [mysql]> Bye

[root@localhost ~]# ps -aux |grep mysql
root 3906 0.0 0.1 113256 1644 pts/1 S 02:51 0:00 /bin/sh /usr/bin/mysqld_safe –skip-grant-tables
mysql 4235 1.7 8.7 791148 88712 pts/1 Sl 02:54 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 4257 0.0 0.0 112640 980 pts/1 R+ 02:54 0:00 grep –color=auto mysql
[root@localhost ~]# kill -9 3906 ##关闭mysald_safe的进程
[root@localhost ~]# kill -9 4235 ##关闭mysald_safe的进程,先关闭第一个,再关闭第二个
[1]+ Killed mysqld_safe –skip-grant-tables
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# mysql
ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO)
[root@localhost ~]# mysql -uroot -pwestos
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
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 |
+——————–+
3 rows in set (0.00 sec)

MariaDB [(none)]>

创建用户和用户授权

CREATE USER lee@localhost identified by ‘lee’; ##创建本地用户lee并且密码为lee(by后面的为密码)
CREATE USER lee@’%’ identified by ‘lee’; ##创建本地和远程都可以登陆用户lee,密码lee
用户授权
GRANT INSERT,UPDATE,DELETE,SELECT,DROP on linux.* to lee@localhost; ##授予本地用户lee@localhost:导入….删除等权限在linux数据库上
重载授权表
FLUSH PRIVILEGES;
查看用户授权表
SHOW GRANTS FOR lee@localhost
撤销用户权限
REVOKE INSERT,UPDATE,DELETE,SELECT,DROP on westos.* from lee@localhost;
删除用户
DROP USER lee@localhost;


MariaDB [(none)]> create user lee@’%’ identified by ‘lee’;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant insert,update,delate,drop,select on linux.* to lee@’%’;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘delate,drop,select on linux.* to lee@’%” at line 1
(delete 打错了,报错)
MariaDB [(none)]> grant insert,update,delete,drop,select on linux.* to lee@’%’;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show grants for lee@’%’;
+—————————————————————————————————-+
| Grants for lee@% |
+—————————————————————————————————-+
| GRANT USAGE ON . TO ‘lee’@’%’ IDENTIFIED BY PASSWORD ‘*9BB439A3A652A9DAD3718215F77A7AA06108A267’ |
| GRANT SELECT, INSERT, UPDATE, DELETE, DROP ON linux.* TO ‘lee’@’%’ |
+—————————————————————————————————-+
2 rows in set (0.00 sec)

MariaDB [(none)]> revoke UPDATE, DELETE on linux.* from lee@’%’;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show grants for lee@’%’;
+—————————————————————————————————-+
| Grants for lee@% |
+—————————————————————————————————-+
| GRANT USAGE ON . TO ‘lee’@’%’ IDENTIFIED BY PASSWORD ‘*9BB439A3A652A9DAD3718215F77A7AA06108A267’ |
| GRANT SELECT, INSERT, DROP ON linux.* TO ‘lee’@’%’ |
+—————————————————————————————————-+
2 rows in set (0.00 sec)

MariaDB [(none)]> drop user lee@’%’;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>

mysql图形化管理工具phpmysql

yum install mariadb httpd php php-mysql -y ##安装必要的软件
get /var/www/html/phpMyAdmin-3.4.0-all-languages.tar.bz2 ##下phpmyadmin软件包
tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 ##解包
phpMyAdmin-3.4.0-all-languages ##解压得到的文件夹
mv phpMyAdmin-3.4.0-all-languages mariadbadmin ##重命名目录
less Documentation.txt ##查看安装说明
cp config.sample.inc.php config.inc.php ##按照说明重命名这个文件
vim config.inc.php
$cfg[‘blowfish_secret’]= ‘ba17clec07d65003’;
systemctl restart httpd
systemctl stop firewalld

测试
httpd://localhost/mysqladmin


[root@foundation23 Desktop]# tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 -C /var/www/html/
[root@foundation23 Desktop]# cd /var/www/html/
[root@foundation23 html]# ls
index.html ks1 phpmyadmin redhat7
ks ks.cfg phpMyAdmin-3.4.0-all-languages redhat72
[root@foundation23 html]# mv phpMyAdmin-3.4.0-all-languages/ mysqladmin
[root@foundation23 html]# ls
index.html ks ks1 ks.cfg mysqladmin redhat7 redhat72
[root@foundation23 html]# cd mysqladmin/
[root@foundation23 mysqladmin]# cp config.sample.inc.php config.inc.php
[root@foundation23 mysqladmin]# less Documentation.txt
[root@foundation23 mysqladmin]# vim config.inc.php
[root@foundation23 mysqladmin]# yum install php php-mysql -y
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
: manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
redhat72 | 4.1 kB 00:00
software | 2.9 kB 00:00
Package php-5.4.16-36.el7_1.x86_64 already installed and latest version
Package php-mysql-5.4.16-36.el7_1.x86_64 already installed and latest version
Nothing to do
[root@foundation23 mysqladmin]# systemctl restart httpd.service
[root@foundation23 mysqladmin]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)

Aug 12 15:35:55 foundation23.ilt.example.com systemd[1]: Stopped firewalld - …
Hint: Some lines were ellipsized, use -l to show in full.
[root@foundation23 mysqladmin]# systemctl restart mariadb.service
这里写图片描述
这里写图片描述
这里写图片描述

发布了66 篇原创文章 · 获赞 25 · 访问量 4万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章