MySQL 启动、登录、基本操作

使用权限正确的操作系统用户,我用的是root,执行以下命令

查看MySQL版本

[root@sean oracle]# mysqladmin --version
mysqladmin  Ver 8.42 Distrib 5.1.73, for redhat-linux-gnu on x86_64

启动MySQL

[root@sean oracle]# service mysqld start
Starting mysqld:                                           [  OK  ]

登录MySQL

[root@sean oracle]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

查看数据库列表

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| sean               |
| test               |
+--------------------+
4 rows in set (0.00 sec)

进入指定数据库

mysql> use sean;
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

查看表信息

mysql> show tables;
+----------------+
| Tables_in_sean |
+----------------+
| t_sean         |
+----------------+
1 row in set (0.00 sec)

基本操作CRUD

--建表
mysql> create table t_sean (id int,name varchar(10),age int);
Query OK, 0 rows affected (0.01 sec)

--插入数据
mysql> insert into t_sean values (1,'a',18);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_sean values (2,'b',20);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_sean values (3,'c',27);
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

--查询数据
mysql> select * from t_sean;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | a    |   18 |
|    2 | b    |   20 |
|    3 | c    |   27 |
+------+------+------+
3 rows in set (0.00 sec)

--修改数据
mysql> update t_sean set age = 99 where id = 3;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

--查询数据
mysql> select * from t_sean;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | a    |   18 |
|    2 | b    |   20 |
|    3 | c    |   99 |
+------+------+------+
3 rows in set (0.00 sec)

--删除数据
mysql> delete from t_sean where id=2;
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

--查询数据
mysql> select * from t_sean;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | a    |   18 |
|    3 | c    |   99 |
+------+------+------+
2 rows in set (0.00 sec)

--删除表
mysql> drop table t_sean;
Query OK, 0 rows affected (0.00 sec)

退出登录

mysql> exit
Bye

关闭MySQL

[root@sean oracle]# service mysqld stop
Stopping mysqld:                                           [  OK  ]

总结:以上就是在登录主机使用命令行的方式下,启动、登录和一些基本操作。

对于以上的一些操作,可能需要相关权限,可以通过查看数据库mysql下的user表,查看用户的具体权限。

如果没有权限,可以修改user表。

如下,给在服务器主机localhost上登录的root用户,赋予删除权限。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| sean               |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> use mysql
Database changed

mysql> select * from user where user='root'\G
*************************** 1. row ***************************
                 Host: localhost
                 User: root
             Password: *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: N
          Create_priv: Y
            Drop_priv: Y
          Reload_priv: Y
        Shutdown_priv: Y
         Process_priv: Y
            File_priv: Y
           Grant_priv: Y
      References_priv: Y
           Index_priv: Y
           Alter_priv: Y
         Show_db_priv: Y
           Super_priv: Y
Create_tmp_table_priv: Y
     Lock_tables_priv: Y
         Execute_priv: Y
      Repl_slave_priv: Y
     Repl_client_priv: Y
     Create_view_priv: Y
       Show_view_priv: Y
  Create_routine_priv: Y
   Alter_routine_priv: Y
     Create_user_priv: Y
           Event_priv: Y
         Trigger_priv: Y
             ssl_type: 
           ssl_cipher: 
          x509_issuer: 
         x509_subject: 
        max_questions: 0
          max_updates: 0
      max_connections: 0
 max_user_connections: 0
*************************** 2. row ***************************
                 Host: sean.ora11g
                 User: root
             Password: 
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: N
          Create_priv: Y
            Drop_priv: Y
          Reload_priv: Y
        Shutdown_priv: Y
         Process_priv: Y
            File_priv: Y
           Grant_priv: Y
      References_priv: Y
           Index_priv: Y
           Alter_priv: Y
         Show_db_priv: Y
           Super_priv: Y
Create_tmp_table_priv: Y
     Lock_tables_priv: Y
         Execute_priv: Y
      Repl_slave_priv: Y
     Repl_client_priv: Y
     Create_view_priv: Y
       Show_view_priv: Y
  Create_routine_priv: Y
   Alter_routine_priv: Y
     Create_user_priv: Y
           Event_priv: Y
         Trigger_priv: Y
             ssl_type: 
           ssl_cipher: 
          x509_issuer: 
         x509_subject: 
        max_questions: 0
          max_updates: 0
      max_connections: 0
 max_user_connections: 0
*************************** 3. row ***************************
                 Host: 127.0.0.1
                 User: root
             Password: 
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: Y
          Create_priv: Y
            Drop_priv: Y
          Reload_priv: Y
        Shutdown_priv: Y
         Process_priv: Y
            File_priv: Y
           Grant_priv: Y
      References_priv: Y
           Index_priv: Y
           Alter_priv: Y
         Show_db_priv: Y
           Super_priv: Y
Create_tmp_table_priv: Y
     Lock_tables_priv: Y
         Execute_priv: Y
      Repl_slave_priv: Y
     Repl_client_priv: Y
     Create_view_priv: Y
       Show_view_priv: Y
  Create_routine_priv: Y
   Alter_routine_priv: Y
     Create_user_priv: Y
           Event_priv: Y
         Trigger_priv: Y
             ssl_type: 
           ssl_cipher: 
          x509_issuer: 
         x509_subject: 
        max_questions: 0
          max_updates: 0
      max_connections: 0
 max_user_connections: 0
3 rows in set (0.00 sec)

mysql> update user set Delete_priv='Y' where user='root' and host='localhost';
Query OK, 1 rows affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> commit;
Query OK, 0 rows affected (0.00 sec)
--这种直接修改user表,好像要重启mysql服务器,不是特别确定

或者直接赋权
mysql> grant all privileges on sean.t_sean to root@localhost;
mysql> flush priviledges; 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值