下载与解压
不少公司还在使用MySQL5.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
复制代码
为什么要创建软链接,请看我的上篇文章,详细介绍软链接使用场景,这就是其中一种。
创建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基础知识系列之二的用户/用户组命令集合部分,详细介绍这种操作的使用场景。
创建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。
文件权限更改
# 变更配置文件权限和所属用户
[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:>
复制代码