Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 知乎專欄 | 視頻教程 | About

第 19 章 MySQL Server

目錄

19.1. MySQL Installation
19.1.1. Installation by apt under debian/ubuntu
19.1.1.1. mysql-5.5.21-debian6.0-i686.deb
19.1.2. Installation by source code
19.1.3. MySQL binary distribution
19.1.4. CentOS
19.1.4.1. CentOS 6.2 + MySQL 5.5.25 (RPM)
19.1.4.2. MySQL 8.0
19.1.5. Docker
19.1.6. mysql-admin
19.1.7. Installing MySQL on Linux Using the MySQL Yum Repository
19.1.7.1. MySQL 5.6
19.1.7.2. MySQL 5.7
19.1.8. Firewall
19.1.9. Limit 狀態
19.1.10. 使用 Btrfs 檔案系統存儲mysql數據
19.1.11. Mac OS
19.2. MariaDB
19.2.1. CentOS 6 YUM 安裝 MariaDB
19.2.2. CentOS 7 安裝 MariaDB
19.3. Percona
19.3.1. Percona yum Repository
19.3.2. Percona XtraBackup
19.3.2.1. 安裝 XtraBackup
19.3.2.2. innobackupex
19.3.2.2.1. 備份資料庫
19.3.2.2.2. 恢復資料庫
19.3.2.3. xbstream
19.3.2.4. xtrabackup
19.3.3. Percona Toolkit - MySQL Management Software
19.4. my.cnf
19.4.1. bind-address
19.4.2. 禁用TCP/IP連結
19.4.3. 配置字符集
19.4.4. 最大連結數 max_connections
19.4.5. 預設引擎 storage-engine
19.4.6. max_allowed_packet
19.4.7. skip-name-resolve
19.4.8. timeout
19.4.9. 與複製有關的參數
19.4.9.1. 用於主庫的選項 Master
19.4.9.2. 用於從庫的選項 Slave
19.4.9.3. 逃過錯誤
19.4.10. 與 InnoDB 有關的配置項
19.4.11. EVENT 設置
19.4.12. 日誌
19.4.13. MySQL 5.7 my.cnf 實例
19.4.14. Example for my.cnf
19.5. MySQL Plugin
19.5.1. validate_password
19.5.2. MySQL Images manager
19.5.3. MySQL fifo
19.5.4. 內容輸出到文本插件
19.6. Replication
19.6.1. Master Slave
19.6.1.1. Master
19.6.1.2. Slave
19.6.1.3. Testing
19.6.1.4. 將現有資料庫遷移到主從結構資料庫
19.6.1.5. 主從複製安全問題
19.6.2. Master Master(主主)
19.6.2.1. Master A
19.6.2.2. Master B
19.6.2.3. 將Master A 資料庫 同步到 Master B 兩端資料庫內容保持一致
19.6.2.4. Master A - B 同步兩端資料庫
19.6.2.5. Master A 資料庫解除只讀權限
19.6.2.6. 查看主主的工作狀態
19.6.3. Semisynchronous Replication
19.6.3.1. Master
19.6.3.2. Slave 配置
19.6.3.3. 卸載插件
19.6.3.4. my.cnf
19.6.4. multi-master replication
19.6.5. multi-source replication
19.6.6. 與複製有關的問題
19.6.6.1. 主從不同步問題
19.6.6.2. mysql-bin 清理問題
19.6.6.3. 跳過 Last_Errno
19.6.6.4. 重置Slave
19.6.7. GTID
19.6.7.1. Master
19.6.7.2. Slave
19.7. MySQL Custer
19.7.1. Management node (MGM node)
19.7.2. Data node
19.7.3. SQL node
19.7.4. Starting
19.7.5. Shutdown
19.7.6. Testing
19.8. MySQL Proxy
19.8.1. Ubuntu
19.8.2. CentOS
19.8.2.1. FAQ
19.9. MySQL Router
19.9.1. 安裝 MySQL Router
19.9.2. 配置 MySQL Router
19.9.2.1. 主備配置
19.9.2.2. 負載均衡配置
19.9.3. MySQL Router , Haproxy,LVS 的選擇
19.10. variables
19.10.1. time_zone
19.10.2. sql_mode
19.10.2.1. 設置 sql_mode
19.10.2.2. 查看 sql_mode
19.10.2.3. 兼容早起 MySQL 版本
19.10.2.4. 5.7.16
19.10.3. wait_timeout
19.10.4. table_lock_wait_timeout
19.10.5. low_priority_updates
19.10.6. collation_server
19.10.7. character_set
19.10.8. datadir
19.10.9. plugin_dir
19.10.10. storage_engine
19.10.11. timeout
19.10.12. max_connections
19.11. SHOW COMMAND
19.11.1. 查看版本
19.11.2. status
19.11.2.1. show status
19.11.2.2. show master status
19.11.2.3. show slave status
19.11.2.4. show plugins
19.11.3. show processlist
19.11.4. binary 日誌
19.11.5. 綫程的使用情況
19.11.6. DATABASES
19.11.7. TABLE
19.11.8. 臨時表
19.11.9. 排序統計信息
19.11.10. Key 狀態
19.11.11. FUNCTION
19.11.12. PROCEDURE
19.11.13. TRIGGERS
19.11.14. EVENTS
19.11.15. 引擎(ENGINES)
19.11.16. 字符集(Collation)
19.11.17. SHOW GRANTS
19.11.18. validate_password
19.12. Monitoring
19.12.1. Analysis and Optimization
19.12.1.1. mytop - top like query monitor for MySQL
19.12.1.2. mtop - MySQL terminal based query monitor
19.12.1.3. innotop
19.12.1.4. mysqlreport - A friendly report of important MySQL status values
19.12.1.5. mysqltuner - MySQL configuration assistant
19.12.2. Munin
19.12.3. Cacti
19.12.4. Monitoring MySQL with SNMP

