linux exercise19

来源:互联网 发布:云计算的商业模式 编辑:程序博客网 时间:2024/06/18 10:14
[root@mariadb ~]# yum install mariadb-server -y
[root@mariadb ~]# systemctl start mariadb       ####开启mariadb服务
[root@mariadb ~]# netstat -antlpe | grep mysql   ####查看mysql网络端口是开启的
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      27         75409      3232/mysqld         
[root@mariadb ~]# vim /etc/my.cnf   ####配置文件
 10 skip-networking=1           ####跳过网络接口
[root@mariadb ~]# systemctl restart mariadb    

[root@mariadb ~]# netstat -antlpe | grep mysql  

   [root@mariadb ~]# mysql_secure_installation   ####安全初始化
/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      ####设置密码
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]     ####删除匿名用户访问
 ... 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]    ####禁止超级用户远程访问
 ... 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]    ####删除测试用户
 - 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]    ####重新加载
 ... 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!
[root@mariadb ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@mariadb ~]# mysql -uroot -p  ####使用刚刚设置的密码进入
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 24
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)]>




2.查询
[root@mariadb ~]# mysql -uroot -p
MariaDB [(none)]> show databases;  ####显示数据库
MariaDB [(none)]> use mysql;         ####进入mysql数据库
MariaDB [mysql]> show tables;        ####显示mysql数据库的所有表格
MariaDB [mysql]> select * from user;    ####显示user表格的所有内容,*可以被替换为user表格的字段名
MariaDB [mysql]> desc user;        ####显示user表格的所有字段






3.建立
[root@mariadb ~]# mysql -uroot -p
MariaDB [(none)]> create database westos;    ####建立westos库
MariaDB [(none)]> use westos;            ####进入westos库
MariaDB [westos]> create table linux(        ####不加;号,创建linux表,并编辑出两个字段
    -> username varchar(15) not null,        ####username字段,最多可容纳15字符,并且不能为空
    -> password varchar(15) not null);        ####password字段
Query OK, 0 rows affected (0.07 sec)
MariaDB [westos]> insert into linux values ('user1','123');        ####向linux表插入数据,username字段的数据是user1,password字段的数据是123
MariaDB [westos]> insert into linux values ('user2',password('123'));    ####对password字段的数据加密





5.delete
delete from linux where username='user1';
drop table linux;
drop database westos;




4.

MariaDB [westos]> select * from linux;
+----------+-----------------+
| username | password        |
+----------+-----------------+
| user1    | 123             |
| user2    | *23AE809DDACAF9 |        ####由此看出user该行的password字段是加密的
+----------+-----------------+
2 rows in set (0.00 sec)

更新
MariaDB [westos]> select * from linux;
+----------+-----------------+
| username | password        |
+----------+-----------------+
| user1    | 123             |
| user2    | *23AE809DDACAF9 |
| user3    | 123             |
+----------+-----------------+
3 rows in set (0.00 sec)

(1)update linux set password=password('123') where username='user1';####更新user1的密码

MariaDB [westos]> select * from linux;
+----------+-----------------+
| username | password        |
+----------+-----------------+
| user1    | *23AE809DDACAF9 |
| user2    | *23AE809DDACAF9 |
| user3    | 123             |
+----------+-----------------+
3 rows in set (0.00 sec)







2)delete from linux where username='user1';        ####删除user1的信息

(3)alter table linux add date varchar(5);####添加date字段
Query OK, 2 rows affected (0.02 sec)               
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [westos]> select * from linux;            
+----------+-----------------+------+
| username | password        | date |
+----------+-----------------+------+
| user2    | *23AE809DDACAF9 | NULL |
| user3    | 123             | NULL |
+----------+-----------------+------+






(3)alter table linux add date varchar(5);####添加date字段
Query OK, 2 rows affected (0.02 sec)               
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [westos]> select * from linux;            
+----------+-----------------+------+
| username | password        | date |
+----------+-----------------+------+
| user2    | *23AE809DDACAF9 | NULL |
| user3    | 123             | NULL |
+----------+-----------------+------+
2 rows in set (0.00 sec)



(5)MariaDB [westos]> alter table linux drop age;        ####删除age字段

MariaDB [westos]> select * from linux;
+----------+-----------------+------+
| username | password        | date |
+----------+-----------------+------+
| user2    | *23AE809DDACAF9 | NULL |
| user3    | 123             | NULL |
+----------+-----------------+------+
2 rows in set (0.00 sec)

