美文网首页
Mariadb数据库入门语句

Mariadb数据库入门语句

作者: Sainbyno | 来源:发表于2017-08-02 19:16 被阅读0次

    创建用户账号

    MariaDB [mydb]> create user yonghuming@'172.16.%.%' identified by 'yonghumima'; 
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [mydb]> 
    

    忘记用户密码后修改密码

    >>方法一:
    MariaDB [mydb]> set password for 'yonghuming'@'172.16.%.%' = password('mageedu');
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [mydb]> flush privileges;    <-- 让MySQL重新加载授权表,即修改密码后让其即时生效
    Query OK, 0 rows affected (0.00 sec)
     
    >>方法二:
    MariaDB [mydb]> select user,host,password from mysql.user;
    +------------+---------------+-------------------------------------------+
    | user       | host          | password                                  |
    +------------+---------------+-------------------------------------------+
    | root       | localhost     |                                           |
    | root       | host1.centos7 |                                           |
    | root       | 127.0.0.1     |                                           |
    | root       | ::1           |                                           |
    |            | localhost     |                                           |
    |            | host1.centos7 |                                           |
    | test       | 192.168.10.%  | *9A94EE7D14C10908118B62D2DA88E6932E11E438 | <-- 修改前结尾是E438
    | wxg        | 192.168.%.%   | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
    | yonghuming | 172.16.%.%    | *9A94EE7D14C10908118B62D2DA88E6932E11E438 | <-- 相同密码的加密后生成的也相同
    +------------+---------------+-------------------------------------------+
    9 rows in set (0.00 sec)
    
    MariaDB [mydb]> update mysql.user set password=password('123456') where user='test' and host='192.168.10.%';    
    (上面的整条命令的意思是:把test用户的密码修改为‘123456’ where 是条件,后面一定要跟条件)   
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    MariaDB [mydb]> select user,host,password from mysql.user;                                                  
    +------------+---------------+-------------------------------------------+
    | user       | host          | password                                  |
    +------------+---------------+-------------------------------------------+
    | root       | localhost     |                                           |
    | root       | host1.centos7 |                                           |
    | root       | 127.0.0.1     |                                           |
    | root       | ::1           |                                           |
    |            | localhost     |                                           |
    |            | host1.centos7 |                                           |
    | test       | 192.168.10.%  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | <--修改后是2AD9
    | wxg        | 192.168.%.%   | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | <--相同密码生成的password字段的加密码相同
    | yonghuming | 172.16.%.%    | *9A94EE7D14C10908118B62D2DA88E6932E11E438 |
    +------------+---------------+-------------------------------------------+
    9 rows in set (0.00 sec)
    (注意当我们调用内键变量password修改密码的时候,它是对称的,同样的密码所生成的加密码是相同的!细看表中test和wxg的加密码)
    
    MariaDB [mydb]> flush privileges;    <-- 让MySQL重新加载授权表,即修改密码后让其即时生效
    Query OK, 0 rows affected (0.00 sec)
    MariaDB [mydb]> 
    

    查看当前数据库

    [root@centos7 ~]#mysql -uroot -p
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    
    MariaDB [(none)]> show databases;    <---查看当前数据库
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | bjtechan           |
    | mysql              |
    | performance_schema |
    +--------------------+
    4 rows in set (0.09 sec)
    
    MariaDB [(none)]> 
    
    

    切换到某个数据库去访问

    MariaDB [bjtechan]> 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]> 
    
    

    查看当前数据库有哪些表

    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]> 
    
    

    查看某一个表的表结构

    MariaDB [mysql]> desc user;    <---查看表结构:desc 加 表名
    +------------------------+-----------------------------------+------+-----+---------+-------+
    | 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       |       |
    | Show_db_priv           | enum('N','Y')                     | NO   |     | N       |       |
    | Super_priv             | enum('N','Y')                     | NO   |     | N       |       |
    | Repl_slave_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.00 sec)
    
    MariaDB [mysql]> 
    
    

    选择性查看一个表格的某些字段

    
    MariaDB [mysql]> select host,user,password from user;    <---查看user表格的主机用户密码信息
    +---------------+------+-------------------------------------------+
    | host          | user | password                                  |
    +---------------+------+-------------------------------------------+
    | localhost     | root | *9A94EE7D14C10908118B62D2DA88E6932E11E438 |
    | centos7.3.wxg | root | *9A94EE7D14C10908118B62D2DA88E6932E11E438 |
    | 127.0.0.1     | root | *9A94EE7D14C10908118B62D2DA88E6932E11E438 |
    | ::1           | root | *9A94EE7D14C10908118B62D2DA88E6932E11E438 |
    +---------------+------+-------------------------------------------+
    4 rows in set (0.01 sec)
    
    MariaDB [mysql]> 
    
    

    显示当前的用户名

    MariaDB [mysql]> select user();    <---显示当前的用户名
    +----------------+
    | user()         |
    +----------------+
    | root@localhost |    <---当前登录的用户,这才是完整的用户名;用户@主机名(可以是ip地址)
    +----------------+
    1 row in set (0.00 sec)
    
    MariaDB [mysql]> 
    
    

    创建数据库

    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | bjtechan           |
    | mysql              |
    | performance_schema |
    +--------------------+
    4 rows in set (0.00 sec)
    
    MariaDB [(none)]> create database testdb;    <---创建一个叫testdb的数据库
    Query OK, 1 row affected (0.00 sec)
    
    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | bjtechan           |
    | mysql              |
    | performance_schema |
    | testdb             |
    +--------------------+
    5 rows in set (0.00 sec)
    
    MariaDB [(none)]> quit    <---退出数据库
    Bye
    [root@centos7 ~]#
    
    

    显示当前登录用户

    
    MariaDB [mydb]> select current_user();    <-- 内键函数,显示当前登录用户
    +----------------+
    | current_user() |
    +----------------+
    | root@localhost |
    +----------------+
    1 row in set (0.00 sec)
    
    MariaDB [mydb]>
    

    相关文章

      网友评论

          本文标题:Mariadb数据库入门语句

          本文链接:https://www.haomeiwen.com/subject/twlylxtx.html