Mysql介紹和實踐總結

来源:http://www.cnblogs.com/yate1996/archive/2017/09/13/7513325.html
-Advertisement-
Play Games

本文首先介紹mysql的安裝和基本使用、進階操作、講解mysql的導入導出和自動備份,然後介紹安全模式修改密碼和mysql的全文本搜索功能,最後記錄了個人使用mysql中遇到的問題集。 開始安裝: 簡單使用 建庫 建表 增 刪 改 查 顯示所有的view 進一步操作 創建用戶: 重命名: 刪除用戶: ...


本文首先介紹mysql的安裝和基本使用、進階操作、講解mysql的導入導出和自動備份,然後介紹安全模式修改密碼和mysql的全文本搜索功能,最後記錄了個人使用mysql中遇到的問題集。

開始安裝:

sudo apt-get install mysql-common mysql-server

簡單使用

建庫

CREATE DATABASE IF NOT EXISTS yourdbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

建表

create table MyClass(id int(4) not null primary key auto_increment,name char(20) not null,sex int(4) not null default '0',degree double(16,2));

insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);

delete from MyClass where id=1;

update MyClass set name='Mary' where id=1;

select * from MyClass;

顯示所有的view

select * from information_schema.TABLES where table_type='view' AND table_schema = '資料庫名';

進一步操作

創建用戶:

create user xxx identified by ‘password’;

重命名:

rename user aaa to bbb;

刪除用戶:

drop user aaa;

顯示許可權:

show grants for aaa(用戶);

授予許可權:

grant select on xxx(資料庫).* to aaa(用戶);

授予某個資料庫的全部許可權:

grant all on  xxx(資料庫).* to aaa(用戶);
grant all on  xxx(資料庫).* to aaa(用戶)@localhost;

取消授權:

revoke all on *.* from aaa(用戶)@localhost;

修改許可權

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%’   WITH GRANT OPTION;

以上操作完成之後記得刷新許可權:

flush privileges;

導入導出

導出數據和表結構:

mysqldump -uroot -p abc(資料庫名) > abc.sql
敲回車後輸入密碼

只導出表結構

mysqldump -uroot -p -d abc > abc.sql

導入資料庫
1、首先建空資料庫

mysql> create database abc;

2、導入資料庫

mysql -u root -p abc(資料庫名) < abc.sql

資料庫自動備份

新建備份腳本xxx.sh,輸入以下內容

#!/bin/bash

# 要備份的資料庫名,多個資料庫用空格分開
databases=("db1", "db2") 

# 備份文件要保存的目錄,註意當前用戶必須用戶保存目錄的讀寫許可權
basepath='/root/backup/mysql/'

if [ ! -d "$basepath" ]; then
  mkdir -p "$basepath"
fi

