CMD command to operate MySql database, detailed operation

First: start and stop the mysql service

net stop mysql
net start mysql

Second: login

mysqlu用户名 [–h主机名或者IP地址]p密码

Note: The user name is the user you log in. The host name or IP address is optional. If it is a local connection, it is not required. Remote connection needs to be filled in. The password is the password of the corresponding user.

Third: Add a new user 
format: grant permission on database.* to username@login host identified by "password" 
For example, add a user user1 with password as password1, so that it can log in on this machine and query all databases , insert, modify, delete permissions. First connect to mysql as the root user, and then type the following command:

grant select,insert,update,delete on *.* to user1@localhost Identified by "password1";

If you want the user to be able to log in to mysql on any machine, change localhost to "%". 
If you do not want user1 to have a password, you can type another command to remove the password.

grant select,insert,update,delete on mydb.* to user1@localhost identified by "";

The article is selected from [blog.4ud.cn] 
Fourth trick: 
Log in to mysql by operating the database, and then run the following commands at the mysql prompt, each command ends with a semicolon. 
1. Display the database list.

show databases;

There are two databases by default: mysql and test. The mysql library stores the system and user permission information of mysql. When we change passwords and add users, we actually operate on this library. 
2. Display the data table in the library:

use mysql;
show tables;

3. Display the structure of the data table:

describe 表名;

4. Create and delete database:

create database 库名;
drop database 库名;

5. Create a table:

use 库名;
create table 表名(字段列表);
drop table 表名;

6. Clear the records in the table:

delete from 表名;

7. Display the records in the table:

select * from 表名;

8. Set the encoding

set names utf8
  1. Modify the root user's password; 
    mysql> update mysql.user set password=PASSWORD('new password') where User='root'; 
    mysql> flush privileges; 
    Fifth trick, export and import data
  2. export data:

mysqldump –opt test > mysql.test

即将数据库test数据库导出到mysql.test文件,后者是一个文本文件 
如:mysqldump -u root -p123456 --databases dbname > mysql.dbname 
就是把数据库dbname导出到文件mysql.dbname中。 
2. 导入数据:

source  D:\ceshi.sql  

这是sql文件存放的地址 
文章精选自【blog.4ud.cn】 
操作手册: 
文本数据的字段数据之间用tab键隔开。

use test;
load data local infile "文件名" into table 表名;

1:使用SHOW语句找出在服务器上当前存在什么数据库:

mysql> SHOW DATABASES;

2:2、创建一个数据库MYSQLDATA

mysql> CREATE DATABASE MYSQLDATA;

3:选择你所创建的数据库

mysql> USE MYSQLDATA; (按回车键出现Database changed 时说明操作成功!)

4:查看现在的数据库中存在什么表

mysql> SHOW TABLES;

5:创建一个数据库表

mysql> CREATE TABLE MYTABLE (name VARCHAR(20), sex CHAR(1));

6:显示表的结构:

mysql> DESCRIBE MYTABLE;

7:往表中加入记录

mysql> insert into MYTABLE values ("hyq","M");

8:用文本方式将数据装入数据库表中(例如D:/mysql.txt)

mysql> LOAD DATA LOCAL INFILE "D:/mysql.txt" INTO TABLE MYTABLE;

9:导入.sql文件命令(例如D:/mysql.sql)

mysql>use database;
mysql>source d:/mysql.sql;

10:删除表

mysql>drop TABLE MYTABLE;

11:清空表

mysql>delete from MYTABLE;

12:更新表中数据

mysql>update MYTABLE set sex="f" where name='hyq';

13:重命名表名 
例如,在表MyClass名字更改为YouClass:

mysql> rename table MyClass to YouClass;

14.修改字段名称及属性

mysql> alter table test change t_name t_name_new varchar(20);

15.表插入/添加新的字段

alter table `fy_images` add newColumn varchar(8)  NOT NULL COMMENT '新添加的字段'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324746760&siteId=291194637