Ubuntu Mysql 常用指令

时间:2021-06-08 07:44:50

mysql 常用指令及中文乱码解决

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* TABLES
=============================================================================*/

table th {
font-weight: bold;
}

table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}

table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}

table tr:nth-child(2n) {
background-color: #f8f8f8;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

Mysql 系统管理指令

  • 登陆本地 Mysql

mysql -u username -p # 回车输入密码

或者 mysql -u username -p passswd;

  • 登陆远程 Mysql

mysql -h address -u username -p # 回车输入密码

或者 mysql -h address -u username -p passswd;

  • 修改新密码

mysql> use mysql;

mysql> update user set password=PASSWORD(‘newpasswd’) where user=’username’;

mysql> flush privileges; #更新权限

mysql> quit; #退出

  • 增加新用户

mysql> grant select on database_name.* to user@address identified by ‘passwd

ex: 增加一个用户 test1 密码为 abc,让他可以在任何主机上登录,并对所有数据库有 查询、插入、修改、删除的权限。

  1. mysql> grant select,insert,update,delete on . to root@localhost identified by 'mysql';
  2. or mysql> grant all privileges on . to root@localhost identified by 'mysql';
  3. mysql> flush privileges;

ex: 不希望 root 有密码操作数据库“mydb”里的数据表,可以再打一个命令将密码消掉

  • mysql> grant select,insert,update,delete on mydb.* to root@localhost identified by '';
  • 删除用户

mysql> delete from user where user='user_name' and host='address';

mysql> flush privileges;

mysql> drop database dbname; #删除用户的数据库

数据库导入导出(mysqldump)

  • 导出所有数据库

mysqldump -u user -p -A > backup_file_path

  • 导出数据和数据结构

mysqldump -u user -p database_name_need_to_backup > backup_file_path

ex00: export database mydb

mysqldump -h localhost -u root -p mydb > ./mydb.sql

ex01: export database mydb mytable

mysqldump -h localhost -u root -p mydb mytable > ./mytable.sql

ex02: export database mydb framework

mysqldump -h localhost -u root -p mydb –add-drop-table > ./mydb_stru.sql

  • 只导出数据不导出数据结构

mysqldump -u user -p -t database_name_need_to_backup > backup_file_path

  • 导出数据库中的Events

mysqldump -u user -p -E database_name_need_to_backup > backup_file_path

  • 导出数据库中的存储过程和函数

mysqldump -u user -p -R database_name_need_to_backup > backup_file_path

  • 从外部文件导入数据库中
  • 使用“source”命令

mysql > source path_of_backup_file

  • 使用“<”符号

mysql -u root –p < path_of_backup_file

Mysql 常用指令

  • 查看所有数据库

mysql> show databases;

  • 选择要操作数据库

mysql> use database_name;

  • 查看当前数据库下所有表

mysql> show tables;

  • 获取表结构

mysql> desc table_name;

或者 mysql> show columns from table_name;

  • 创建一个数据库

mysql> create database database_name;

  • 删除一个数据库

mysql> drop database database_name;

  • 创建一个表

mysql> create table table_name( uid bigint(20) not null, uname varchar(20) not null);

  • 删除一个表

mysql> drop table table_name;

  • SQL插入语句

mysql> insert into table_name(col1, col2) values(value1, value2);

  • SQL更新语句

mysql> update tablename set col1='value1', col2='value2' where wheredefinition;

  • SQL查询语句

mysql> select * from table_name where....... #(最复杂的语句)

  • SQL删除语句

mysql> delete from table_name where...

  • 增加表结构的字段

mysql> alert table table_name add column field1 date, add column field2 time ...

  • 删除表结构的字段

mysql> alert table table_name drop field1;

  • 查看表的结构

mysql> show columns from table_name;

  • limit 的使用

mysql> select * from table_name limit 3; #每页只显示3行

mysql> select * from table_name limit 3,4; #从查询结果的第三个开始,显示四项结果。 此处可很好的用来作分页处理。

  • 对查询结果进行排序

mysql> select * from table_name order by field1, orderby field2; #多重排序

  • 查看字符编码

mysql> show variables like 'character%' ;

Ubuntu mysql中文乱码解决

  • 打开配置文件

sudo vim /etc/mysql/my.cnf

  • 找到[mysqld]添加

character-set-server = utf8

  • 重启mysql

/etc/init.d/mysql restart 或者 service mysql restart