MySQL生產環境級別部署

下載與解壓

很多公司還在使用MySQL5.6,因此本文章依舊以5.6爲例演示。下載地址,分享碼爲:pvvc。將MySQL安裝在/usr/local目錄下。mysql

# 切換到安裝路徑
[root@hadoop001 ~]# cd /usr/local

# 檢查以前是否有MySQL安裝
[root@hadoop001 local]# ps -ef|grep mysqld
root      2493  2423  0 19:48 pts/3    00:00:00 grep mysqld
[root@hadoop001 local]# rpm -qa |grep -i mysql

# 解壓並建立軟連接
[root@hadoop001 local]# tar xzvf mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz
[root@hadoop001 local]# ln -s /usr/local/mysql-5.6.23-linux-glibc2.5-x86_64 /usr/local/mysql
複製代碼

爲何要建立軟連接,請看個人上篇文章,詳細介紹軟連接使用場景,這就是其中一種。linux

建立MySQL的用戶和用戶組

建立MySQL專屬的用戶,能夠作到其餘用戶不會誤操做致使數據庫出問題,生產上權限控制是很重要的。sql

# 建立dba用戶組
[root@hadoop001 local]# groupadd -g 101 dba

# 建立一個家目錄在/usr/local/mysql且主組爲dba的用戶mysqladmin
[root@hadoop001 local]# useradd -u 514 -g dba -G root -d /usr/local/mysql mysqladmin
[root@hadoop001 local]# id mysqladmin
uid=514(mysqladmin) gid=101(dba) groups=101(dba),0(root)

# 賦予mysqladmin用戶密碼
[root@hadoop001 local]# passwd mysqladmin

# copy 環境變量配置文件至mysqladmin用戶的home目錄
[root@hadoop001 local]# cp /etc/skel/.* /usr/local/mysql/

複製代碼

由於該用戶以軟連接爲家目錄,不存在我的環境變量配置文件(也就是以.開頭的隱藏文件),若是不太清楚的同窗請看以前的Linux基礎知識系列之二的用戶/用戶組命令集合部分,詳細介紹這種操做的使用場景。shell

建立MySQL的配置文件

咱們所使用的配置文件在/etc/my.conf目錄,若是你的系統有這個文件,請清空文件,將下方內容拷貝進文件。數據庫

[client]
port            = 3306
socket          = /usr/local/mysql/data/mysql.sock
 
[mysqld]
port            = 3306
socket          = /usr/local/mysql/data/mysql.sock

skip-external-locking
key_buffer_size = 256M
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 4M
query_cache_size= 32M
max_allowed_packet = 16M
myisam_sort_buffer_size=128M
tmp_table_size=32M

table_open_cache = 512
thread_cache_size = 8
wait_timeout = 86400
interactive_timeout = 86400
max_connections = 600

# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 32

#isolation level and default engine 
default-storage-engine = INNODB
transaction-isolation = READ-COMMITTED

server-id  = 1
basedir     = /usr/local/mysql
datadir     = /usr/local/mysql/data
pid-file     = /usr/local/mysql/data/hostname.pid

#open performance schema
log-warnings
sysdate-is-now

binlog_format = MIXED
log_bin_trust_function_creators=1
log-error  = /usr/local/mysql/data/hostname.err
log-bin=/usr/local/mysql/arch/mysql-bin
#other logs
#general_log =1
#general_log_file = /usr/local/mysql/data/general_log.err
#slow_query_log=1
#slow_query_log_file=/usr/local/mysql/data/slow_log.err

#for replication slave
#log-slave-updates 
#sync_binlog = 1

#for innodb options 
innodb_data_home_dir = /usr/local/mysql/data/
innodb_data_file_path = ibdata1:500M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/arch
innodb_log_files_in_group = 2
innodb_log_file_size = 200M

innodb_buffer_pool_size = 2048M          <-- 若是你的服務器內存小於等於4G,請調成1024M
innodb_additional_mem_pool_size = 50M
innodb_log_buffer_size = 16M

innodb_lock_wait_timeout = 100
#innodb_thread_concurrency = 0
innodb_flush_log_at_trx_commit = 1
innodb_locks_unsafe_for_binlog=1

#innodb io features: add for mysql5.5.8
performance_schema
innodb_read_io_threads=4
innodb-write-io-threads=4
innodb-io-capacity=200
#purge threads change default(0) to 1 for purge
innodb_purge_threads=1
innodb_use_native_aio=on

#case-sensitive file names and separate tablespace
innodb_file_per_table = 1
lower_case_table_names=1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[mysqlhotcopy]
interactive-timeout

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
複製代碼

默認的讀取配置文件順序爲:/etc/my.cnf->/etc/mysql/my.cnf->SYSCONFDIR/my.cnf->$MYSQL_HOME/my.cnf-> --defaults-extra-file->~/my.cnf,爲了不讀取到其餘的配置文件,因此咱們使用/etc/my.conf。bash

文件權限更改

# 變動配置文件權限和所屬用戶
[root@hadoop001 local]# chown mysqladmin:dba /etc/my.cnf 
[root@hadoop001 local]# chmod 640 /etc/my.cnf 

