一、安装JDK
安装JDK。由于会在大量项目中用到,建议设成全局环境变量。建议使用gz包(jdk-8u45-linux-x64.tar.gz),解压到指定的目录(/usr/java)下,然后设置环境变量。这样做的目的可以自定义安装的位置,方便。
1、解压
tar -zxvf jdk-8u45-linux-x64.gz
2、更改解压下所有文件的拥有者
chown -R root:root jdk1.8.0_45
3、设置环境变量
vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_45
export PATH=$JAVA_HOME/bin:$PATH
4、生效环境变量
source /etc/profile
二、创建账号
创建运行与管理MySQL的账号。
1、创建一个用户组dba
groupadd -g 101 dba
2、创建一个家目录为/usr/local/mysql的账户mysqladmin
useradd -u 514 -g dba -G root -d /usr/local/mysql mysqladmin
passwd mysqladmin
3、设置mysqladmin个人环境变量
cp /etc/skel/.* /usr/local/mysql
三、解压MySQL
MySQL解压到/usr/local这个路径下。
1、检查系统上是否已经运行
ps -ef|grep mysqld
2、检查系统上是否已经安装过MySQL
rpm -qa |grep -i mysql
3、解压
tar -zxvf mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz
4、更名和更改所有文件的拥有者
mv mysql-5.6.23-linux-glibc2.5-x86_64 mysql
chown -R mysqladmin:dba mysql
chmod -R 755 /usr/local/mysql
四、配置
MySQL启动的时候会找配置文件,如果找不到就启动不起来,所以MySQL的配置文件比较重要。
寻找配置文件的路劲
/etc/my.cnf ->
/etc/mysql/my.cnf ->
SYSCONFDIR/my.cnf ->
$MYSQL_HOME/my.cnf ->
--defaults-extra-file ->
~/my.cnf
1、创建my.cnf配置文件
touch /etc/my.cnf
2、赋予权限640
chmod 640 /etc/my.cnf
chown mysqladmin:dba /etc/my.cnf
往/etc/my.cnf添加的内容 ----->
[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_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
五、安装
MySQL正式开始安装,安装肯定不是一次就好了的。多次尝试才能成功。
1、切换mysqladmin用户到/usr/local/mysql/路径下建立arch文件夹,用于归档
mkdir arch
2、开始安装
scripts/mysql_install_db --user=mysqladmin --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
六、安装时候常见的问题
6.1 libaio.so.1: cannot open shared object file: No such file or director
这个是缺少安装包libaio和libaio-devel. 安装一下
yum install libaio*
七、添加MySQL的开机自启动
正常生产中一般开机时自动启动MySQL。
1、切换到root账户
su -
2、复制启动文件到init.d/下
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql
3、增加权限
chmod +x /etc/rc.d/init.d/mysql
4、添加服务
chkconfig --add mysql
chkconfig --level 345 mysql on
5、编辑启动设置
vim /etc/rc.local
最后一行增加
su - mysqladmin -c "/etc/init.d/mysql start --federated"
八、启动MySQL并查看进程和收听
启动MySQL
1、切换用户mysqladmin
su - mysqladmin
2、如果在家目录下有my.cnf文件,就删除,以免不必要麻烦
rm -rf my.cnf
3、后台启动mysql
bin/mysqld_safe &
4、查看是否启动
service mysql status
5、登入到MySQL交互shell中
mysql
6、显示所有数据库
show databases;