Mysql remote login and common commands

From: http://www.cnblogs.com/good_hans/archive/2010/03/29/1700046.html

The first trick, the start and stop of the mysql service

   net stop mysql

   net start mysql

The second trick, log in to mysql

   The syntax is as follows: mysql -u username -p user password

   Type the command mysql -uroot -p, after pressing Enter, you will be prompted to enter the password, enter 12345, and then press Enter to enter mysql. The prompt of mysql is:

mysql>

Note that if you are connecting to another machine, you need to add a parameter -h machine IP

For example:  mysql -h172.16.38.227 -uhuagang -p

The third measure, adding new users

   Format: grant permission on database.* to username@login host identified by "password"

   For example, add a user user1 with a password of password1, so that it can log in on the local machine and have the rights to query, insert, modify, and delete all databases. 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 fourth trick: operating the database

   Log in to mysql and run the following commands at the mysql prompt, ending each command with a semicolon.

1. Display the database list.

show databases;

缺省有两个数据库:mysql和test。 mysql库存放着mysql的系统和用户权限信息,我们改密码和新增用户,实际上就是对这个库进行操作。

2、 显示库中的数据表:

   use mysql;

   show tables;

3、 显示数据表的结构:

    describe 表名;

4、 建库与删库:

   create database 库名;

   drop database 库名;

5、 建表:

   use 库名;

  create table 表名(字段列表);

  drop table 表名;

6、 清空表中记录:

  delete from 表名;

7、 显示表中的记录:

  select * from 表名;

 

第五招、导出和导入数据

1. 导出数据:

   mysqldump --opt test > mysql.test

即将数据库test数据库导出到mysql.test文件,后者是一个文本文件

如:mysqldump -u root -p123456 --databases dbname > mysql.dbname

就是把数据库dbname导出到文件mysql.dbname中。

2. 导入数据:

   mysqlimport -u root -p123456 < mysql.dbname。

不用解释了吧。

3. 将文本数据导入数据库:

文本数据的字段数据之间用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';

  posted on 2006-01-10 16:21 happytian 阅读(6) 评论(0) 编辑 收藏 收藏至365Key

13:备份数据库

  mysqldump -u root 库名>xxx.data

14:例2:连接到远程主机上的MYSQL

假设远程主机的IP为:110.110.110.110,用户名为root,密码为abcd123。则键入以下命令:

  mysql -h110.110.110.110 -uroot -pabcd123                       // 远程登录

(注:u与root可以不用加空格,其它也一样)

3、退出MYSQL命令: exit (回车)

 

Guess you like

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