19.1. MySQL Installation

http://downloads.mysql.com/archives.php

19.1.1. Installation by apt under debian/ubuntu

安裝環境 ubuntu 17.10

			sudo apt install mysql-server mysql-client
		

New password for the MySQL "root" user

		
         ┌──────────────────────┤ Configuring mysql-server-5.7 ├─────────────────────┐
         │ While not mandatory, it is highly recommended that you set a password for the MySQL administrative "root" user.  │
         │                                                                                                                  │
         │ If that field is left blank, the password will not be changed.                                                   │
         │                                                                                                                  │
         │ New password for the MySQL "root" user:                                                                          │
         │                                                                                                                  │
         │ ****____________________________________________________________________________________________________________ │
         │                                                                                                                  │
         │                                                      <Ok>                                                        │
         │                                                                                                                  │
         └─────────────────────────────────────────────────────────────┘
		
		

Repeat password for the MySQL "root" user

		
         ┌───┤ Configuring mysql-server-5.7 ├────┐
         │                                             │
         │                                             │
         │ Repeat password for the MySQL "root" user:  │
         │                                             │
         │ ****_______________________________________ │
         │                                             │
         │                   <Ok>                      │
         │                                             │
         └─────────────────────────┘
         
		

嘗試登錄,驗證是否安裝成功

		
# mysql -udbuser -p
Enter password:

mysql> SHOW GRANTS;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*C6325DAF39AE6CC34E960D3C65F1398FE467E1D0' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

GRANT ALL PRIVILEGES ON example.* TO 'dbuser'@'localhost' IDENTIFIED BY '******' WITH GRANT OPTION;
FLUSH PRIVILEGES;
		
		

配置監聽IP地址,預設資料庫只能從 127.0.0.1訪問

			neo@netkiller /etc/mysql/mysql.conf.d % vim
			/etc/mysql/mysql.conf.d/mysqld.cnf

			bind-address = 0.0.0.0
		

19.1.1.1. mysql-5.5.21-debian6.0-i686.deb

		
sudo apt-get install libaio1


sudo groupadd mysql
sudo useradd -r -g mysql mysql

sudo dpkg -i mysql-5.5.21-debian6.0-i686.deb

cd /opt/mysql/
sudo chown -R mysql .
sudo chgrp -R mysql .

