mysql常用命令

    技术2022-07-13  120

    检查是否安装成功:sudo netstat -tap | grep mysql

    进入mysql:mysql -u root -p;

    退出msyql:exit;

    重启mysql:sudo /etc/init.d/mysql restart

     

    查看mysql版本:mysqladmin -u root -p version

    查看当前所有数据库:show databases;

    选择数据库:use mydata;

    创建数据库:create database test1;

    删除数据库:drop database test1;

    显示表数据结构:describe 表名

     

    修改密码:mysqladmin -u用户名 -p旧密码 password 新密码

    创建数据表:create table <表名> ( <字段名1> <类型1> [,..<字段名n> <类型n>]);

    如:

    create table MyClass(

        > id int(4) not null primary key auto_increment,

        > name char(20) not null,

        > sex int(4) not null default '0',

        > degree double(16,2));

     

    drop table MyClass; # 删除表

    alter table 表名 add字段 类型; # 增加表字段

        如:alter table MyClass add passtest int(4) default '0'

    alter table table_name DROP field_name; # 删除表字段

    modify column `department_name`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL; # 修改列

    ALTER TABLE `t_distributor_purchase_order` ADD INDEX order_type ( `order_type` );  ## 添加索引

    alter table drop index `mobile_phone`;  # 删除索引

    alter table `t_store_image` add column `id` int not null auto_increment primary key comment '主键' first;   #添加列,并设置主键自增长

    alter table `t_tires_special_offer` modify column `distributor_ids` varchar(5000);  ## 修改表列字段长度

     

    ## 查询是否有锁表

    select * from information_schema.INNODB_LOCKS;

    select * from information_schema.INNODB_LOCK_WAITS;

     

    设置字符集(以utf8为例):

      1) 查看当前的编码:show variables like 'character%';

      2) 修改my.cnf,在[client]下添加default-character-set=utf8

      3) 修改[mysqld]下添加character-set-server=utf8

      4) 重启mysql。 

     

    安装 mysql

    sudo apt-get install mysql-server

     

     

     

     

    Processed: 0.025, SQL: 9