首页 > 代码库 > mariadb数据库
mariadb数据库
第八单元
mariadb数据库
一 mariadb建立
1 yum install mariadb-server.x86_64 -y ###安装软件###
2 systemctl start mariadb ###开启服务###
3 netstat -antlpe | grep mysql ###查看数据库在网络中的端口###
4 vim /etc/my.cnf ###编辑配置文件###
内容:
skip-networking=1 #####绕过网络的功能,关闭数据库在网络中开启的端口,此时只允许通过套接字文件进行本地连接,阻断所有来自网络的tcp/ip连接。###
5 systemctl restart mariadb.service ###重启服务###
6 mysql_secure_installation ####对数据库初始化,如果不初始化,则使用mysql直接能进入数据库####
初始化的内容:
Set root password? [Y/n] ###是否设置密码###
Remove anonymous users? [Y/n] ###是否允许匿名用户登入###Disallow root login remotely? [Y/n] ###是否允许远程root登入####Remove test database and access to it? [Y/n] ###是否删除测试数据库####
Reload privilege tables now? [Y/n] y ###是否重新加载表####
7 mysql -uroot -p ###-u表示指定登入用户,-p表示此用户密码,不要在-p后直接输入密码,会被截取,不安全####
过程如下:
[root@server Desktop]# yum install mariadb-server.x86_64 -y
[root@server Desktop]# vim /etc/my.cnf ###编辑配置文件###
内容:
[mysqld]
skip-networking=1 #####绕过网络的功能,关闭数据库在网络中开启的端口###
[root@server Desktop]# systemctl restart mariadb.service ###重启服务###
[root@server Desktop]# netstat -antlpe | grep mysql ###再次查看端口###
[root@server Desktop]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
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)]> -Ctrl-C -- exit!
Aborted
[root@server Desktop]# 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] 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!
[root@server Desktop]# mysql ###现在没有指定用户就登不上###
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)
[root@server Desktop]# mysql -uroot -p ###-u表示指定登入用户,-p表示此用户密码,不要在-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)]> Ctrl-C -- exit!
Aborted
二 数据库的基本sql语句操作
1)数据库的登入
mysql -uroot -p ###-u表示指定登入用户,-p表示此用户密码####
2)查询
show databases; ####显示有哪些数据库####
use mysql; ###进入mysql库#####
show tables; ###显示当前库中表的名称####
select * from user; ##查询user表中的所有内容(*可以用此表中的任何字段代替)####
desc user; ###查询user表的结构(显示所有字段名称)
过程如下:
[root@server ~]# mysql -uroot -p ####登入数据库####
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 15
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)]> use mysql; ###进入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 User,Host from user;
+------+-----------+
| User | Host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
+------+-----------+
3 rows in set (0.00 sec)
MariaDB [mysql]> desc user; ###查询user表的结构(显示所有字段名称)
+------------------------+-----------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+---------+-------+
| Host | char(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)
3)数据库和表的建立
1 create database westos; ###创建westos库####
2 create table linux( ####创建linux表,并且linux表中含有两个字段,username,password######
-> username varchar(15) not null, ####username字段,varchar类型的字符长度最多为255,在此处限定15,not null指不能为空###
-> password varchar(15) not null ); ###password字段,varchar类型
3 insert into linux valus (‘user1‘,‘123‘); ###在linux表格中插入数据,username字段的数据为user1,password字段为123
insert into linux values (‘user4‘,password(‘123‘)); ###在linux表格中插入数据user4,且插入password字段是用password加密过的####
4 delete from linux where password=‘0‘; ###删除linux表中password为0的数据####
过程如下:
MariaDB [mysql]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [mysql]> 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)
MariaDB [(none)]> use westos; ###进入westos库###
Database changed
MariaDB [westos]> show tables;
Empty set (0.00 sec)
MariaDB [westos]> create table linux( ####创建linux表,并且linux表中含有两个字段,username,password######
-> username varchar(15),not null, ####username字段,varchar类型的字符长度最多为255,在此处限定15
-> password varchar(15),not null );
Query OK, 0 rows affected (0.35 sec)
MariaDB [westos]> desc linux;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(15) | NO | | NULL | |
| password | varchar(15) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
MariaDB [westos]> insert into linux values (‘user3‘,‘123‘);
Query OK, 1 row affected (0.09 sec)
MariaDB [westos]> insert into linux values (‘user2‘,‘123‘);
Query OK, 1 row affected (0.09 sec)
MariaDB [westos]> insert into linux values (‘user1‘,‘123‘);
Query OK, 1 row affected (0.08 sec)
MariaDB [westos]> insert into linux values (‘user4‘,password(‘123‘));
Query OK, 1 row affected, 1 warning (0.08 sec)
MariaDB [westos]> select * from linux
-> ;
+----------+-----------------+
| username | password |
+----------+-----------------+
| user3 | 123 |
| user2 | 123 |
| user1 | 123 |
| user4 | *23AE809DDACAF9 |
+----------+-----------------+
4 rows in set (0.00 sec)
MariaDB [westos]> select * from linux;
+----------+-----------------+
| username | password |
+----------+-----------------+
| user1 | 123 |
| user2 | 123 |
| user3 | *23AE809DDACAF9 |
| user4 | 123 |
| user5 | 0 |
| user6 | 0 |
+----------+-----------------+
6 rows in set (0.01 sec)
MariaDB [westos]> delete from linux where password=‘0‘; ###删除linux表中password为0的数据####
MariaDB [westos]> select * from linux;
+----------+-----------------+
| username | password |
+----------+-----------------+
| user1 | 123 |
| user2 | 123 |
| user3 | *23AE809DDACAF9 |
| user4 | 123 |
+----------+-----------------+
4 rows in set (0.00 sec)
4)更新数据库信息
1 update linux set password=password(‘123‘) where password=‘123‘; ###更新linux表中密码为123的数据,将其全部变成加密###
2 alter table linux add class varchar(20) not null; ##alter指改变表的结构,添加class字段,字符最大长度为20且不能为空####
3 alter table linux add date varchar(20) not null; ###添加date字段到linux表的最后一列##
4 alter table linux drop date; ###删除date字段###
5 alter table linux add date varchar(20) not null after password;###添加date字段在password字段后
6 updata linux set password=password(‘456‘) where ( username=‘user1‘or username=‘user2‘ ); ###更新linux表将username字段的user1和user2的password字段为password加密的456###
过程如下:
MariaDB [westos]> select * from linux;
+----------+-----------------+
| username | password |
+----------+-----------------+
| user1 | 123 |
| user2 | 123 |
| user3 | *23AE809DDACAF9 |
| user4 | 123 |
+----------+-----------------+
4 rows in set (0.00 sec)
MariaDB [westos]> update linux set password=password(‘123‘) where password=‘123‘; ###更新linux表中密码为123的数据,将其全部变成加密###
Query OK, 3 rows affected, 3 warnings (0.29 sec)
Rows matched: 3 Changed: 3 Warnings: 3
MariaDB [westos]> select * from linux;+----------+-----------------+
| username | password |
+----------+-----------------+
| user1 | *23AE809DDACAF9 |
| user2 | *23AE809DDACAF9 |
| user3 | *23AE809DDACAF9 |
| user4 | *23AE809DDACAF9 |
+----------+-----------------+
4 rows in set (0.00 sec)
MariaDB [westos]> alter table linux add class varchar(20) not null; ##alter指改变表的结构,添加class字段,字符最大长度为20####
Query OK, 4 rows affected (0.14 sec)
Records: 4 Duplicates: 0 Warnings: 0
MariaDB [westos]> select * from linux;+----------+-----------------+-------+
| username | password | class |
+----------+-----------------+-------+
| user1 | *23AE809DDACAF9 | |
| user2 | *23AE809DDACAF9 | |
| user3 | *23AE809DDACAF9 | |
| user4 | *23AE809DDACAF9 | |
+----------+-----------------+-------+
4 rows in set (0.00 sec)
MariaDB [westos]> alter table linux add date varchar(20) not null; ###添加date字段到linux表的最后一列###
Query OK, 4 rows affected (0.11 sec)
Records: 4 Duplicates: 0 Warnings: 0
MariaDB [westos]> select * from linux;
+----------+-----------------+-------+------+
| username | password | class | date |
+----------+-----------------+-------+------+
| user1 | *23AE809DDACAF9 | | |
| user2 | *23AE809DDACAF9 | | |
| user3 | *23AE809DDACAF9 | | |
| user4 | *23AE809DDACAF9 | | |
+----------+-----------------+-------+------+
4 rows in set (0.00 sec)
MariaDB [westos]> alter table linux drop date; ###删除datte字段###
Query OK, 4 rows affected (0.11 sec)
Records: 4 Duplicates: 0 Warnings: 0
MariaDB [westos]> select * from linux;
+----------+-----------------+-------+
| username | password | class |
+----------+-----------------+-------+
| user1 | *23AE809DDACAF9 | |
| user2 | *23AE809DDACAF9 | |
| user3 | *23AE809DDACAF9 | |
| user4 | *23AE809DDACAF9 | |
+----------+-----------------+-------+
4 rows in set (0.00 sec)
MariaDB [westos]> alter table add date varchar(20) not null after password;###添加date字段在password字段后
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 ‘add date varchar(20) not null after password‘ at line 1
MariaDB [westos]> alter table linux add date varchar(20) not null after password;
Query OK, 4 rows affected (0.10 sec)
Records: 4 Duplicates: 0 Warnings: 0
MariaDB [westos]> select * from linux
-> ;
+----------+-----------------+
| username | password |
+----------+-----------------+
| user3 | 123 |
| user2 | 123 |
| user1 | 123 |
| user4 | *23AE809DDACAF9 |
+----------+-----------------+
4 rows in set (0.00 sec)
MariaDB [westos]> update linux set password=password(‘456‘) where (username=‘user1‘ or username=‘user2‘);
Query OK, 2 rows affected, 2 warnings (0.09 sec)
Rows matched: 2 Changed: 2 Warnings: 2
MariaDB [westos]> select * from linux;
+----------+-----------------+
| username | password |
+----------+-----------------+
| user3 | 123 |
| user2 | *531E182E2F7208 |
| user1 | *531E182E2F7208 |
| user4 | *23AE809DDACAF9 |
+----------+-----------------+
4 rows in set (0.00 sec)
5)删除数据库
1 delete from linux where username=‘user1‘; ###将linux表中的数据user删除###
2 drop table linux; ###删除linux表###
3 drop database westos; ###删除westos库###
过程如下:
MariaDB [westos]> select * from linux;
+----------+-----------------+
| username | password |
+----------+-----------------+
| user3 | 123 |
| user2 | *531E182E2F7208 |
| user1 | *531E182E2F7208 |
| user4 | *23AE809DDACAF9 |
+----------+-----------------+
4 rows in set (0.00 sec)
MariaDB [westos]> delete from linux where username=‘user1‘;
Query OK, 1 row affected (0.34 sec)
MariaDB [westos]> select * from linux
-> ;
+----------+-----------------+
| username | password |
+----------+-----------------+
| user3 | 123 |
| user2 | *531E182E2F7208 |
| user4 | *23AE809DDACAF9 |
+----------+-----------------+
3 rows in set (0.00 sec)
MariaDB [westos]> drop table linux;
Query OK, 0 rows affected (0.08 sec)
MariaDB [westos]> show tables;
Empty set (0.00 sec)
MariaDB [westos]> drop database westos;
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)
6)数据库备份
1 mysqldump -uroot -predhat --all-database ###备份所有表中的所有数据###
2 mysqldump -uroot -predhat --all-database --no-data ###备份所有表,但是不备份数据####
3 mysqldump -uroot -predhat westos > /mnt/westos.sql ###备份westos库并把数据保存到/mnt/westos.sql####
4 mysqldump -uroot -predhat westos 备份westos库
5 mysqldump -uroot -predhat westos > /mnt/westos.sql ###备份westos库并把数据保存到/mnt/westos.sql####
6 mysql -uroot -predhat -e "drop database westos;" ###删除westos库##
7 mysql -uroot -predhat -e "create database westos;" ###建立westos库###
8 mysql -uroot -predhat westos < /mnt/westos.sql ###将数据导入westos库###
9 mysql -uroot -predhat -e "select * from westos.linux;" ###显示westos库中的linux表格的所有数据###
10 mysqldump -uroot -predhat westos linux > /mnt/linux.sql ###备份为westos库中的linux表并保存到/mnt/linux.sql
11 mysql -uroot -predhat westos < /mnt/linux.sql ###将数据导入为westos库####
过程如下:
[root@mariadb ~]# mysqldump -uroot -predhat westos > /mnt/westos.sql
[root@mariadb ~]# mysql -uroot -predhat -e "drop database westos;"
[root@mariadb ~]# mysql -uroot -predhat -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
[root@mariadb ~]# mysql -uroot -predhat -e "create database westos;"
[root@mariadb ~]# mysql -uroot -predhat -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| westos |
+--------------------+
[root@mariadb ~]# mysql -uroot -predhat westos < /mnt/westos.sql
[root@mariadb ~]# mysql -uroot -predhat -e "select * from westos.linux"
+----------+-------------------------------------------+
| username | password |
+----------+-------------------------------------------+
| user1 | 123 |
| user2 | 123 |
| user3 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+----------+-------------------------------------------+
[root@mariadb ~]# mysql -uroot -predhat westos linux > /mnt/linux.sql
[root@mariadb ~]# mysql -uroot -predhat -e "drop table linux;"
ERROR 1046 (3D000) at line 1: No database selected
[root@mariadb ~]# mysql -uroot -predhat -e "drop table linux from westos;"
ERROR 1046 (3D000) at line 1: No database selected
[root@mariadb ~]# mysql -uroot -predhat -e "drop database westos;"
[root@mariadb ~]# mysql -uroot -predhat -e "create database westos;"
[root@mariadb ~]# mysql -uroot -predhat -e "select * from westos.linux;"
ERROR 1146 (42S02) at line 1: Table ‘westos.linux‘ doesn‘t exist
[root@mariadb ~]# mysql -uroot -predhat westos < /mnt/linux.sql
[root@server ~]# mysql -uroot -predhat -e "show tables from westos;"
+------------------+
| Tables_in_westos |
+------------------+
| linux |
+------------------+
7)用户授权
1 create user hello@localhost identified by ‘hello‘; ###建立用户hello,此用户只能通过本机登入####
2 create user hello@‘%‘ identified by ‘hello‘; ###建立用户hello,此用户可以通过网络登入###
3 grant insert,update,delete,select on westos.* to hello@localhost;
###用户授权###
4 grant select on westos.* to hello@‘%‘; ###用户授权###
5 show grants for hello@‘%‘; ###查看用户权限##
6 show grants for hello@localhost;
7 revoke delete on westos.* from hello@localhost; ###去除用户权限##
8 drop user hello@‘%‘; ###删除用户###
过程如下:
[root@mariadb ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 25
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MariaDB [(none)]> select User,Host from mysql.user;
+------+-----------+
| User | Host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
+------+-----------+
3 rows in set (0.00 sec)
MariaDB [(none)]> create user hello@localhost identified by ‘hello‘;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select User,Host from mysql.user;
+-------+-----------+
| User | Host |
+-------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| hello | localhost |
| root | localhost |
+-------+-----------+
4 rows in set (0.00 sec)
MariaDB [(none)]> create hello@‘%‘ identified by ‘hello‘;
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 ‘hello@‘%‘ identified by ‘hello‘‘ at line 1
MariaDB [(none)]> create user hello@‘%‘ identified by ‘hello‘;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select User,Host from mysql.user;
+-------+-----------+
| User | Host |
+-------+-----------+
| hello | % |
| root | 127.0.0.1 |
| root | ::1 |
| hello | localhost |
| root | localhost |
+-------+-----------+
5 rows in set (0.00 sec)
MariaDB [(none)]> quit
Bye
[root@mariadb ~]# mysql -uhello -phello -h localhost
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 26
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MariaDB [(none)]> quit
Bye
[root@mariadb ~]# vim /etc/my.cnf
[root@mariadb ~]# netstat -antlpt | grep mysql
[root@mariadb ~]# systemctl restart mariadb.service
[root@mariadb ~]# netstat -antlpt | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 6148/mysqld
[root@mariadb ~]# mysql -uhello -phello -h 172.25.254.112
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> quit
Bye
[root@mariadb ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MariaDB [(none)]> grant insert,update,delete,select on westos.* hello@localhost;
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 ‘hello@localhost‘ at line 1
MariaDB [(none)]> grant insert,update,delete,select on westos.* to hello@localhost;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant select on westos.* to hello@‘%‘;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> quit
Bye
[root@mariadb ~]# mysql -uhello -phello -h localhost
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| westos |
+--------------------+
2 rows in set (0.00 sec)
MariaDB [(none)]> use westos;
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 [westos]> show tables;
+------------------+
| Tables_in_westos |
+------------------+
| linux |
+------------------+
1 row in set (0.00 sec)
MariaDB [westos]> select * from linux;
+----------+-------------------------------------------+
| username | password |
+----------+-------------------------------------------+
| user1 | 123 |
| user2 | 123 |
| user3 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+----------+-------------------------------------------+
3 rows in set (0.00 sec)
MariaDB [westos]> insert into linux values (‘user4‘,‘123‘);
Query OK, 1 row affected (0.08 sec)
MariaDB [westos]> delete from linux where usrename=‘user4‘;
ERROR 1054 (42S22): Unknown column ‘usrename‘ in ‘where clause‘
MariaDB [westos]> delete from linux where username=‘user4‘;
Query OK, 1 row affected (0.08 sec)
MariaDB [westos]> quit
Bye
[root@mariadb ~]# mysql -uhello -phello -h 172.25.254.112
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MariaDB [(none)]> select * from westos.linux;
+----------+-------------------------------------------+
| username | password |
+----------+-------------------------------------------+
| user1 | 123 |
| user2 | 123 |
| user3 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+----------+-------------------------------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> show grants for hello@‘%‘;
+------------------------------------------------------------------------------------------------------+
| Grants for hello@% |
+------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘hello‘@‘%‘ IDENTIFIED BY PASSWORD ‘*6B4F89A54E2D27ECD7E8DA05B4AB8FD9D1D8B119‘ |
| GRANT SELECT ON `westos`.* TO ‘hello‘@‘%‘ |
+------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
MariaDB [(none)]> show grants for hello@localhost;
+--------------------------------------------------------------------------------------------------------------+
| Grants for hello@localhost |
+--------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘hello‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*6B4F89A54E2D27ECD7E8DA05B4AB8FD9D1D8B119‘ |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `westos`.* TO ‘hello‘@‘localhost‘ |
+--------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
[root@mariadb ~]# 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.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MariaDB [(none)]> revoke delete on westos.* from hello@localhost;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show grants for hello@localhost;
+--------------------------------------------------------------------------------------------------------------+
| Grants for hello@localhost |
+--------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘hello‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*6B4F89A54E2D27ECD7E8DA05B4AB8FD9D1D8B119‘ |
| GRANT SELECT, INSERT, UPDATE ON `westos`.* TO ‘hello‘@‘localhost‘ |
+--------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
MariaDB [(none)]> drop user hello@‘%‘;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select User,Host from mysql.user;
+-------+-----------+
| User | Host |
+-------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| hello | localhost |
| root | localhost |
+-------+-----------+
5 rows in set (0.00 sec)
8) 密码修改
(1) 当原密码还记得的时候:
mysqladmin -uroot -predhat passsword westos ###将原来为redhat的密码改成westos
过程如下:
[root@mariadb ~]# mysqladmin -uroot -predhat password westos
[root@mariadb ~]# mysql -uroot -pwestos
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MariaDB [(none)]> quit
Bye
(2) 当原密码不记得的时候:
1 systemctl stop mariadb.service
2 mysqld_safe --skip-grant-tables & ###开启mysql登入接口并忽略授权表##
3 mysql ###直接不用密码可以登入###
4 select User,Host,Password from mysql.user;
5 update mysql.user set Password=password(‘123‘) where User=‘root‘;
###更新超级用户密码信息###
6 ps aux | grep mysql ###过滤所有mysql的所有进程###
7 kill -9 pid ###结束所有mysql进程###
8 systemctl start mariadb ###重启服务###
过程如下:
[root@mariadb ~]# systemctl stop mariadb.service
[root@mariadb ~]# mysqld_safe --skip-grant-tables &
[1] 7556
[root@mariadb ~]# 170514 22:21:02 mysqld_safe Logging to ‘/var/log/mariadb/mariadb.log‘.
170514 22:21:02 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@mariadb ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MariaDB [(none)]> select User,Host,Password from mysql.user;
+-------+-----------+-------------------------------------------+
| User | Host | Password |
+-------+-----------+-------------------------------------------+
| root | localhost | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
| root | 127.0.0.1 | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | ::1 | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| hello | localhost | *6B4F89A54E2D27ECD7E8DA05B4AB8FD9D1D8B119 |
| lee | % | |
+-------+-----------+-------------------------------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> update mysql.user set Password=password(‘123‘) where User=‘root‘;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
MariaDB [(none)]> quit
Bye
[root@mariadb ~]# ps aux | grep mysql
root 7556 0.0 0.1 113248 1556 pts/0 S 22:21 0:00 /bin/sh /usr/bin/mysqld_safe --skip-grant-tables
mysql 7711 0.0 9.0 843944 89480 pts/0 Sl 22:21 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 7848 0.0 0.0 112640 936 pts/0 S+ 22:26 0:00 grep --color=auto mysql
[root@mariadb ~]# kill -9 7711
[root@mariadb ~]# /usr/bin/mysqld_safe: line 183: 7711 Killed nohup /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 < /dev/null >> /var/log/mariadb/mariadb.log 2>&1
170514 22:26:20 mysqld_safe Number of processes running now: 0
170514 22:26:20 mysqld_safe mysqld restarted
[root@mariadb ~]# kill -9 7556
[root@mariadb ~]# kill -9 7848
-bash: kill: (7848) - No such process
[1]+ Killed mysqld_safe --skip-grant-tables
[root@mariadb ~]# ps aux | grep mysql
mysql 7865 0.1 8.5 778116 84628 pts/0 Sl 22:26 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 7922 0.0 0.0 112640 932 pts/0 R+ 22:27 0:00 grep --color=auto mysql
[root@mariadb ~]# kill -9 7865
[root@mariadb ~]# ps aux | grep mysql
root 7927 0.0 0.0 112640 936 pts/0 R+ 22:27 0:00 grep --color=auto mysql
[root@mariadb ~]# systemctl start mariadb
[root@mariadb ~]# mysql -uroot -p123
9)数据库的网页管理工具
1 yum install httpd -y
2 yum install php -y
3 yum install php-mysql -y
4 systemctl start httpd
5 systemctl enable httpd.service
6 tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 -C /var/www/html/ ####解压,-C指指定路径,解压到/var/www/html/####
7 mv phpMyAdmin-3.4.0-all-languages/ mysqladmin ###重命名,不然名字太长,不方便访问####
8 cd /var/www/html/mysqladmin/
9 cp -p config.sample.inc.php config.inc.php
10 vim config.inc.php
11 systemctl restart httpd.service
测试:
访问172.25.254.112/mysqladmin
过程如下:
[root@server ~]# yum install httpd -y
[root@server ~]# yum install php-mysql -y
[root@server ~]# yum install php -y
[root@server ~]# systemctl start httpd
[root@server ~]# systemctl enable httpd.service
ln -s ‘/usr/lib/systemd/system/httpd.service‘ ‘/etc/systemd/system/multi-user.target.wants/httpd.service‘
[root@server ~]# systemctl stop firewalld.service
[root@server ~]# systemctl disable firewalld.service
rm ‘/etc/systemd/system/basic.target.wants/firewalld.service‘
rm ‘/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service‘
[root@server ~]# tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 -C /var/www/html/ ####解压,-C指指定路径,解压到/var/www/html/####
[root@server ~]# cd /var/www/html/
[root@server html]# ls
phpMyAdmin-3.4.0-all-languages
[root@server html]# mv phpMyAdmin-3.4.0-all-languages/ mysqladmin ###重命名,不然名字太长,不方便访问####
[root@server html]# ls
mysqladmin
[root@server html]# ll
total 4
drwxr-xr-x. 10 root root 4096 May 11 2011 mysqladmin
[root@server html]# cd mysqladmin/
[root@server mysqladmin]# cp -p config.sample.inc.php config.inc.php
[root@server mysqladmin]# vim config.inc.php
[root@server mysqladmin]# systemctl restart httpd.service
mariadb数据库