cd server-5.5/

sudo support-files/binary-configure

sudo chown -R mysql data

# Next command is optional
shell> cp support-files/my-medium.cnf /etc/my.cnf

shell> bin/mysqld_safe --user=mysql &

# Next command is optional
sudo cp support-files/mysql.server /etc/init.d/mysql
		
			

19.1.2. Installation by source code

		
./configure \
--prefix=/usr/local/$MYSQL_DIR \
--enable-assembler \
--enable-local-infile \
--with-charset=utf8 \
--with-collation=utf8_general_ci \
--with-extra-charsets=none \
--with-openssl \
--with-pthread \
--with-unix-socket-path=/var/lib/mysql/mysql.sock \
--with-mysqld-user=mysql \
--with-mysqld-ldflags \
--with-client-ldflags \
--with-comment \
--with-big-tables \
--without-ndb-debug \
--without-docs \
--without-debug \
--without-bench

make && make install
		
		

/usr/local/$MYSQL_DIR/bin/mysql_install_db

other option

			--without-isam
			--without-innodb
			--without-ndbcluster
			--without-blackhole
			--without-ibmdb2i
			--without-federated
			--without-example
			--without-comment
			--localstatedir=/usr/local/mysql/data
		

19.1.3. MySQL binary distribution

		
shell> groupadd mysql
shell> useradd -r -g mysql mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
# Next command is optional
shell> cp support-files/my-medium.cnf /etc/my.cnf
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server
		
		

install core database

		
[root@test mysql]# ./scripts/mysql_install_db
Installing MySQL system tables...
100428 23:16:20 [Warning] '--skip-locking' is deprecated and will be removed in a future release. Please use '--skip-external-locking' instead.
OK
Filling help tables...
100428 23:16:20 [Warning] '--skip-locking' is deprecated and will be removed in a future release. Please use '--skip-external-locking' instead.
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h db.example.com password 'new-password'

Alternatively you can run:
./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd ./mysql-test ; perl mysql-test-run.pl

Please report any problems with the ./bin/mysqlbug script!
		
		

set root's password

			[root@test mysql]# cp support-files/mysql.server
			/etc/init.d/mysqld
			[root@test mysql]# /etc/init.d/mysqld start
			Starting MySQL. [ OK ]

			[root@test mysql]# ./bin/mysqladmin -u root
			password 'chen'
			[root@test mysql]# ./bin/mysqladmin -u root -h
			db.example.com password 'chen'
		

test

		
[root@test mysql]# ./bin/mysql -uroot -pchen
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.45 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
		
		

19.1.4. CentOS

19.1.4.1. CentOS 6.2 + MySQL 5.5.25 (RPM)

準備下面的軟件包

# ls -1
MySQL-client-5.5.25-1.el6.x86_64.rpm
MySQL-devel-5.5.25-1.el6.x86_64.rpm
MySQL-server-5.5.25-1.el6.x86_64.rpm
MySQL-shared-5.5.25-1.el6.x86_64.rpm
MySQL-shared-compat-5.5.25-1.el6.x86_64.rpm
			

使用 yum 本地安裝 rpm, yum 可以幫你解決依賴于衝突

# yum localinstall MySQL-*
			
# /etc/init.d/mysql start
Starting MySQL... SUCCESS!

# /usr/bin/mysqladmin -u root password 'tUG26WSslP30bkbwtMhn'
			

19.1.4.2. MySQL 8.0

安裝

			
curl -s https://raw.githubusercontent.com/oscm/shell/master/database/mysql/8.0/server.sh | bash
			
			

啟動

			
systemctl enable mysql
systemctl start mysql
			
			

必須修改密碼後才能使用

			
[root@netkiller ~]# grep "A temporary password" /var/log/mysqld.log
2018-04-03T02:24:16.935070Z 1 [Note] A temporary password is generated for root@localhost: kMA*d<e#Q3EC
2018-04-20T03:36:31.935143Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: MqatK=hae5F#

[root@netkiller ~]# mysqladmin -u root -p'MqatK=hae5F#' password
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
New password:
Confirm new password:
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

