Centos7安装并配置mysql5.6
下载安装mysql-5.6.39-linux-glibc2.5-x86_64.tar.gz
创建一个管理数据库的新用户,例如:mysql 并创建所属组
groupadd mysql
useradd -g mysql mysql
将下载的压缩包放到 /usr/local/ 目录下
mv 源文件 /usr/local/
解压安装包
tar -zxvf mysql-5.6.39-linux-glibc2.5-x86_64.tar.gz
进入/usr/local/目录,更给解压文件名
mv mysql-5.6.39-linux-glibc2.5-x86_64 mysql
创建/etc/my.cnf 文件
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysql]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
[mysqld]
skip-name-resolve
port = 3306
#设置3306端口
socket=/var/lib/mysql/mysql.sock
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_name=1
max_allowed_packet=16M
default-time-zone = '+08:00'
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#指定日志
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
更改/etc/my.cnf权限为644,默认my.cnf权限不能为777。
更改文件的所属者
chown -R mysql:mysql ./
创建socket、日志,pid文件。
为socket文件创建软连接(不创建安全初始化会报错)
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
安装数据库
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ 安装数据库
可能出现的问题:报错
FATAL ERROR: please install the following Perl modules before executing
./scripts/mysql_install_db:Data::Dumper
解决方法: yum -y install autoconf //此包安装时会安装Data:Dumper模块
重新安装数据库
设置开机启动脚本,并添加执行权限。
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
将mysqld服务加入到系统服务
chkconfig --add mysqld
检查mysqld服务是否已经生效
chkconfig --list mysqld
可以尝试启动mysql
service mysqld start
将mysql的bin目录加入PATH环境变量,编辑 ~/.bash_profile文件
vim ~/.bash_profile
export PATH=/usr/local/mysql/bin: $PATH
安全初始化
/user/local/mysql/bin/mysql_secure_installation
连接数据库
mysql -uroot -p
默认禁止远程连接,可以打开。
mysql>GRANT ALL PRIVILEGES ON *.* TO 'your username'@'%' IDENTIFIED BY 'your password' WITH GRANT OPTION;
(%代表全部连接地址,也可以指定IP地址远程连接。)