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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章