編譯安裝:html
建立MySQL專用帳戶和用戶組:mysql
groupaddmysqlc++
useradd-g mysql mysqlsql
設置用戶操做系統資源的限制,在/etc/security/limits.conf最後添加數據庫
mysql soft nproc 2047bash
mysql hardnproc 16384socket
mysql soft nofile 1024ide
mysql hardnofile 65536工具
執行cmake . -LAH查看MySQL編譯全部支持的參數,或者經過http://dev/mysql.com/doc/refman/5.6/en/source-configuration-options.html查看官方技術文檔ui
安裝cmake:
yuminstall cmake
出現問題:Generic Linux (Architecture Independent), Compressed TAR Archive
解決:yum -y install ncurses-devel
出現問題:CMAKE_C_COMPILER not set, after EnableLanguage
解決:yum install gcc-c++
rm -f CMakeCache.txt
從新編譯安裝
解壓源碼包:tar -zvfx mysql-5.6.28.tar.gz
進入解壓目錄,執行cmake編譯並安裝:
cdmysql-5.6.28
cmake. -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_COLLATION=utf8_general_ci-DDEFAULT_CHARSET=utf8 -DENABLED_LOCAL_INFILE=ON-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1-DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1-DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DCOMPILATION_COMMENT='JSSfor mysqltest' -DWITH_READLINE=ON -DSYSCONFDIR=/data/mysqldata/3306-DMYSQL_UNIX_ADDR=/data/mysqldata/3306/mysql.sock
make && make install
修改軟件所在目錄擁有者爲mysql:
chown-R mysql:mysql /usr/local/mysql
修改mysql用戶環境變量,在/home/mysql/.bash_profile添加
# exportLANG=zh_CN.GB18030
exportPATH=/usr/local/mysql/bin:$PATH
管理MySQL數據庫服務
MySQL數據庫服務:MySQL軟件、MySQL實例和MySQL數據庫幾部分的總稱
MySQL實例:指MySQL進程以及該進程持有的內存資源
MySQL數據庫:MySQLdata目錄或MySQLdatabase文件
mysql數據庫:mysql這個DB
mysql:鏈接MySQL數據庫的命令行方式交互工具mysql
建立數據庫文件默認路徑:
mkdir -p/data/mysqldata/{3306/{data,tmp,binlog},backup,scripts}
chown -R mysql:mysql /data/mysqldata
chown -R mysql:mysql /usr/local/mysql
添加MySQL配置文件/data/mysqldata/3306/my.cnf以下:
[client]
port = 3306
socket = /data/mysqldata/3306/mysql.sock
# The MySQL server
[mysqld]
port = 3306
user = mysql
socket = /data/mysqldata/3306/mysql.sock
pid-file = /data/mysqldata/3306/mysql.pid
basedir = /usr/local/mysql
datadir = /data/mysqldata/3306/data
tmpdir = /data/mysqldata/3306/tmp
open_files_limit = 10240
explicit_defaults_for_timestamp
sql_mode =NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# Buffer
max_allowed_packet = 256M
max_heap_table_size = 256M
net_buffer_length = 8K
sort_buffer_size = 2M
join_buffer_size = 4M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
# Log
log-bin =/data/mysqldata/3306/binlog/mysql-bin
binlog_cache_size = 32m
max_binlog_cache_size = 512m
max_binlog_size = 512m
binlog_format = mixed
log_output = FILE
log-error = ../mysql-error.log
slow_query_log = 1
slow_query_log_file = ../slow_query.log
general_log = 0
general_log_file = ../general_query.log
expire-logs-days = 14
# InnoDB
innodb_data_file_path = ibdata1:2048M:autoextend
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_buffer_pool_size = 1024M
[mysql]
no-auto-rehash
prompt = (\u@\h) [\d]\_
default-character-set = gbk
初始化MySQL數據庫:
/usr/local/mysql/scripts/mysql_install_db--datadir=/data/mysqldata/3306/data --basedir=/usr/local/mysql
啓動MySQL服務:
$ /usr/local/mysql/bin/mysqld_safe--defaults-file=/data/mysqldata/3306/my.cnf &
鏈接數據庫:
$ /usr/local/mysql/bin/mysql
檢查MySQL服務是否啓動:
netstat -tnl | grep 3306
保留一條容許root用戶從本地鏈接的記錄外,刪除所有其餘用戶:
delete from mysql.user where (user,host)not in (select 'root','localhost');
修改管理員用戶:
update mysql.user setuser='system',password=password('password');
truncate table mysql.db;
生效全部操做:
flush privileges;
MySQL服務管理配置
建立中間定義文件,提升腳本的複用性:
$ vi/data/mysqldata/scripts/mysql_env.ini
# set env
MYSQL_USER=system
MYSQL_PASS=’password’
# check parameter
if [ $# -ne 1 ]
then
HOST_PORT=3306
else
HOST_PORT=$1
fi
修改該文件在操做系統層的權限:
$ chmod 600/data/mysqldata/scripts/mysql_env.ini
建立mysql_db_startup.sh腳本,用於啓動MySQL服務:
$ vi/data/mysqldata/scripts/mysql_db_startup.sh
#!/bin/sh
source/data/mysqldata/scripts/mysql_env.ini
echo "Stratup MySQL Service:local_"${HOST_PORT}
/usr/local/mysql/bin/mysqld_safe--defaults-file=/data/mysqldata/${HOST_PORT}/my.cnf &
建立mysql_db_shutdown.sh腳本,用於關閉MySQL服務:
$ vi /data/mysqldata/scripts/mysql_db_shutdown.sh
#!/bin/sh
source/data/mysqldata/scripts/mysql_env.ini
echo "Shutdown MySQL Service:localhost_"${HOST_PORT}
/usr/local/mysqldata/bin/mysqladmin-u${MYSQL_USER} -p${MYSQL_PASS} -S /data/mysqldata/${HOST_PORT}/mysql.sockshutdown
建立mysqlplus.sh腳本文件用於快速登陸:
$ vi/data/mysqldata/scripts/mysqlplus.sh
#!/bin/sh
source/data/mysqldata/scripts/mysql_env.ini
echo "Login MySQL Service:localhost_"${HOST_PORT}
/usr/local/mysql/bin/mysql-u${MYSQL_USER} -p${MYSQL_PASS} -S /data/mysqldata/${HOST_PORT}/mysql.sock $2
受權以上腳本執行權限:
$ chmod +x /data/mysqldata/scripts/*.sh
編輯mysql用戶的環境變量,將上述路徑加入到PATH中:
$ echo "exportPATH=/data/mysqldata/scripts:$PATH">>~/.bash_profile
$ source ~/.bash_profile
編輯/etc/rc.local使MySQL開機啓動,在最後添加:
sudo -i -u mysql/data/mysqldata/scripts/mysql_db_startupsh 3306 >/home/mysql/mysql_db_startup.log 2>&1
visudo把Defaultsrequiretty註釋掉,避免sudo: sorry,you myst have a ttyto run sudo