1. 程式人生 > >CentOS7上安裝MySQL總結

CentOS7上安裝MySQL總結

MySQL5.6

一、MySQL數據庫介紹

1、數據庫基礎知識

  • MySQL是一種開放源代碼的關系型數據庫管理系統(RDBMS),MySQL數據庫系統使用最常用的數據庫管理語言–結構化查詢語言(SQL)進行數據庫管理。

  • MySQL在2008年被SUN公司收購,2009年SUN公司被Oracle公司收購。

  • MySQL5.6功能變化較大,MySQL5.7性能上有很大提升

  • Mariadb為MySQL的一個分支,官方網站:https://mariadb.com, MariaDB主要由SkySQL公司維護,而SkySQL公司團隊是由MySQL原作者等大部分原班人馬組成的。

  • MySQL版本分為Community社區版、Enterprise企業版、GA(Generally Available)通過版本(通常用於生產環境)、DMR(Development Milestone Release)開發裏程碑發布版、RC(Release Candidate)發行候選版本、Beta開放測試版本、Alpha內部測試版本。

數據庫(Database)是按照數據結構來組織、存儲和管理數據的倉庫,它產生於距今六十多年前,隨著信息技術和市場的發展,特別是二十世紀九十年代以後,數據管理不再僅僅是存儲和管理數據,而轉變成用戶所需要的各種數據管理的方式。數據庫有很多種類型,從最簡單的存儲有各種數據的表格到能夠進行海量數據存儲的大型數據庫系統都在各個方面得到了廣泛的應用。

主流的數據庫有:sqlserver,mysql,Oracle、SQLite、Access、MS SQL Server等,本文主要講述的是mysql

2、數據庫管理的功能

a. 將數據保存到文件或內存

b. 接收特定的命令,然後對文件進行相應的操作

說明:對於數據庫管理系統,無須自己再去創建文件和文件夾,而是直接傳遞 命令給相應軟件,讓其來進行文件操作,他們統稱為數據庫管理系統(DBMS,Database Management System)

二、MySQL安裝配置

1. MySQL的幾個常用安裝包方法

a.rpm安裝 ##不能定義安裝路徑,默認就是安裝/usr/目錄下面
b.源碼安裝
c.二進制免編譯安裝 ####不用編譯,可以指定安裝目錄

說明:一般在工作中推薦使用二進制免編譯安裝

2.安裝操作步驟

cd /usr/local/src/   ####指定mysql的安裝目錄,其他目錄也行
wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz

解壓安裝包

tar zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz  

移動重命名

mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql
cd /usr/local/mysql/
ls
bin  COPYING  data  docs  include  lib  man  mysql-test  README  scripts  share  sql-bench  support-files

創建mysql用戶

useradd mysql

創建存放mysql數據的目錄

[root@localhost mysql]# mkdir /data 
[root@localhost mysql]# ls /data/
[root@localhost mysql]#
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

說明:在執行這條命令後,怎麽驗證這條初始化命令是否成功,可以從兩點來確認

1)執行命令後,最後會出現兩個OK

[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
Installing MySQL system tables...2017-11-29 06:16:59 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-11-29 06:16:59 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-11-29 06:16:59 0 [Note] ./bin/mysqld (mysqld 5.6.36) starting as process 6626 ...
2017-11-29 06:16:59 6626 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-11-29 06:16:59 6626 [Note] InnoDB: The InnoDB memory heap is disabled
2017-11-29 06:16:59 6626 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-11-29 06:16:59 6626 [Note] InnoDB: Memory barrier is not used
2017-11-29 06:16:59 6626 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-11-29 06:16:59 6626 [Note] InnoDB: Using Linux native AIO
2017-11-29 06:16:59 6626 [Note] InnoDB: Using CPU crc32 instructions
2017-11-29 06:16:59 6626 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-11-29 06:16:59 6626 [Note] InnoDB: Completed initialization of buffer pool
2017-11-29 06:16:59 6626 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2017-11-29 06:16:59 6626 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2017-11-29 06:16:59 6626 [Note] InnoDB: Database physically writes the file full: wait...
2017-11-29 06:16:59 6626 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2017-11-29 06:17:00 6626 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2017-11-29 06:17:00 6626 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2017-11-29 06:17:00 6626 [Warning] InnoDB: New log files created, LSN=45781
2017-11-29 06:17:00 6626 [Note] InnoDB: Doublewrite buffer not found: creating new
2017-11-29 06:17:00 6626 [Note] InnoDB: Doublewrite buffer created
2017-11-29 06:17:00 6626 [Note] InnoDB: 128 rollback segment(s) are active.
2017-11-29 06:17:00 6626 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-11-29 06:17:00 6626 [Note] InnoDB: Foreign key constraint system tables created
2017-11-29 06:17:00 6626 [Note] InnoDB: Creating tablespace and datafile system tables.
2017-11-29 06:17:00 6626 [Note] InnoDB: Tablespace and datafile system tables created.
2017-11-29 06:17:00 6626 [Note] InnoDB: Waiting for purge to start
2017-11-29 06:17:00 6626 [Note] InnoDB: 5.6.36 started; log sequence number 0
2017-11-29 06:17:00 6626 [Note] Binlog end
2017-11-29 06:17:00 6626 [Note] InnoDB: FTS optimize thread exiting.
2017-11-29 06:17:00 6626 [Note] InnoDB: Starting shutdown...
2017-11-29 06:17:02 6626 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK
Filling help tables...2017-11-29 06:17:02 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-11-29 06:17:02 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-11-29 06:17:02 0 [Note] ./bin/mysqld (mysqld 5.6.36) starting as process 6652 ...
2017-11-29 06:17:02 6652 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-11-29 06:17:02 6652 [Note] InnoDB: The InnoDB memory heap is disabled
2017-11-29 06:17:02 6652 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-11-29 06:17:02 6652 [Note] InnoDB: Memory barrier is not used
2017-11-29 06:17:02 6652 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-11-29 06:17:02 6652 [Note] InnoDB: Using Linux native AIO
2017-11-29 06:17:02 6652 [Note] InnoDB: Using CPU crc32 instructions
2017-11-29 06:17:02 6652 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-11-29 06:17:02 6652 [Note] InnoDB: Completed initialization of buffer pool
2017-11-29 06:17:02 6652 [Note] InnoDB: Highest supported file format is Barracuda.
2017-11-29 06:17:02 6652 [Note] InnoDB: 128 rollback segment(s) are active.
2017-11-29 06:17:02 6652 [Note] InnoDB: Waiting for purge to start
2017-11-29 06:17:02 6652 [Note] InnoDB: 5.6.36 started; log sequence number 1625977
2017-11-29 06:17:02 6652 [Note] Binlog end
2017-11-29 06:17:02 6652 [Note] InnoDB: FTS optimize thread exiting.
2017-11-29 06:17:02 6652 [Note] InnoDB: Starting shutdown...
2017-11-29 06:17:04 6652 [Note] InnoDB: Shutdown completed; log sequence number 1625987
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 localhost.localdomain 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 at http://bugs.mysql.com/
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
New default config file was created as ./my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings
WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

2) 確認命令是否執行成功的方法