# 迴圈databases數組
for db in ${databases[*]}
  do
    # 備份資料庫生成SQL文件
    nice -n 19 /usr/bin/mysqldump -uroot -pcd32d5e86e --database $db > $basepath$db-$(date +%Y%m%d).sql
    
    # 將生成的SQL文件壓縮
    nice -n 19 tar zPcf $basepath$db-$(date +%Y%m%d).sql.tar.gz -C $basepath $db-$(date +%Y%m%d).sql
    
    # 刪除7天之前的備份數據
    find $basepath -mtime +7 -name "*.sql.tar.gz" -exec rm -rf {} \;
  done

  # 刪除生成的SQL文件
  rm -rf $basepath/*.sql

使用crontab設置定時任務,在終端輸入crontab -e,加入以下內容,此任務為每天3點自動執行。

0 3 * * * bash xxx.sh(此處填寫腳本絕對地址)

開啟日誌記錄

[mysqld]
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
max_binlog_size = 1000M
binlog-format = row

安全模式操作

進入安全模式修改密碼

mysqld_safe --skip-grant-tables &

select user,host,password from user where user="root"

不同版本的mysql修改用戶密碼方式不一樣,需要查看mysql->user中的密碼欄位,如果不是password的話就是authentication_string。

authentication_string的修改方式不太一樣:

use mysql;
update user set authentication_string=PASSWORD("") where User='root';
update user set plugin="mysql_native_password";
flush privileges;
quit;
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start

如果不是authentication_string,則可用以下方法。

update user set password=PASSWORD("your_password") where user="root" and host=“localhost"

新操作

Mysql全文本搜索

Mysql5.6之後支持InnoDB,中文的全文本搜索,內置使用n-gram為分詞處理器,還支持中文~。

創建索引

create fulltext index ngram_idx on tag(Title) with parser ngram;
或
alter table tag add fulltext index ngram_idx(Title) with parser ngram;

獲取支持的最小分詞長度

SHOW VARIABLES LIKE 'ft_min_word_len';
//unix系統可在/etc/my.cnf中修改
[mysqld]
ft_min_word_len = 1

開始使用

select Title,match(Title) against('清水') from tag ;

可能出現的問題集:

  • 描述
2017-05-04T01:21:32.004560Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-05-04T01:21:32.023009Z mysqld_safe A mysqld process already exists

解決方法:

$ sudo killall mysqld
  • 描述
2017-05-04T01:22:26.486677Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-05-04T01:22:26.488204Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.

解決方法:

sudo mkdir -p /var/run/mysqld
sudo chown -R mysql:mysql /var/run/mysqld
  • 描述
$ sudo /etc/init.d/mysql start
ies: No such file or directory
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
[....] Starting mysql (via systemctl): mysql.servicejob-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.

解決方法:

當前文件夾不是實際目錄導致
cd到一個實際目錄位置即可
  • 描述
sudo /etc/init.d/mysql start
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
[....] Starting mysql (via systemctl): mysql.servicejob-working-directory: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.
按照提示:See "systemctl status mysql.service" and "journalctl -xe" for details.
但是並麽有什麼卵用,直接看mysql的log:/var/log/mysql/error.log
2017-05-04T01:37:56.583745Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.

解決方法:

殺掉所有mysqld進程:killall mysqld 
再次sudo /etc/init.d/mysql start 成功
  • 描述
dpkg被鎖定

解決方法

sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、編譯生成FFmpeg的頭文件和so庫 如果不知道怎麼做可以看這裡:http://blog.csdn.net/qsw15923/article/details/77943515 二、新建項目 在新建時勾選下方的include C++support,然後一路下一步即可 創建完項目後如果之前沒有配置N ...
  • 【新版】Android技術博客精華彙總(原文鏈接內持續更新) http://www.apkbus.com/thread-313856-1-1.html Kotlin Kotlin學習資料彙總 http://www.apkbus.com/blog-261991-68033.html 使用Kotlin來 ...
  • Android精選源碼 Android優質博客 從零開始搭建一個項目(rxJava+Retrofit+Dagger2) 下http://www.apkbus.com/blog-873057-72599.htmltip:本文所使用到的技術有 RxJava,Retrofit,Glide,Dagger2, ...
  • FFmpeg是很好用的一個音視頻庫,功能強大,但是用起來並不是很方便。之前一直不想用FFmpeg,因為感覺編譯太麻煩,但是到了不得不用的時候了,沒辦法,參考了網上大神的方法,在這裡自己也記錄一下方便以後再次查看。 一、環境 Ubuntu14.04 二、NDK環境配置 NDK下載鏈接:https:// ...
  • 當我們使用Xcode進行開發的時候,並不是所有的時候都需要將代碼運行在iPhone,有時候模擬器就可以解決這些問題, 但是當你使用模擬器的時候會發現,在TextFiled中輸入信息時,如果你是用模擬器上的鍵盤進行操作,OK沒問題, 但是當你是用鍵盤輸入信息的話,那麼你會發現模擬器上的鍵盤就不會再顯示 ...
  • 那麼 Appium 到底是怎麼工作的呢? Appium 官方教程好難啊, 我自己總結了一份超簡單 Appium 教程。 ...
  • TTS語音合成方案分為離線合成方案和線上合成方案,離線合成方案省去網路請求,合成速度更快,節省網路流量,但是合成音的聽起來比較機械,語速和停頓的處理較差一些。如果對合成音的效果要求不是特別高,可以考慮採用iOS自帶的AVSpeechSynthesis框架,免去語音庫的合入,減少安裝包大小。 ...
  • 1、查找表中多餘的重覆記錄,重覆記錄是根據單個欄位(peopleId)來判斷 select * from peoplewhere peopleId in (select peopleId from people group by peopleId having count (peopleId) > ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...