mysql基本操作sql操作语句

2019-06-10 分类:未分类ggg 阅读(24) 评论(0)

配置安装源

下载安装源并上传到服务器。

epel-release-latest-7.noarch_(1).rpm webtatic-release_(1).rpm

安装安装源

rpm -Uvh epel-release-latest-7.noarch_(1).rpm
rpm -Uvh webtatic-release_(1).rpm

安装mysql

yum install -y mariadb-server 

启动mysql并加如开机自启

 systemctl start mariadb.service  &&  systemctl enable mariadb.service 

进入mysql

[root@web02 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 24
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

查看数据库中的所有数据库

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

MariaDB [(none)]> 

查看mysql数据库中的所有表


MariaDB [(none)]> show tables from mysql;
+---------------------------+
| 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)

查看mysql数据库中user表中的user和hosts字段。

MariaDB [(none)]> select user,host from mysql.user;
+-----------+------------+
| user      | host       |
+-----------+------------+
| root      | 127.0.0.1  |
| root      | ::1        |
| root      | localhost  |
| root      | web02      |
+-----------+------------+
5 rows in set (0.04 sec)

查看当前在以哪个用户的身份登录的数据库

MariaDB [(none)]> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.03 sec)

进入mysql数据库

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

查看当前在使用的数据库

MariaDB [mysql]> select database();
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)

创建一个wordpress数据库

MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.03 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)

创建两个用户

授权wordpress数据库的所有权限(增删改查)

MariaDB [(none)]> grant all on wordpress.* to wordpress@'172.16.1.%' identified by '123456' ;
Query OK, 0 rows affected (0.25 sec)
MariaDB [(none)]> grant all on wordpress.* to wordpress@'locallhost' identified by '123456' ;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user,host from mysql.user;
+-----------+------------+
| user      | host       |
+-----------+------------+
| root      | 127.0.0.1  |
| wordpress | 172.16.1.% |
| root      | ::1        |
| root      | localhost  |
| wordpress | locallhost |
| root      | web02      |
+-----------+------------+
6 rows in set (0.00 sec)

备份数据库

[root@web02 ~]# mysqldump -uroot -A >/root/all.sql
[root@web02 ~]# ll /root
total 22420
-rw-r--r--  1 root   root        515388 Jun 10 19:07 all.sql

删除wordpress 数据库的管理用户


MariaDB [(none)]> drop user wordpress@'172.16.1.%' ;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> drop user wordpress@'locallhost';
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       |
| root | localhost |
| root | web02     |
+------+-----------+
4 rows in set (0.00 sec)
##删除wordpress数据库

MariaDB [(none)]> drop databases wordpress; 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 'databases wordpress' at line 1 MariaDB [(none)]> drop database wordpress; Query OK, 0 rows affected (0.00 sec)

>到这里最初创建的的wordpress数据库和两个用户被删除了。接下来就可以使用备份的数据库进行恢复

##使用数据库备份文件进行恢复数据库 并刷新用户权限

[root@web02 ~]# mysql </root/all.sql

##进入数据进行检查

MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | wordpress | +--------------------+ 5 rows in set (0.00 sec)

MariaDB [(none)]> select user,host from mysql.user; +-----------+------------+ | user | host | +-----------+------------+ | root | 127.0.0.1 | | wordpress | 172.16.1.% | | root | ::1 | | root | localhost | | wordpress | locallhost | | root | web02 | +-----------+------------+ 6 rows in set (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)


##如果修改用户后没有刷新用户权限,会导致用户无法使用

[root@web02 ~]# mysql -uwordpress -p Enter password: ERROR 1045 (28000): Access denied for user 'wordpress'@'localhost' (using password: YES)

##备份数据库直接为压缩包

[root@web01 ~]# mysqldump -uroot -p -A |gzip >/root/all-gzip.sql.gz Enter password: [root@web01 ~]# ll /root/all-gzip.sql.gz -rw-r--r-- 1 root root 139656 Jun 10 11:21 /root/all-gzip.sql.gz [root@web01 ~]# ll -h /root/all-gzip.sql.gz -rw-r--r-- 1 root root 137K Jun 10 11:21 /root/all-gzip.sql.gz

您可能也喜欢:

centos7 安装mysql 解决 Failed to start mariadb.service: Unit not found.

启动mysql服务的时候出现这样的错误。 解决办法:   首先需要安装mariadb-server yum install -y mariadb-server 启动服务 systemctl start mariadb.service 添加到开机启动  systemctl enable mariadb...

more

数据库 nfs拆分

数据库拆分 为什么要进行数据库拆分? 由于单台服务器运行LNMP架构会导致网站访问缓慢,当内存被吃满时,很容易导致系统出现oom,从而kill掉MySQL 数据库,所以需要将web和数据库进行独立部署。 数据库拆分后解决了什么问题? 1.缓解web网站的压力 2.增强数据库读写性能 3.提高用户...

more

世界,您好!

欢迎使用WordPress。这是您的第一篇文章。编辑或删除它,然后开始写作吧! ...

more

评论&留言
欢迎新朋友你的到来!
还没有人抢沙发呢~
昵称
邮箱
网站

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

切换登录

注册