數(shù)據(jù)庫

sudo service mysql start
mysql -u root
CREATE DATABASE shiyanlou;
use mysql;
create user shiyanlou IDENTIFIED by 'shiyanlou';
#select host,user,password from user;
grant ALL privileges on shiyanlou.* to shiyanlou identified by 'shiyanlou';
flush privileges;
#show grants for shiyanlou;

中文csv導(dǎo)入數(shù)據(jù)庫

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

use shiyanlou;

#show character set;

/*********table course********/
create table course (  
    id  integer not null,  
    name varchar(64) not null,  
    primary key (id)  
) default character set utf8mb4;  

load data infile '/home/shiyanlou/loudatabase/shiyanlou_course.csv'  
into table course character set utf8mb4
fields terminated by ','  optionally enclosed by '"' escaped by '"'   
lines terminated by '\n';

#select * from course;

/*********table user********/
create table user (  
    id  integer not null,  
    name varchar(64) not null,  
    primary key (id)  
) default character set utf8mb4;  

load data infile '/home/shiyanlou/loudatabase/shiyanlou_user.csv'  
into table user character set utf8mb4
fields terminated by ','  optionally enclosed by '"' escaped by '"'   
lines terminated by '\n';

/********table usercourse*********/
create table usercourse (  
    user_id  integer not null,  
    course_id  integer not null,  
    study_time integer not null,
    foreign key(user_id) references user(id), 
    foreign key(course_id) references course(id)
) default character set utf8mb4;  

load data infile '/home/shiyanlou/loudatabase/shiyanlou_usercourse.csv'  
into table usercourse character set utf8mb4
fields terminated by ','  optionally enclosed by '"' escaped by '"'   
lines terminated by '\n';
 # 在第一列增加一列自動(dòng)增長(zhǎng)的id列
alter table usercourse add COLUMN id INT NOT NULL AUTO_INCREMENT PRIMARY KEY first;


學(xué)習(xí)時(shí)間最長(zhǎng)

#select user.name,course.name,study_time from usercourse,course,user where usercourse.course_id = course.id and user.id = usercourse.user_id group by user_id group by user.name,course.name;


create table favorite select user.name as user_name ,course.name as course_name,max(study_time) as study_time from usercourse,course,user where usercourse.course_id = course.id and user.id = usercourse.user_id group by user.name;

alter table favorite add COLUMN id INT NOT NULL AUTO_INCREMENT PRIMARY KEY first;


查詢慢

alter table usercourse add index user_Index(user_id);

alter table usercourse add index studytime_Index(study_time);

備份和恢復(fù)


/*************************/
crontab -u shiyanlou –e
#每個(gè)星期一凌晨3:00完全備份
0 3 * * 1 /usr/bin/mysqldump --opt --force -uroot shiyanlou> /usr/lib/mysql/"mysql-`date +'%Y%m%d-%H:%M:%S'`".sql
#周二到周天凌晨3:00增量備份
0 3 * * 2-7 mysqladmin -uroot flush-logs

mysqlbackup.sh

# /bin/bash  
DB_NAME="****"  
DB_USER="****"  
DB_PASS="****"  
BIN_DIR="/usr/bin"  
BACK_DIR="/data/backdata"  
DATE="mysql-`date +'%Y%m%d-%H:%M:%S'`"  
LogFile="$BACK_DIR"/dbbakup.log #日志記錄保存的目錄  
BackNewFile=$DATE.sql  
$BIN_DIR/mysqldump --opt --force -u$DB_USER  -p$DB_PASS $DB_NAME > $BACK_DIR/$DATE.sql  
echo ----------"$(date +"%y-%m-%d %H:%M:%S")"------------ >> $LogFile  
echo  createFile:"$BackNewFile" >> $LogFile  
#find "/data/backdata/" -cmin +1 -type f -name "*.sql" -print > deleted.txt  
find "/data/backdata/" -ctime +7 -type f -name "*.sql" -print > deleted.txt  
echo -e "delete files:\n" >> $LogFile  
#循環(huán)刪除匹配到的文件  
cat deleted.txt | while read LINE  
do  
rm -rf $LINE  
echo $LINE>> $LogFile  
done  
echo "---------------------------------------------------------------" >> $LogFile  
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容