01 . Mysql簡介及部署

Mysql數據庫簡介

什麼是數據?

​ 數據(data)是事實或觀察的結果,是對客觀事物的邏輯概括,是用於表示客觀事物的未經加工的原始素材,數據是信息的表現形式和載體,能夠是符號,文字,數字,語音,圖像,視頻等,數據和信息是不可分離的,數據是信息的表達,信息是數據的內涵,數據自己沒有任何意義,數據只有對實體行爲產生影響才成爲信息。在計算機系統中,數據以二進制信息單元0,1形式表示java

什麼是數據庫?

​ 數據庫(Database): 是按照數據結構來組織、存儲和管理數據的倉庫,給咱們提供了一種以關係的方式來存放數據的方法,可以解決文本性存儲數據的劣勢。node

數據庫分類

關係型數據庫

庫中有表,幾個表之間有關聯的,擁有共同的列,稱之爲關係型數據庫python

Oracle						# 1521
DB2							# 5000
Mysql,Mariadb				# 3306
SQLServer					# 1433
非關係型數據庫

一般是以Key-value形式存儲的,不支持SQL語句,沒有表結構,配置簡單,低廉學習成本,能很好做爲Mysql中間層:mysql

# 1. 鍵值存儲:  Redis<6379>  Memcached<11211>,由於相比其餘數據存儲沒有數據結構,又工做在內存中,因此性能很高.
# 2. 列式存儲:  HBase:
# 3. 文檔存儲:  Documentation ,  MongoDB<27017>

分佈式數據庫: 經過分片機制進行數據分佈,每一個節點都能接收客戶端請求<去中心化>,而且持有全局元數據的一部分數據.linux

# Hadoop(HDFS):  適用於大文件存儲,Apache公司的產品,java程序編寫
# FastDFS(開源軟件): 適用於小文件存儲(網盤,短視頻,images),對於高併發有很好的支持.

SQL介紹:

# SQL語言主要用於存取數據,查詢數據,更新數據和管理關係數據庫系統,由IBM開發,分爲四種類型
# DDL語句        數據庫定義語言(Create,Alter,Drop,Declare)  
# 用於定義或改變表的結構,數據類型,表之間的鏈接和約束等初始化工做上,他們大多在建表時使用.

# DML語句       數據庫操做語言(Select,Delete,Update,Insert)    # 用來對數據庫裏的數據進行操做的語言.

# DCL語句        數據庫控制語言(GRANT,REVOKE,COMMIT,ROLLBACK)  
# 用來設置或更改數據庫或角色權限的語句,只有sysadmin,dbcreator,db_owner等人員才能執行.

# DQL語句        數據庫查詢語言(select)

MysqlRPM安裝

