MySQL本番環境レベルの導入


ダウンロードと解凍
多くの会社がMySQL 5を使用しています.6ですので、この記事では5.6を例に説明します.ダウンロードアドレス、共有コード:pvvc.MySQLを/usr/localディレクトリにインストールします.
#        
[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

なぜソフトリンクを作成するのか、私の前の記事を見て、ソフトリンクの使用シーンを詳しく紹介してください.これがその1つです.
MySQLのユーザーとユーザーグループの作成
MySQL専用のユーザーを作成し、他のユーザーが誤操作してデータベースに問題が発生しないようにするには、生産上の権限制御が重要です.
#   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基礎知識シリーズの2つのユーザ/ユーザグループコマンド集合部分を見て,この操作の使用場面を詳しく紹介してください.
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          #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.
ファイル権限の変更
#              
[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コマンドを使用する必要はありません.以下が例です.
[root@hadoop001 ~]# su - mysqladmin
Last login: Mon Jun 24 22:11:23 CST 2019 on pts/1
hadoop001:mysqladmin:/usr/local/mysql:>

転載先:https://juejin.im/post/5d10dc5ae51d4510a37bac21