mysql使用手册
1、mysql登录
1.1 远程登陆mysql
mysql -h ip -u root -p 密码
1.2 本地登陆mysql
mysql -u root -p 密码
2、为数据库配置远程连接权限
采用授权法
例如:以用户名:root 密码:888888从任何主机连接到mysql服务器
先在服务器上用root用户登录:
mysql –uroot–p
出现命令行提示符:
mysql>
输入:
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '1qaz2wsx' WITH GRANT OPTION;
然后使配置生效:
mysql>FLUSH PRIVILEGES;
mysql>exit;
如果仅仅允许用户root 从ip为192.168.1.6的主机连接到mysql服务器,并使用888888作为密码
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.6' IDENTIFIED BY '888888' WITH GRANT OPTION;
mysql>FLUSH PRIVILEGES;
mysql>exit;
3、用户使用权限
mysql>use mysql;
Database changed
mysql>select host, user, password from user;
+-----------+---------+-------------------------------------------+
| host | user | password |
+-----------+---------+-------------------------------------------+
| localhost | root | *535CC5FDC76618DED475584E5F5957B9F7E63D4B |
| 127.0.0.1 | root | *7C057B0B526E12B8D3AFBD4B9C187CA3E62DA280 |
| ::1 | root | *7C057B0B526E12B8D3AFBD4B9C187CA3E62DA280 |
| % | phptest | *523DF7DC12DF59EC98AD12A7125000A5B4E63721 |
| % | root | |
| % | mysqld | *83D34C89B8E0F100D54C6D9276D357DB43E8779F |
| % | server | *866D5A029D62EC05ACC4584CE50F1CD2F50E0E82 |
+-----------+---------+-------------------------------------------+
7 rows in set (0.00 sec)
4、用户登陆及密码更改
#修改当前登录用户密码:
mysql>SET PASSWORD = PASSWORD('1qaz2wsx');
mysql>exit;
#添加ydlm用户查询、插入、更新、删除的权限:
grant select,insert,update,delete on *.* to test@"%" identified by "123456";
grant select,insert,delete,update,create,drop on *.* to test@"%" identified by "123456";
flush privileges;
5、限制root用户远程登陆
mysql> delete from user where user="root" and host="%";
mysql> flush privileges;
mysql>exit;
#启用root用户远程登陆
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '1qaz2wsx' WITH GRANT OPTION;
然后使配置生效:
mysql>FLUSH PRIVILEGES;
mysql>exit;
6、删除某用户
drop user 用户名@'%';
drop user ydlm@'%';
DELETE FROM user WHERE user='test';
或者
drop user test@"%";
delete from user Where User='test' and Host='%';#指定用户和主机名
7、数据库导入
source /opt/test.dump_20171124_112640.sql
8、数据库的备份与恢复
—备份数据库
—/var/lib/mysql
—备份
—mysqldump -u 用户名 -p [密码] [选项] [数据库名] [表名] > /备份路径/备份文件名 ,选项有 --all-databases 表示备份所有数据库
mysqldump -u root -p studb > /studb.sql
mysqldump -uroot -p --all-databases >/studb.sql #不建议用,恢复麻烦
—还原数据库
mysql -u root -p studb < /studb.sql
9、mysql数据库使用
mysql> show databases;查看数据库列表信息
mysql> use mysql;USE 数据库名
mysql>show tables;
—创建新的数据库
—CREATE DATABASE 数据库名
—创建新的数据表
—CREATE TABLE 表名 (字段定义……)
—删除指定的数据表
—DROP TABLE [数据库名.]表名
—删除指定的数据库
—DROP DATABASE 数据库名
—建表:
—create table student (表名称是student
—studentName varchar(30) not null,字段studentName
—studentId int not null,字段studentId
—studentAge int not null,字段studentAge
—loginPass varchar(10)字段loginPass
—);
10、重置mysql数据库root用户密码
1、修改MySQL的登录设置:
vi /etc/my.cnf
在[mysqld]的段中加上一句:skip-grant-tables
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-name-resolve
skip-grant-tables
2、重新启动mysqld:
/etc/init.d/mysqld restart
3、登录并修改MySQL的root密码:
/usr/bin/mysql
USE mysql ;
UPDATE user SET Password = password (‘new-password’) WHERE User =‘root’
flushprivileges ;
Quit;
4、再把配置文件修改回来,再重启服务
11、Mysql备份脚本
#!/bin/sh
# File: /root/mysql_dump/mysql_back.sh
# Database info
DB_NAME="test"
DB_USER="root"
DB_PASS="123456"
# Others vars
# whereis mysqldump
# IS ` but not '
BIN_DIR="/usr/bin"
BCK_DIR="/root/mysql_dump/data"
DATE=`date +%Y%m%d_%H%M%S`
# TODO DATABASE BAK AND DELETE 15 DAYS AGO BAKFILES
mkdir -p $BCK_DIR
$BIN_DIR/mysqldump --opt -u$DB_USER -p$DB_PASS $DB_NAME \
> $BCK_DIR/$DB_NAME.dump_$DATE.sql
find $BCK_DIR -mtime +15 -name "test.dump_*" -exec rm -rf {} \;