mysql使用手冊php
一、mysql登陸
1.1 遠程登錄mysql
mysql -h ip -u root -p 密碼mysql
1.2 本地登錄mysql
mysql -u root -p 密碼sql
二、爲數據庫配置遠程鏈接權限
採用受權法數據庫
例如:以用戶名:root 密碼:888888從任何主機鏈接到mysql服務器 服務器
先在服務器上用root用戶登陸:socket
mysql –uroot –pide
出現命令行提示符:ui
mysql>spa
輸入:命令行
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;
三、用戶使用權限
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)
四、用戶登錄及密碼更改
#修改當前登陸用戶密碼:
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;
五、限制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;
六、刪除某用戶
drop user 用戶名@'%';
drop user ydlm@'%';
DELETE FROM user WHERE user='test';
或者
drop user test@"%";
delete from user Where User='test' and Host='%'; #指定用戶和主機名
七、數據庫導入
source /opt/test.dump_20171124_112640.sql
八、數據庫的備份與恢復
— 備份數據庫
— /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
九、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
— );
十、重置mysql數據庫root用戶密碼
一、修改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
二、從新啓動mysqld:
/etc/init.d/mysqld restart
三、登陸並修改MySQL的root密碼:
/usr/bin/mysql
USE mysql ;
UPDATE user SET Password = password ( ‘new-password’ ) WHERE User = ‘root’
flush privileges ;
Quit;
四、再把配置文件修改回來,再重啓服務
十一、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 {} \;