目录
1. 设置、更改 root 密码
2. 连接 MySQL
3. MySQL 常用命令
4. MySQL 用户管理
5. MySQL 常用语句
6. MySQL 的备份与恢复
[root@LNMP ~]# vim .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin:/usr/local/mysql/bin
export PATH
[root@LNMP ~]# source .bash_profile
[root@LNMP ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
[root@LNMP ~]# mysqladmin -uroot password '123123';
Warning: Using a password on the command line interface can be insecure.
[root@LNMP ~]# mysql -uroot -p123123
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
[root@LNMP ~]# mysqladmin -uroot -p'123123' password '456456'
Warning: Using a password on the command line interface can be insecure.
[root@LNMP ~]# mysql -uroot -p456456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
[root@LNMP ~]# vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
skip-grant # 加入该行
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
socket = /tmp/mysql.sock
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[root@LNMP ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@LNMP ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> # 成功登录
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set password=password('123123') where user='root';
Query OK, 4 rows affected (0.02 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql -uroot -p123123
mysql -uroot -p123123 -h192.168.1.1 -P3306
mysql -uroot -p123123 -S/tmp/mysql.sock
mysql -uroot -p123123 -e "show databases"
show databases;
show tables;
desc tb_name;
show create table tb_name\G; ## \G 代表竖排显示
select user();
select databsase();
create database db1;
use db1; create table t1(
idint(4),
namechar(40));
use db1; create table t1(
idint(4),
namechar(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
select version();
show status;
show variables; show variables like 'max_connect%';
set global max_connect_errors=1000; # 仅为临时修改,若要永久修改需配置 my.conf 文件
show processlist;
show full processlist;
grant all on *.* to 'user1' identified by 'passwd';
grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.133.1' identified by 'passwd';
grant all on db1.* to 'user3'@'%' identified by 'passwd';
show grants;
show grants for [email protected];
select count(*) from mysql.user;
select * from mysql.db;
select db from mysql.db;
select db,user from mysql.db;
select * from mysql.db where host like ‘192.168.%’;
insert into db1.t1 values (1, ‘abc’);
update db1.t1 set name=’aaa’ where id=1;
truncate table db1.t1;
drop table db1.t1;
drop database db1;
mysqldump -uroot -p123456 mysql > /tmp/mysql.sql
mysql -uroot -p123456 mysql < /tmp/mysql.sql
mysqldump -uroot -p123456 mysql user > /tmp/user.sql
mysql -uroot -p123456 mysql < /tmp/user.sql
mysqldump -uroot -p -A >/tmp/123.sql
mysqldump -uroot -p123456 -d mysql > /tmp/mysql.sql