[root@localhost mysql]#  echo $?
0
說明命令初始化成功

3.拷貝mysql配置文件和啟動腳本

[root@localhost mysql]# ls
bin      data  include  man     mysql-test  scripts  sql-bench COPYING  docs  lib      my.cnf  README      share support-files
[root@localhost mysql]# ls support-files/my-default.cnf    ####mysql的啟動模板配置文件
support-files/my-default.cnf
[root@localhost mysql]# cat !$
cat support-files/my-default.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

拷貝mysql啟動配置文件

[root@localhost mysql]# cp support-files/my-default.cnf /etc/my.cnf 

查看/etc/my.cnf 是由哪個rpm安裝的

[root@docker mysql-5.6.36-linux-glibc2.5-x86_64]# rpm -qf /etc/my.cnf
mariadb-libs-5.5.52-1.el7.x86_64

編輯/etc/my.cnf配置文件,修改datadir、socket參數為

[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock

MySQL數據默認啟動腳本為mysql.server

[root@localhost mysql]# ls support-files/
binary-configure  magic  my-default.cnf  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@localhost mysql]# ls support-files/mysql.server  
support-files/mysql.server      

配置MySQL數據庫啟動腳本參數,編輯support-files/mysql.server,把basedir、datadir參數修改為

basedir=/usr/local/mysql
datadir=/data/mysql

啟動MySQL數據庫,並驗證進程是否存在

[root@centos7 mysql]# systemctl start mysql
[root@centos7 mysql]# systemctl status mysql
● mysqld.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
   Active: active (running) since Mon 2018-03-19 07:25:05 CST; 9s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 20142 ExecStop=/etc/rc.d/init.d/mysqld stop (code=exited, status=0/SUCCESS)
  Process: 21328 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
   Memory: 436.5M
   CGroup: /system.slice/mysqld.service
           ├─21336 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/centos7.pid
           └─21474 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=...
Mar 19 07:25:03 centos7 systemd[1]: Starting LSB: start and stop MySQL...
Mar 19 07:25:03 centos7 mysqld[21328]: Starting MySQL.Logging to ‘/data/mysql/centos7.err‘.
Mar 19 07:25:05 centos7 mysqld[21328]: . SUCCESS!
Mar 19 07:25:05 centos7 systemd[1]: Started LSB: start and stop MySQL.

查看MySQL進程是否存在

[root@centos7 mysql]# ps aux |grep mysql
root     21336  0.0  0.0 115392  1700 ?        S    07:25   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/centos7.pid
mysql    21474  7.5  0.3 1302864 452224 ?      Sl   07:25   0:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/centos7.err --pid-file=/data/mysql/centos7.pid --socket=/tmp/mysql.sock
root     21511  0.0  0.0 112664   968 pts/7    S+   07:25   0:00 grep --color=auto mysql

數據庫3306端口是否被監聽

[root@centos7 mysql]# netstat -nlp |grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      21474/mysqld        
[root@centos7 mysql]#

三、技巧經驗總結

killall和kill命令在日常運維工作中的使用

killall是殺掉所有正在運行的進程,對於正在向MySQL寫數據的時候,如果無法使用kill命令殺掉某個進程,說明此時數據量較大,那麽這樣的情況下,不能使用kill -9 來殺進程,因為這樣會導致正在寫入磁盤或者緩存(內存)中的數據沒有同步到磁盤,這樣可能會丟掉一些數據,這時候只能用killall。

如果無法kill掉某個服務的進程,說明這個服務的數據量的非常大,只有等服務數據同步完才能殺死進程。

CentOS7上安裝MySQL總結