[root@netkiller ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

			
			

創建用戶

			
mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'MQiEge1ikst7S_6tlXzBOmt_4b';
Query OK, 0 rows affected (0.05 sec)

mysql> GRANT ALL ON *.* TO 'root'@'%';
Query OK, 0 rows affected (0.03 sec)

			
			

19.1.5. Docker

		
docker run --name mysql -d \
    --restart always \
    -e MYSQL_ROOT_PASSWORD=123456 \
    -e MYSQL_DATABASE=test \
    -e MYSQL_USER=test \
    -e MYSQL_PASSWORD=test \
    -p 127.0.0.1:3306:3306 \
    mysql:latest		
		
		

19.1.6. mysql-admin

$ sudo apt-get install mysql-admin
		

運行mysql-admin

/usr/bin/mysql-admin
		

運行 mysql-query-browser

mysql-query-browser --query="SELECT * FROM users"
		

19.1.7. Installing MySQL on Linux Using the MySQL Yum Repository

19.1.7.1. MySQL 5.6

http://dev.mysql.com/doc/mysql-repo-excerpt/5.6/en/linux-installation-yum-repo.html

yum localinstall http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
			

安裝MySQL Server

yum install mysql-server
chkconfig mysqld on
service mysqld start		
			

修改root密碼

mysqladmin -u root password 'new-password'		
			

安全設置嚮導

/usr/bin/mysql_secure_installation		
			

19.1.7.2. MySQL 5.7

			
yum localinstall -y https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
yum install mysql-server -y
systemctl enable mysqld
systemctl start mysqld

cp /etc/my.cnf{,.original}

cat >> /etc/security/limits.d/20-nofile.conf <<EOF

mysql soft nofile 40960
mysql hard nofile 40960
EOF

cat >> /etc/my.cnf.d/default.cnf <<EOF
[mysqld]
skip-name-resolve
max_connections=8192
default-storage-engine=INNODB

#wait_timeout=30
#interactive_timeout=30

character-set-server=utf8
collation_server=utf8_general_ci
init_connect='SET NAMES utf8'

explicit_defaults_for_timestamp=true

query_cache_type=1
query_cache_size=512M

[client]
character_set_client=utf8

EOF
			
			

MySQL 5.7 會隨機分配一個密碼給用戶

grep "A temporary password" /var/log/mysqld.log
			

登陸後修改密碼

ALTER USER root@localhost identified by 'MQiEge1ikst7S_6tlXzBOmt_4b';
ALTER USER root@localhost PASSWORD EXPIRE NEVER;
			

19.1.8. Firewall

iptables

iptables -A INPUT -i eth0 -p tcp -s xxx.xxx.xxx.xxx --dport 3306 -j ACCEPT
		

19.1.9. Limit 狀態

$ sudo cat /proc/`pidof mysqld`/limits
Limit                     Soft Limit           Hard Limit           Units     
Max cpu time              unlimited            unlimited            seconds   
Max file size             unlimited            unlimited            bytes     
Max data size             unlimited            unlimited            bytes     
Max stack size            10485760             unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             62662                62662                processes 
Max open files            20480                20480                files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       62662                62662                signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us   		
		

19.1.10. 使用 Btrfs 檔案系統存儲mysql數據

		
#!/bin/sh
systemctl stop mysqld

btrfs subvolume create /srv/@mysql
btrfs subvolume list /srv/

UUID=$(blkid | grep btrfs | sed -e 's/.*UUID="\([^"]*\)".*/\1/')
# UUID=786f570d-fe5c-4d5f-832a-c1b0963dd4e6 /srv btrfs defaults 1 1
cat << EOF >> /etc/fstab
UUID=${UUID} /var/lib/mysql  btrfs   noatime,nodiratime,subvol=@mysql 0 2
EOF

mkdir /tmp/mysql
mv /var/lib/mysql/* /tmp/mysql/

mount /var/lib/mysql/
chown mysql:mysql /var/lib/mysql

mv /tmp/mysql/* /var/lib/mysql/

systemctl start mysqld
		
		

19.1.11. Mac OS

brew install mysql
		

啟動

brew services start mysql