MariaDB [westos]>





5.删除
delete from linux where username='user3';    ####删除username为user3的信息
drop table linux;                ####删除表linux
drop database westos;                ####删除库westos





6.备份
mysqldump -u root -pwestos --all-databases    ####备份所有表的所有数据
mysqldump -u root -pwestos --all-databases --no-data     ####备份所有表,但不备份数据
mysqldump -u root -pwestos westos        ####备份westos库
mysqldump -u root -pwestos westos > /mnt/westos.sql    ####备份westos库并把数据保存到/mnt/westos.sq1
mysql -u root -pwestos -e "create databases westos"    ####建立westos库
mysql -u root -pwestos westos < /mnt/westos.sql        ####将westos.sql导入到westos库中
mysqldump -u root -pwestos westos linux > /mnt/linux.sql####备份westos库的linux表到/mnt/linux.sql中
mysql -u root -pwestos westos < /mnt/linux.sql        ####恢复
[root@mariadb ~]# mysql -uroot -pwestos westos -e "show tables from westos"####查看westos库的表格
+------------------+
| Tables_in_westos |
+------------------+
| linux            |
| text             |
+------------------+





7.



用户授权
[root@mariadb ~]# vim /etc/my.cnf
 10 skip-networking=0            ####修改可以使用网络登陆

create user lee@localhost identified by 'lee';    ####创建用户lee,只能在本机登陆
create user lee@'%' identified by 'lee';    ####创建用户lee,只能通过网络登陆
select User,Host from mysql.user        ####查看用户表
MariaDB [(none)]> select User,Host from mysql.user
    -> ;
+------+-----------+
| User | Host      |
+------+-----------+
| lee  | %         |        ####网络登陆
| root | 127.0.0.1 |        
| root | ::1       |
| lee  | localhost |        ####本地登陆
| root | localhost |
+------+-----------+
5 rows in set (0.00 sec)
grant insert,update,delete,select on westos.* to lee@localhost;####用户授权
grant select on westos.* to lee@'%';        ####用户授权
show grants for lee@'%';            ####查看用户授权
MariaDB [(none)]> show grants for lee@localhost;
+------------------------------------------------------------------------------------------------------------+
| Grants for lee@localhost                                                                                   |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'lee'@'localhost' IDENTIFIED BY PASSWORD '*9BB439A3A652A9DAD3718215F77A7AA06108A267' |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `westos`.* TO 'lee'@'localhost'                                    |
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

drop user lee@'%'                ####删除用户
revoke delete on westos.* from lee@localhost    ####删除lee@localhost的delete权限




8.密码修改
(1)知道密码
mysqladmin -uroot -pwestos password lee
(2)忘记密码
systemctl stop mariadb.service        ####停止mariadb服务
mysqld_safe --skip-grant-tables &    ####开启mysql登陆接口并忽略授权表
mysql                    ####直接进入数据库
select User,Host,Password from mysql.user    ####查看密码
MariaDB [(none)]> select User,Host,Password from mysql.user
    -> ;
+------+-----------+-------------------------------------------+
| User | Host      | Password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *9BB439A3A652A9DAD3718215F77A7AA06108A267 |
| root | 127.0.0.1 | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
| root | ::1       | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
| lee  | %         | *9BB439A3A652A9DAD3718215F77A7AA06108A267 |
| lee  | localhost | *9BB439A3A652A9DAD3718215F77A7AA06108A267 |
+------+-----------+-------------------------------------------+
5 rows in set (0.00 sec)

update mysql.user set Password=password('123') where User='root';####更新超级用户密码
ps aux | grep mysql        ####过滤出mysql进程
[root@mariadb ~]# ps aux | grep mysql
root      2258  0.0  0.1 113252  1560 pts/0    S    01:50   0:00 /bin/sh /usr/bin/mysqld_safe --skip-grant-tables
mysql     2414  0.1  9.2 859068 91748 pts/0    Sl   01:50   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      2458  0.0  0.0 112640   936 pts/0    S+   01:52   0:00 grep --color=auto mysql
[root@mariadb ~]# kill -9 2258 2414 2458

fg
kill -9
[root@mariadb ~]# systemctl start mariadb
[root@mariadb ~]# mysql -uroot -p123        ####用新密码登陆






原创粉丝点击