Linux课程第十九天学习笔记
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux课程第十九天学习笔记相关的知识,希望对你有一定的参考价值。
####################DNS(接上一篇的内容)####################
=====主备DNS=====
dns-server 172.25.254.115
dns-slave 172.25.254.215
yum install bind -y
[[email protected] ~]# firewall-cmd --permanent --add-service=dns
success
[[email protected] ~]# firewall-cmd --reload
success
vim /etc/named.conf
-------------------------------------------
11 listen-on port 53 { any; };
17 allow-query { any; };
32 dnssec-validation no;
-------------------------------------------
vim /etc/named.rfc1912.zones
-------------------------------------------
master:
zone "westos.com" IN {
type master;
file "westos.com.zone";
allow-update { none; };
allow-transfer { 172.25.254.215; };
};
slave:
zone "westos.com" IN {
type slave;
masters { 172.25.254.115; };
file "slaves/westos.com.zone"; ##文件位置
allow-update { none; };
};
-------------------------------------------
[[email protected] named]# cp -p named.localhost westos.com.zone
[[email protected] named]# vim /var/named/westos.com.zone
-------------------------------------------
$TTL 1D
@ IN SOA dns.westos.com. root.westos.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.westos.com.
dns A 172.25.254.100
www A 172.25.254.100
-------------------------------------------
[[email protected] named]# systemctl restart named
[[email protected] named]# systemctl restart named
[[email protected] named]# ls slaves/
westos.com.zone
[[email protected] named]# vim /etc/resolv.conf
-------------------------------------------
nameserver 172.25.254.215
-------------------------------------------
[[email protected] named]# dig www.westos.com
=====同步=====
[[email protected] named]# vim /etc/resolv.conf
-------------------------------------------
nameserver 172.25.254.115
-------------------------------------------
[[email protected] named]# vim /etc/named.rfc1912.zones
-------------------------------------------
also-notify { 172.25.254.215; }; ##允许谁来同步
-------------------------------------------
[[email protected] named]# vim /var/named/westos.com.zone
-------------------------------------------
3 2016112601 ; serial
##只能识别10位数字,"2016112601"刚好10位。超过10位,比如11位,就不识别
##本次数字要大于之前的数字,才能同步
10 www A 172.25.254.101
-------------------------------------------
[[email protected] ~]# firewall-cmd --permanent --add-service=dns
success
[[email protected] ~]# firewall-cmd --reload
success
[[email protected] named]# systemctl restart named
[[email protected] named]# systemctl restart named
[[email protected] named]# vim slaves/westos.com.zone
##全是乱码
[[email protected] named]# dig www.westos.com
[[email protected] named]# dig www.westos.com
=====更新=====
[[email protected] named]# vim /etc/named.rfc1912.zones
-------------------------------------------
zone "westos.com" IN {
type master;
file "westos.com.zone";
allow-update { 172.25.254.215; };
};
-------------------------------------------
[[email protected] named]# systemctl restart named
--备份--
[[email protected] named]# cp -p westos.com.zone /mnt
##备份和恢复->复制不要移动
--selinux改为警告模式---
[[email protected] named]# getenforce
Enforcing
[[email protected] named]# setenforce 0
[[email protected] named]# getenforce
Permissive
--修改目录权限--
[[email protected] named]# ll -d
drwxr-x---. 5 root named 4096 Nov 25 21:26 .
[[email protected] named]# pwd
/var/named
[[email protected] named]# chmod 770 /var/named/
[[email protected] named]# ll -d
drwxrwx---. 5 root named 4096 Nov 25 21:26 .
--测试--
[[email protected] named]# nsupdate
> server 172.25.254.115
> update delete www.westos.com
> send
[[email protected] named]# dig www.westos.com
NXDOMAIN
slave:
> server 172.25.254.115
> update add www.westos.com 86400 A 172.25.254.111
> send
> quit
[[email protected] named]# dig www.westos.com
www.westos.com. 86400 IN A 172.25.254.111
--重启服务后westos.com.zone文件才会更新--
[[email protected] named]# cat westos.com.zone
$TTL 1D
@ IN SOA dns.westos.com. root.westos.com. (
2016112703 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.westos.com.
dns A 172.25.254.100
www A 172.25.254.105
[[email protected] named]# systemctl restart named
[[email protected] named]# cat westos.com.zone
$ORIGIN .
$TTL 86400 ; 1 day
westos.com IN SOA dns.westos.com. root.westos.com. (
2016112705 ; serial
86400 ; refresh (1 day)
3600 ; retry (1 hour)
604800 ; expire (1 week)
10800 ; minimum (3 hours)
)
NS dns.westos.com.
$ORIGIN westos.com.
dns A 172.25.254.100
www A 172.25.254.111
--恢复--
[[email protected] named]# rm -fr westos.com.zone.jnl
[[email protected] named]# cp /mnt/westos.com.zone .
cp: overwrite ‘./westos.com.zone’? yes
##备份和恢复->复制不要移动
[[email protected] named]# cat westos.com.zone
$TTL 1D
@ IN SOA dns.westos.com. root.westos.com. (
2016112703 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.westos.com.
dns A 172.25.254.100
www A 172.25.254.105
[[email protected] named]# systemctl restart named
[[email protected] named]# dig www.westos.com
www.westos.com. 86400 IN A 172.25.254.105
--由于后面要做花生壳实验,关闭selinux--
[[email protected] named]# vim /etc/sysconfig/selinux
-------------------------------------------
SELINUX=disabled
-------------------------------------------
[[email protected] named]# systemctl enable named
ln -s ‘/usr/lib/systemd/system/named.service‘ ‘/etc/systemd/system/multi-user.target.wants/named.service‘
[[email protected] named]# reboot
=====加密更新=====
[[email protected] named]# cat /etc/rndc.key
key "rndc-key" {
algorithm hmac-md5;
secret "eqrr+4+aYsyqezh6/bVzEA==";
};
##加密算法:hmac-md5
[[email protected] named]# cd /mnt
[[email protected] mnt]# dnssec-keygen --help
-------------------------------------------
-a <algorithm>:
RSA | RSAMD5 | DSA | RSASHA1 | NSEC3RSASHA1 | NSEC3DSA |
RSASHA256 | RSASHA512 | ECCGOST |
ECDSAP256SHA256 | ECDSAP384SHA384 |
DH | HMAC-MD5 | HMAC-SHA1 | HMAC-SHA224 | HMAC-SHA256 |
HMAC-SHA384 | HMAC-SHA512
(default: RSASHA1, or NSEC3RSASHA1 if using -3)
-b <key size in bits>:
RSAMD5: [512..4096]
RSASHA1: [512..4096]
NSEC3RSASHA1: [512..4096]
RSASHA256: [512..4096]
RSASHA512: [1024..4096]
DH: [128..4096]
DSA: [512..1024] and divisible by 64
NSEC3DSA: [512..1024] and divisible by 64
ECCGOST: ignored
ECDSAP256SHA256: ignored
ECDSAP384SHA384: ignored
HMAC-MD5: [1..512]
HMAC-SHA1: [1..160]
HMAC-SHA224: [1..224]
HMAC-SHA256: [1..256]
HMAC-SHA384: [1..384]
HMAC-SHA512: [1..512]
(if using the default algorithm, key size
defaults to 2048 for KSK, or 1024 for all others)
-n <nametype>: ZONE | HOST | ENTITY | USER | OTHER
(DNSKEY generation defaults to ZONE)
-------------------------------------------
--生成加密key--
[[email protected] mnt]# dnssec-keygen -a HMAC-MD5 -b 128 -n HOST westos
Kwestos.+157+11066
[[email protected] mnt]# ls
Kwestos.+157+11066.key Kwestos.+157+11066.private westos.com.zone
[[email protected] mnt]# cat Kwestos.+157+11066.key
westos. IN KEY 512 3 157 0HsT4u2VY9D0g+X2gPOUag==
[[email protected] mnt]# cat Kwestos.+157+11066.private
Private-key-format: v1.3
Algorithm: 157 (HMAC_MD5)
Key: 0HsT4u2VY9D0g+X2gPOUag==
Bits: AAA=
Created: 20161126043640
Publish: 20161126043640
Activate: 20161126043640
##MD5加密,公钥和私钥加密字符一样
--创建westos.key--
[[email protected] mnt]# cp -p /etc/rndc.key /etc/westos.key
[[email protected] mnt]# vim /etc/westos.key
-------------------------------------------
key "westos" {
algorithm hmac-md5;
secret "0HsT4u2VY9D0g+X2gPOUag==";
};
:wq
-------------------------------------------
--在主文件包含westos.key--
[[email protected] mnt]# vim /etc/named.conf
-------------------------------------------
42 };
43 include "/etc/westos.key";
44 logging {
:wq
-------------------------------------------
##也可以把文件/etc/westos.key中的内容复制过来,但是太乱
--只允许key匹配的远端更新--
[[email protected] mnt]# vim /etc/named.rfc1912.zones
-------------------------------------------
25 zone "westos.com" IN {
26 type master;
27 file "westos.com.zone";
28 allow-update { key westos; };
29 };
:wq
-------------------------------------------
[[email protected] mnt]# systemctl restart named
[[email protected] mnt]# cat /var/named/westos.com.zone
$TTL 1D
@ IN SOA dns.westos.com. root.westos.com. (
2016112703 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.westos.com.
dns A 172.25.254.100
www A 172.25.254.105
##注意:一定要还原文件,2次更新的方式不一样,会有冲突
[[email protected] mnt]# dig www.westos.com
www.westos.com. 86400 IN A 172.25.254.105
[[email protected] named]# nsupdate
> server 172.25.254.115
> update delete www.westos.com
> send
update failed: REFUSED
> quit
--发送key文件--
[[email protected] mnt]# scp Kwestos.+157+11066.* [email protected]:/mnt
The authenticity of host ‘172.25.254.215 (172.25.254.215)‘ can‘t be established.
ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘172.25.254.215‘ (ECDSA) to the list of known hosts.
[email protected]‘s password:
Kwestos.+157+11066.key 100% 50 0.1KB/s 00:00
Kwestos.+157+11066.private 100% 165 0.2KB/s 00:00
--加密更新--
[[email protected] named]# cd /mnt
[[email protected] mnt]# ls
Kwestos.+157+11066.key Kwestos.+157+11066.private
[[email protected] mnt]# nsupdate --help
nsupdate: illegal option -- -
nsupdate: invalid argument --
usage: nsupdate [-dD] [-L level] [-l][-g | -o | -y keyname:secret | -k keyfile] [-v] [filename]
[[email protected] mnt]# nsupdate -k Kwestos.+157+11066.private
> server 172.25.254.115
> update delete www.westos.com
> send
> quit
--测试--
[[email protected] mnt]# dig www.westos.com
;www.westos.com. IN A
[[email protected] mnt]# systemctl restart named
[[email protected] mnt]# cd /var/named
[[email protected] named]# cat westos.com.zone
$ORIGIN .
$TTL 86400 ; 1 day
westos.com IN SOA dns.westos.com. root.westos.com. (
2016112704 ; serial
86400 ; refresh (1 day)
3600 ; retry (1 hour)
604800 ; expire (1 week)
10800 ; minimum (3 hours)
)
NS dns.westos.com.
$ORIGIN westos.com.
dns A 172.25.254.100
--恢复--
[[email protected] named]# rm -fr westos.com.zone.jnl
[[email protected] named]# cp /mnt/westos.com.zone .
cp: overwrite ‘./westos.com.zone’? yes
##备份和恢复->复制不要移动
[[email protected] named]# systemctl restart named
=====花生壳=====
[[email protected] mnt]# hostnamectl set-hostname music.westos.com
[[email protected] mnt]# exit
[[email protected] Desktop]$ ssh [email protected]
[[email protected] ~]# hostname
music.westos.com
[[email protected] Desktop]$ rht-vmctl view desktop
desktop:
网卡配置成dhcp
重启network服务 --> 卡住,因为没有dhcp服务器分配地址
ctrl+c
[[email protected] named]# yum install dhcp -y
[[email protected] named]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
cp: overwrite ‘/etc/dhcp/dhcpd.conf’? yes
[[email protected] named]# vim /etc/dhcp/dhcpd.conf
-------------------------------------------
7 option domain-name "westos.com";
8 option domain-name-servers 172.25.254.115;
14 ddns-update-style interim; ##man 5 dhcpd.conf /dns查找,/ddns-update-style再查找
30 subnet 172.25.254.0 netmask 255.255.255.0 {
31 range 172.25.254.240 172.25.254.244;
32 option routers 172.25.254.115;
33 }
-------------------------------------------
[[email protected] named]# man 5 dhcpd.conf
-------------------------------------------
/dns
key DHCP_UPDATER {
algorithm hmac-md5;
secret pRP5FapFoJ95JEL06sv4PQ==;
};
zone "example.org" {
type master;
file "example.org.db";
allow-update { key DHCP_UPDATER; };
};
zone "17.10.10.in-addr.arpa" {
type master;
file "10.10.17.db";
allow-update { key DHCP_UPDATER; };
};
-------------------------------------------
[[email protected] named]# cat /etc/westos.key
-------------------------------------------
key "westos" {
algorithm hmac-md5;
secret "0HsT4u2VY9D0g+X2gPOUag==";
};
-------------------------------------------
[[email protected] named]# vim /etc/dhcp/dhcpd.conf
-------------------------------------------
34 key westos {
35 algorithm hmac-md5;
36 secret 0HsT4u2VY9D0g+X2gPOUag==;
37 };
38 zone westos.com. {
39 primary 127.0.0.1;
40 key westos;
41 }
------------------------------------------- ##man 5 dhcpd.conf /dns查找
[[email protected] named]# vim westos.com.zone
-------------------------------------------
删除
www A 172.25.254.105
-------------------------------------------
测试时要删除music.westos.com的A记录
[[email protected] named]# systemctl restart named
[[email protected] named]# systemctl restart dhcp
desktop:
重启network服务
dig music.westos.com
music.westos.com. 86400 IN A 172.25.254.240
[[email protected] named]# vim /etc/dhcp/dhcpd.conf
-------------------------------------------
31 range 172.25.254.241 172.25.254.244;
-------------------------------------------
[[email protected] named]# systemctl restart named
[[email protected] named]# systemctl restart dhcp
desktop:
重启network服务
dig music.westos.com
music.westos.com. 86400 IN A 172.25.254.241
ping music.westos.com可以ping通
[[email protected] named]# cat westos.com.zone
$ORIGIN .
$TTL 86400 ; 1 day
westos.com IN SOA dns.westos.com. root.westos.com. (
2016112706 ; serial
86400 ; refresh (1 day)
3600 ; retry (1 hour)
604800 ; expire (1 week)
10800 ; minimum (3 hours)
)
NS dns.westos.com.
$ORIGIN westos.com.
dns A 172.25.254.100
$TTL 300 ; 5 minutes
music A 172.25.254.241
TXT "00cd0b026d97ef690997cd0e32d523d187"
##5分钟更新一次
#####################
##### 数据库 #####
#####################
####################1.mariadb安装####################
数据库可以理解为一个高级的Excel
mysql在企业里面比较流行
mysql被orcale收购以后,有闭源的风险,而且发布速度缓慢
linux系统集成的mariadb是mysql的分支,被视为mysql的替代品
[[email protected] ~]# hostnamectl set-hostname mariadb.example.com
[[email protected] ~]# reboot
等待重启
[[email protected] ~]# yum install mariadb-server -y
......
[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# systemctl enable mariadb
ln -s ‘/usr/lib/systemd/system/mariadb.service‘ ‘/etc/systemd/system/multi-user.target.wants/mariadb.service‘
[[email protected] ~]# netstat -antlpe |grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 27 77968 30901/mysqld
[[email protected] ~]# mysql -h 172.25.50.100
ERROR 1130 (HY000): Host ‘172.25.50.100‘ is not allowed to connect to this MariaDB server
[[email protected] ~]# vim /etc/my.cnf
--------------------------------------------------
10 skip-networking=1
:wq
--------------------------------------------------
[[email protected] ~]# systemctl restart mariadb
[[email protected] ~]# netstat -antlpe |grep mysql
[[email protected] ~]# mysql_secure_installation
##安装好mysql后,通过此命令对mysql进行设置
##创建密码"westos",其它一路回车
[[email protected] ~]# systemctl restart mariadb
####################2.数据库基本操作####################
[[email protected] ~]# mysql -uroot -pwestos ##不规范的登陆操作,在5.7版本以上会被拒绝
[[email protected] ~]# mysql -u root -p ##规范的登陆操作
Enter password: westos
[[email protected] ~]# mysql -uroot -pwestos ##从本机登陆mysql数据库
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.
注意:mysql的命令,使用大写字母是规范写法;并且所有命令都以";"结尾
MariaDB [(none)]> SHOW DATABASES; ##显示数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.01 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]> DESC user; ##显示user表的数据结构
+------------------------+-----------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+---------+-------+
| Host | char(60) ##表示最长60字节 | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Password | char(41) | NO | | | |
| Select_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Insert_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Update_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Delete_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Drop_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Reload_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Shutdown_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Process_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| File_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Grant_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| References_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Index_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Alter_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Show_db_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Super_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_tmp_table_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Lock_tables_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Execute_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Repl_slave_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Repl_client_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_view_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Show_view_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_routine_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Alter_routine_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_user_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Event_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Trigger_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_tablespace_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| ssl_type | enum(‘‘,‘ANY‘,‘X509‘,‘SPECIFIED‘) | NO | | | |
| ssl_cipher | blob | NO | | NULL | |
| x509_issuer | blob | NO | | NULL | |
| x509_subject | blob | NO | | NULL | |
| max_questions | int(11) unsigned | NO | | 0 | |
| max_updates | int(11) unsigned | NO | | 0 | |
| max_connections | int(11) unsigned | NO | | 0 | |
| max_user_connections | int(11) | NO | | 0 | |
| plugin | char(64) | NO | | | |
| authentication_string | text | NO | | NULL | |
+------------------------+-----------------------------------+------+-----+---------+-------+
42 rows in set (0.01 sec)
MariaDB [mysql]> SELECT * FROM user; ##列表太长,所以折行显示
+-----------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+
| Host | User | Password | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv | Trigger_priv | Create_tablespace_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections | plugin | authentication_string |
+-----------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+
| localhost | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | | |
| 127.0.0.1 | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | | |
| ::1 | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | | |
+-----------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+
3 rows in set (0.01 sec)
MariaDB [mysql]> select host,user,password from user; ##显示user表中的某几个字段
+-----------+------+-------------------------------------------+
| host | user | password |
+-----------+------+-------------------------------------------+
| localhost | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
| 127.0.0.1 | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
| ::1 | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
+-----------+------+-------------------------------------------+
3 rows in set (0.00 sec)
MariaDB [mysql]> CREATE DATABASE westos; ##创建westos数据库
Query OK, 1 row affected (0.00 sec)
MariaDB [mysql]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| westos |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [mysql]> USE westos;
Database changed
MariaDB [westos]> SHOW TABLES;
Empty set (0.00 sec)
MariaDB [westos]> CREATE TABLE UTAB ( username varchar(10) not null, passwd varchar(50) not null, age varchar(4) ); ##创建username,passwd,age字段
Query OK, 0 rows affected (0.11 sec)
MariaDB [westos]> SHOW TABLES;
+------------------+
| Tables_in_westos |
+------------------+
| UTAB |
+------------------+
1 row in set (0.00 sec)
MariaDB [westos]> DESC UTAB; ##显示UTAB表的数据结构
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(10) | NO | | NULL | |
| passwd | varchar(50) | NO | | NULL | |
| age | varchar(4) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
MariaDB [westos]> INSERT INTO UTAB VALUES (‘lee‘,‘123‘,‘18‘); ##在表中插入一行对应所有字段的值
Query OK, 1 row affected (0.10 sec)
MariaDB [westos]> INSERT INTO UTAB VALUES (‘westos‘,‘123‘,‘‘);
Query OK, 1 row affected (0.09 sec)
MariaDB [westos]> SELECT * FROM UTAB;
+----------+--------+------+
| username | passwd | age |
+----------+--------+------+
| lee | 123 | 18 |
| westos | 123 | |
+----------+--------+------+
2 rows in set (0.00 sec)
##每一列都是一个字段;尽量不要更改字段名,否则可能导致列信息的丢失
MariaDB [westos]> ALTER TABLE UTAB ADD class varchar(8) AFTER passwd; ##在某字段后插入新的字段
Query OK, 2 rows affected (0.16 sec)
Records: 2 Duplicates: 0 Warnings: 0
##可以在某字段的后面插入字段,不能在某字段的前面插入字段
MariaDB [westos]> SELECT * FROM UTAB;
+----------+--------+-------+------+
| username | passwd | class | age |
+----------+--------+-------+------+
| lee | 123 | NULL | 18 |
| westos | 123 | NULL | |
+----------+--------+-------+------+
2 rows in set (0.00 sec)
MariaDB [westos]> UPDATE UTAB SET class=‘1‘; ##更新某字段所有用户的值
Query OK, 2 rows affected (0.51 sec)
Rows matched: 2 Changed: 2 Warnings: 0
MariaDB [westos]> SELECT * FROM UTAB;
+----------+--------+-------+------+
| username | passwd | class | age |
+----------+--------+-------+------+
| lee | 123 | 1 | 18 |
| westos | 123 | 1 | |
+----------+--------+-------+------+
2 rows in set (0.00 sec)
MariaDB [westos]> UPDATE UTAB SET class=‘2‘ WHERE username=‘westos‘; ##更新某用户某字段的值
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [westos]> SELECT * FROM UTAB;
+----------+--------+-------+------+
| username | passwd | class | age |
+----------+--------+-------+------+
| lee | 123 | 1 | 18 |
| westos | 123 | 2 | |
+----------+--------+-------+------+
2 rows in set (0.00 sec)
MariaDB [westos]> DELETE FROM UTAB WHERE username=‘westos‘; ##删除某用户的所有内容
Query OK, 1 row affected (0.32 sec)
MariaDB [westos]> SELECT * FROM UTAB;
+----------+--------+-------+------+
| username | passwd | class | age |
+----------+--------+-------+------+
| lee | 123 | 1 | 18 |
+----------+--------+-------+------+
1 row in set (0.00 sec)
MariaDB [westos]> ALTER TABLE UTAB DROP age; ##删除age字段
Query OK, 1 row affected (0.15 sec)
Records: 1 Duplicates: 0 Warnings: 0
MariaDB [westos]> SELECT * FROM UTAB;
+----------+--------+-------+
| username | passwd | class |
+----------+--------+-------+
| lee | 123 | 1 |
+----------+--------+-------+
1 row in set (0.00 sec)
MariaDB [westos]> DROP table UTAB; ##删除UTAB表
Query OK, 0 rows affected (0.10 sec)
MariaDB [westos]> SHOW TABLES;
Empty set (0.00 sec)
MariaDB [westos]> DROP DATABASE westos; ##删除westos数据库
Query OK, 0 rows affected (0.08 sec)
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> QUIT
Bye
本文出自 “施超Linux学习笔记” 博客,谢绝转载!
以上是关于Linux课程第十九天学习笔记的主要内容,如果未能解决你的问题,请参考以下文章