MySQL:Useful Commands
MySQL Useful Commands
Start/Stop/Restart MySQL
On Linux start/stop/restart from the command line:
/etc/init.d/mysqld start
/etc/init.d/mysqld stop
/etc/init.d/mysqld restart
Some Linux flavours offer the service command too
service mysqld start
service mysqld stop
service mysqld restart
or
service mysql start
service mysql stop
service mysql restart
On OS X to start/stop/restart MySQL from the command line:
sudo /usr/local/mysql/support-files/mysql.server start
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server restart
Connect
mysql -h localhost -u myname -pMyPassword
Set Password
First set: mysqladmin -u root -password root
then:
mysql> UPDATE mysql.user SET password=PASSWORD(’新密码’) WHERE User=’root’;
mysql> FLUSH PRIVILEGES;
Show current user
mysql> select user();
Create Database
mysql> create database openfire character set 'utf8';
Show Databases
mysql> show databases;
Use Database
mysql> use database;
Show Tables
mysql> show tables;
Empty database with root user
mysql> DROP DATABASE atomstore;
mysql> CREATE DATABASE atomstore;
Create User
create user 'hy'@'%' identified by 'M@nager13579'
Grant Permission
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
GRANT ALL PRIVILEGES ON *.* TO 'appuser'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Show table structure
SHOW [FULL] COLUMNS {FROM | IN} tbl_name [{FROM | IN} db_name]
[LIKE 'pattern' | WHERE expr]
Drop database
DROP DATABASE atomstore;
Drop table
DROP TABLE xxxxx;
Create table samples
create table if not exists `fchat`.`user` (
`userId` int(7) not null auto_increment,
`username` varchar(64) not null,
`plainPassword` varchar(32) null,
`encryptedPassword` varchar(255) null,
`name` varchar(100) null,
`email` varchar(100) null,
`creationDate` datetime not null,
`modificationDate` datetime not null,
primary key(`userId`)
);
create table if not exists `fchat`.`tag` (
`tagId` int(5) not null auto_increment,
`tagName` varchar(64) not null,
`userId` int(7) not null,
primary key(`tagId`),
foreign key (userId)
references user(userId)
on delete cascade
);
Rename Table
RENAME TABLE tbl_name TO new_tbl_name
Create Unique Index
create unique index idx_userName on user (userName);
Show Columns Character
show full columns from ofOffline;
Show Character Set
SHOW VARIABLES LIKE 'char%';
Change Client Character
set names utf8;
Backup database
mysqldump -h 192.168.3.32 -u root -p[root_password] [database_name] > dumpfilename.sql
Backup all the databases
mysqldump -u root -ptmppassword --all-databases > /tmp/all-database.sql
Backup a specific table
mysqldump -u root -ptmppassword sugarcrm accounts_contacts \
> /tmp/sugarcrm_accounts_contacts.sql
Restore a database
# mysql -u root -ptmppassword
mysql> create database sugarcrm;
# mysql -u root -ptmppassword sugarcrm < /tmp/sugarcrm.sql
# mysql -u root -p[root_password] [database_name] < dumpfilename.sql
Backup a local database and restore to remote server using single command
mysqldump -u root -ptmppassword sugarcrm | mysql \
-u root -ptmppassword --host=remote-server -C sugarcrm1
根据日期分组统计注册数量
select Date(createDate) date, count(1) count from users where createDate between '2015-07-01 00:00:00' and '2015-07-14 23:59:59' group by Date(createDate);
导出csv文件
mysql -h <host name> -u<user name> -p<password> <database name> -N -B -e "select user.user_id, user.nick_name, user.hs_no, online.online_status, online.create_date from t_user_online_history online, (select user_id, nick_name, hs_no from t_user_base_info where create_date > '2015-09-07 19:00:00') user
where online.user_id = user.user_id" | tee ~/Documents/output.csv
创建临时表
CREATE TEMPORARY TABLE temp1 ENGINE=MEMORYBUT
as (select * from table1)ENGINE=MEMORYis not supported when table containsBLOB/TEXTcolumns Mac
MariaDB Server can be installed with this command:
brew install mariadb
After installation, start MariaDB Server:
mysql.server start
To auto-start MariaDB Server, use Homebrew's services functionality, which integrates with macOS launchctl:
brew services start mariadb
After MariaDB Server is started, you can log in:
mysql -u root
Export All Databases:
mysqldump -u root -p --all-databases > alldb.sql
Look up the documentation for mysqldump. You may want to use some of the options mentioned in comments:
mysqldump -u root -p --opt --all-databases > alldb.sql
mysqldump -u root -p --all-databases --skip-lock-tables > alldb.sql
Import:
mysql -u root -p < alldb.sql
MySQL:Useful Commands的更多相关文章
- MySQL:动态开启慢查询日志(Slow Query Log)
前言 在开发中,高效能的程序 也包括 高效能的查询,所以优化SQL也是程序员必要技能之一.要优化就必须要有慢日志记录才可以知道哪些查询慢,然后反向去修改 慢日志设置方式 写入文件 写入数据库 实践操作 ...
- MySQL:(一)
数据库概述 什么是数据库 数据库是一个文件系统.通过标准SQL语言操作文件系统中数据——用来存放软件系统的数据! SQL:Structured Query Language 结构查询语言 常用数据库简 ...
- MySQL:windows中困扰着我们的中文乱码问题
前言:什么是mysql中的中文乱码问题? 话不多说,直接上图 这个东西困扰了我好久,导致我现在对windows映像非常不好,所以就想改成Linux,行了,牢骚就发到这里,直接说问题,明眼人一眼就看出来 ...
- MySQL:进阶之视图函数
一,视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. 使用视图我们可以把查询过程中的临时 ...
- MySQL:Your password has expired. To log in you must change it using a client that supports expired passwords
MySQL:V5.6.37 安装后发现没远程权限,为了方便,就直接把hostname@root修改为%@root,密码修改为和localhost@root一样 然后尴尬的事情发生了,本地登陆正常,远程 ...
- Mysql:数据库导入导出
Mysql:数据库导入导出 Mysql数据库导出 mysqldump -h IP -u 用户名 -p 数据库名 > 导出的文件名 1.mysqldump是在cmd下的命令,需要在linux命令行 ...
- mysql:视图,触发器,事务,存储过程,函数。
一 视图 1 什么是视图:视图其实就是通过查询得到一张表并且保存下来,就是一张虚拟的表,并非真实存在,比如我们将两个表在终端通过(inner join)内链接起来,那么我们得到的这个表就叫做视图,其 ...
- Mysql:MyIsam和InnoDB的区别
MyISAM: 这个是默认类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Access Method (有索引的 顺序访问方法) 的缩写,它是存储记录和文件的标准方法 ...
- MySQL:SQL进阶
一.数据库相关理论 1.系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等performance_s ...
随机推荐
- [Swift通天遁地]六、智能布局-(7)通过Group(组)命令实现对多个视图的统一约束
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 微信小程序商品展示页面(仿咸鱼)
项目中做了一个商品发布展示的页面,记录下来 解决问题: 想在setData中更改数组具体下标中的某个值: let one = "lowMoney[" + 0 + "].m ...
- Java中JPS命令监控
很多人在学习java的时候只是对java粗略的学了一遍,很少有人能了解jvm层面的一些东西,比如我们想看目前有多少个java进程,可以在命令行执行jps.下面我们来说说jps的一些详细的用法. jps ...
- [转]Linux命令之iconv
转自:http://lorna8023.blog.51cto.com/777608/420313 用途说明 iconv命令是用来转换文件的编码方式的(Convert encoding of given ...
- Spark RDD/Core 编程 API入门系列之简单移动互联网数据(五)
通过对移动互联网数据的分析,了解移动终端在互联网上的行为以及各个应用在互联网上的发展情况等信息. 具体包括对不同的应用使用情况的统计.移动互联网上的日常活跃用户(DAU)和月活跃用户(MAU)的统计, ...
- Task.Run 和 Task.Factory.StartNew
在.Net 4中,Task.Factory.StartNew是启动一个新Task的首选方法.它有很多重载方法,使它在具体使用当中可以非常灵活,通过设置可选参数,可以传递任意状态,取消任务继续执行,甚至 ...
- JVM 内存分配和垃圾回收(GC)机制
一 判断对象是否存活 垃圾收集器在对堆进行回收前,第一件事情就是要确定这些对象之中哪些还“活着”,哪些已经"死去”,即不能再被任何途径使用的对象. 1.1 引用计数法 (Reference ...
- 线性回归的Cost function实现
此处使用Octave来实现 线性方程的代价函数: 代价函数: X 是测试值,假设用矩阵表示为 为了方便用矩阵计算我们把X加一列 1 : 同时 那么h( ...
- Linux(centOS7.2)+node+express初体验
赶着阿里云服务器老用户服务器半折的好时机,手痒买了一个低配. 想着对于低配用Linux应该比较好(无可视化界面) 于是选择安装了centOs7.2: 我是通过SecureCRT进行远程连接的(如何操作 ...
- Angular——流程控制指令
基本介绍 (1)ng-repeat,类似于for循环,对数组进行遍历 (2)ng-switch on,ng-switch-when,类似于switch,case 基本使用 ng-repeat < ...