#!/usr/bin/env bash
# Author: ZhouJian
# Mail: 18621048481@163.com
# Time: 2019-9-3
# Describe: CentOS 7 Install Mysql.rpm Script
clear
echo -ne "\\033[0;33m"
cat<<EOT
                                  _oo0oo_
                                 088888880
                                 88" . "88
                                 (| -_- |)
                                  0\\ = /0
                               ___/'---'\\___
                             .' \\\\\\\\|     |// '.
                            / \\\\\\\\|||  :  |||// \\\\
                           /_ ||||| -:- |||||- \\\\
                          |   | \\\\\\\\\\\\  -  /// |   |
                          | \\_|  ''\\---/''  |_/ |
                          \\  .-\\__  '-'  __/-.  /
                        ___'. .'  /--.--\\  '. .'___
                     ."" '<  '.___\\_<|>_/___.' >'  "".
                    | | : '-  \\'.;'\\ _ /';.'/ - ' : | |
                    \\  \\ '_.   \\_ __\\ /__ _/   .-' /  /
                ====='-.____'.___ \\_____/___.-'____.-'=====
                                  '=---='
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                建議系統                    CentOS7
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# PS:請儘可能使用純淨的CentOS7系統,咱們會在服務器安裝Mysql5.7,
# 將mysql-5.7.23-1.el7.x86_64.rpm-bundle.tar包和腳本放到root目錄下執行便可,密碼爲ZHOUjian.20
EOT
echo -ne "\\033[m"
init_security() {
systemctl stop firewalld
systemctl disable firewalld &>/dev/null
setenforce 0
sed -i '/^SELINUX=/ s/enforcing/disabled/'  /etc/selinux/config
sed -i '/^GSSAPIAu/ s/yes/no/' /etc/ssh/sshd_config
sed -i '/^#UseDNS/ {s/^#//;s/yes/no/}' /etc/ssh/sshd_config
systemctl enable sshd crond &> /dev/null
echo -e "\033[32m [安全配置] ==> OK \033[0m"
}
init_yumsource() {
if [ ! -d /etc/yum.repos.d/backup ];then
    mkdir /etc/yum.repos.d/backup
fi
mv /etc/yum.repos.d/* /etc/yum.repos.d/backup 2>/dev/null

if ! ping -c2 www.baidu.com &>/dev/null    
then
    echo "您沒法上外網,不能配置yum源"
    exit    
fi
curl -o /etc/yum.repos.d/163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
timedatectl set-timezone Asia/Shanghai
echo "nameserver 114.114.114.114" > /etc/resolv.conf
echo "nameserver 8.8.8.8" >> /etc/resolv.conf
chattr +i /etc/resolv.conf


echo -e "\033[32m [YUM Source] ==> OK \033[0m"
}
init_mysql() {
rpm -e mariadb-libs --nodeps
rm -rf /var/lib/mysql
rm -rf /etc/my.cnf
tar xvf /root/mysql-5.7.23-1.el7.x86_64.rpm-bundle.tar -C /usr/local/
cd /usr/local
rpm -ivh mysql-community-server-5.7.23-1.el7.x86_64.rpm \
mysql-community-client-5.7.23-1.el7.x86_64.rpm \
mysql-community-common-5.7.23-1.el7.x86_64.rpm \
mysql-community-libs-5.7.23-1.el7.x86_64.rpm |
rm -rf mysql-community-* 
}
changepass() {
sed -i '/\[mysqld]/ a skip-grant-tables' /etc/my.cnf
systemctl restart mysqld
mysql <<EOF
        update mysql.user set authentication_string='' where user='root' and Host='localhost';
        flush privileges;
EOF
sed -i '/skip-grant/d' /etc/my.cnf
systemctl restart mysqld
yum -y install expect ntp
cat  > /etc/ntp.conf << EOF
restrict default nomodify
server 127.127.1.0
fudge 127.127.1.0 stratum 10
EOF
systemctl start ntpd &&  systemctl enable ntpd
expect <<-EOF
spawn  mysqladmin -uroot -p password "ZHOUjian.20"
        expect {
                "password" { send "\r"  }
}
        expect eof
EOF
systemctl restart mysqld
}
main() {
init_hostname
init_security
init_yumsource
init_mysql
changepass
}
main

修改Mysql密碼

# 修改Mysql密碼下面有三種辦法
# 1.剛安裝好的mysql,能夠從/var/log/mysqld.log獲取臨時密碼
grep  "password"  /var/log/mysqld.log   
        [root@mysql ~]# mysql -uroot -p
    Enter password:
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'ZHOUjian.22';

# 2.mysqladmin -uroot -p password "Baidu.123.com"
    Enter password:

# 3.實驗環境不知道root密碼操做方法以下
sed -i '/\[mysqld]/ a skip-grant-tables' /etc/my.cnf
systemctl restart mysqld
mysql <<EOF
        update mysql.user set authentication_string='' where user='root' and Host='localhost';
        flush privileges;
EOF
sed -i '/skip-grant/d' /etc/my.cnf
systemctl restart mysqld
mysqladmin -uroot -p password "ZHOUjian.20"
Enter password:              # 此處回車一下便可

# 4.mariadb修改密碼  
use mysql  
UPDATE user SET password=password('ZHOUjian.20') WHERE user='root';  
MariaDB [mysql]> flush privileges;

# 若是嫌登錄Mysql輸入密碼麻煩,可使用如下辦法,只須要mysql就能夠進入數據庫
vim /etc/my.cnf
[client]
password=admin
user=root
systemctl restart mysqld Or mariadb

Mysql.tar包方式安裝安裝

# 安裝源碼Mysql
#!/usr/bin/env bash
# Author: ZhouJian
# Mail: 18621048481@163.com
# Time: 2019-9-3
# Describe: CentOS 7 Install Mysql.tar Script    # 此處爲編譯好的tar包,具體編譯有時間再親測一遍
Deplay(){
rpm -e mariadb-libs --nodeps
setenforce 0
systemctl stop firewalld
systemctl enable firewalld
sed -i '/^SELINUX=/ s/enforcing/disabled' /etc/ssh/sshd_config
sed -i '/^GSSAPIAu/ s/yes/no/' /etc/ssh/sshd_config
sed -i '/^#UseDNS/ {s/^#//;s/yes/no}' /etc/ssh/sshd_config

id mysql > /dev/null
if [ $? -eq 0 ];then
echo "mysql user exist"
else
groupadd mysql
useradd -M -s /sbin/nologin mysql -g mysql
fi
if [ ! -d /usr/local/mysqld ];then
	tar xf mysql-5.7.26-bin.tar.xz -C /usr/local/
	chown mysql.mysql /usr/local/mysqld/ -R
fi
echo "export PATH=$PATH:/usr/local/mysqld/mysql/bin" >> /etc/profile
source /etc/profile
cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysqld/mysql
datadir = /usr/local/mysqld/data
tmpdir = /usr/local/mysqld/tmp
socket = /usr/local/mysqld/tmp/mysql.sock
pid_file = /usr/local/mysqld/tmp/mysqld.pid
log_error = /usr/local/mysqld/log/mysql_error.log
slow_query_log_file = /usr/local/mysqld/log/slow_warn.log
user = mysql
port = 3306
bind-address = 0.0.0.0
character-set-server = utf8
default_storage_engine = InnoDB
EOF

ln -s /usr/local/mysqld/mysql/support-files/mysql.server /usr/bin/mysqldctl
mysqldctl start
	ln -s /usr/local/mysqld/tmp/mysql.sock /tmp/mysql.sock
mysqldctl restart
sed -i '/\[mysqld]/ a skip-grant-tables' /etc/my.cnf
mysqldctl restart
mysql <<EOF
	update mysql.user set authentication_string='' where user='root' and Host='localhost';
flush privileges;
EOF
sed -i '/skip-grant/d' /etc/my.cnf
mysqldctl restart
yum -y install expect ntp
cat > /etc/ntp.conf << EOF
restrict default nomodify
server 127.127.1.0
fudge 127.127.1.0 stratum 10
EOF
systemctl start ntpd ; systemctl enable ntpd
expect <<-EOF
spawn mysqladmin -uroot -p password "ZHOUjian.20"
expect {
"password" { send "\r" }
}
expect eof
EOF
	mysqldctl restart
}
Deplay
相關文章
相關標籤/搜索