# 變動家目錄的權限和所屬用戶,修改完成後,必定要cd進去,ll再看一下
[root@hadoop001 local]# chown -R mysqladmin:dba /usr/local/mysql
[root@hadoop001 local]# chown -R mysqladmin:dba /usr/local/mysql/*
[root@hadoop001 local]# chmod -R 755 /usr/local/mysql 
[root@hadoop001 local]# chmod -R 755 /usr/local/mysql/* 
複製代碼

安裝MySQL

# 切換用戶
[root@hadoop001 local]# su - mysqladmin 
[mysqladmin@hadoop001 ~]$ pwd
/usr/local/mysql

# 建立binlog日誌存儲的文件夾
[mysqladmin@hadoop001 ~]$ mkdir arch 

# 檢查系統是否安裝
[mysqladmin@hadoop001 ~]$ yum -y install autoconf
[mysqladmin@hadoop001 ~]$ yum -y install libaio

# 開始安裝
[mysqladmin@hadoop001 ~]$ scripts/mysql_install_db  \
--user=mysqladmin \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data 
複製代碼

配置MySQL服務以及開機自啓

[root@hadoop001 local]# cd /usr/local/mysql
#將服務文件拷貝到init.d下,並重命名爲mysql
[root@hadoop001 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysql 
#賦予可執行權限
[root@hadoop001 mysql]# chmod +x /etc/rc.d/init.d/mysql
#刪除服務
[root@hadoop001 mysql]# chkconfig --del mysql
#添加服務
[root@hadoop001 mysql]# chkconfig --add mysql
[root@hadoop001 mysql]# chkconfig --level 345 mysql on
[root@hadoop001 mysql]# vi /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

su - mysqladmin -c "/etc/init.d/mysql start"    <--添加這一行
複製代碼

開啓MySQL服務並驗證是否運行

# 切換用戶
[root@hadoop001 mysql]# su - mysqladmin
[mysqladmin@hadoop001 ~]$ pwd
/usr/local/mysql

# 刪除自帶配置文件以防以後誤會
[mysqladmin@hadoop001 ~]$ rm -rf my.cnf

# MYSQL運行
[mysqladmin@hadoop001 ~]$ mysqld_safe &     <--這個命令可能不會結束,不太影響

# 驗證MySQL進程是否有
[mysqladmin@hadoop001 ~]$ ps -ef|grep mysqld
514       6247  6219  0 17:30 pts/1    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe
514       6902  6247  2 17:30 pts/1    00:00:01 /usr/local/mysql/bin/mysqld --
basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-
dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/hostname.err --pid-
file=/usr/local/mysql/data/hostname.pid --socket=/usr/local/mysql/data/mysql.sock --
port=3306
514       6927  6219  0 17:31 pts/1    00:00:00 grep mysqld

# 驗證MySQL端口是不是3306
[mysqladmin@hadoop001 ~]$ netstat -tulnp | grep 6902
tcp        0      0 :::3306                     :::*          LISTEN      11541/mysqld   

# 查看MySQL服務
[root@shadoop001 local]# service mysql status
MySQL running (21507)                                      [  OK  ]
複製代碼

若是這一步MySQL並無啓動,請到查看MySQL解壓目錄/data/hostname.error文件具體報錯狀況,而後再出對應解決方法。服務器

登錄MySQL

[mysqladmin@hadoop001 ~]$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.23-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
mysql> use mysql;
Database changed
# 更新root密碼
mysql> update user set password=password('password') where user='root';
Query OK, 4 rows affected (0.00 sec)
# 刪除MySQL沒有帳號也沒密碼的用戶
mysql> delete from user where user='';
Query OK, 2 rows affected (0.00 sec)
mysql> select host,user,password from user;
+----------------+------+-------------------------------------------+
| host           | user | password                                  |
+----------------+------+-------------------------------------------+
| localhost      | root | *6340BE3C15D246B0D74BAF3F135915ED19E0069F |
| hadoop001      | root | *6340BE3C15D246B0D74BAF3F135915ED19E0069F |
| 127.0.0.1      | root | *6340BE3C15D246B0D74BAF3F135915ED19E0069F |
| ::1            | root | *6340BE3C15D246B0D74BAF3F135915ED19E0069F |
+----------------+------+-------------------------------------------+
4 rows in set (0.00 sec)
# 刷新用戶權限
mysql> flush privileges;
複製代碼

配置全局環境變量

[root@hadoop001 ~]# vi /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
 ....
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge


MYSQL_HOME=/usr/local/mysql      <--  新增
export MYSQL_HOME

PATH=${MYSQL_HOME}/bin:$PATH     <--  新增
export PATH

# 全局環境變量生效
[root@hadoop001 ~]# source /etc/profile
複製代碼

配置mysqladmin用戶環境變量 (非必需)

[root@hadoop001 ~]# su - mysqladmin
[mysqladmin@hadoop001 ~]$ vi ~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi

# 如下新增
unset USERNAME

#stty erase ^H
set umask to 022
umask 022
PS1=`uname -n`":"'$USER'":"'$PWD'":>"; export PS1

# 我的環境變量生效
[mysqladmin@hadoop001 ~]$ . ~/.bash_profile
複製代碼

以上設置,只是爲了切換用戶以後,不用再使用pwd命令,如下就是實例:less

[root@hadoop001 ~]# su - mysqladmin
Last login: Mon Jun 24 22:11:23 CST 2019 on pts/1
hadoop001:mysqladmin:/usr/local/mysql:>
複製代碼
相關文章
相關標籤/搜索