这篇博客的主要内容是在CentOS7服务器上搭建一个MySQL5.6版本的数据库服务。
1、我的当前环境:
[root@local-test ~]# cat /proc/cpuinfo |grep "physical id"|wc -l
2
[root@local-test ~]# free -h
total used free shared buff/cache available
Mem: 2.8G 127M 2.4G 9.5M 191M 2.5G
Swap: 2.0G 0B 2.0G
[root@local-test ~]# uname -sr
Linux 3.10.0-1062.el7.x86_64
2、创建mysql服务的相关目录和用户
a.创建安装目录:
[root@local-test ~]# mkdir /opt/mysql
b.创建数据目录:
[root@local-test ~]# mkdir /data/mysql/data -p
c.添加mysql用户
[root@local-test ~]# useradd mysql -M -s /sbin/nologin #-M:不创建家目录,-s /sbin/nologin :不允许该登录
3、下载MySQL安装包
下载MySQL-5.6.47版本:官网下载地址:https://dev.mysql.com/downloads/mysql/
[root@local-test ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
4、安装MySQL:
a.解压:
[root@local-test ~]# tar xf mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz -C /opt/mysql/
b.重命名目录:
[root@local-test ~]# cd /opt/mysql/
[root@local-test mysql]# mv mysql-5.6.47-linux-glibc2.12-x86_64/* .
[root@local-test mysql]# rm -rf mysql-5.6.47-linux-glibc2.12-x86_64/
[root@local-test mysql]# ls
bin data docs include lib LICENSE man mysql-test README scripts share sql-bench support-files
c.安装mysql:把安装目录和数据目录授权给mysql用户
[root@local-test mysql]# chown -R mysql.mysql /opt/mysql/ /data/mysql/data/
[root@local-test mysql]# ./scripts/mysql_install_db --basedir=/opt/mysql/ --datadir=/data/mysql/data/ --user=mysql
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper
出现这种问题很容易解决,仔细看错误信息可以知道,我们需要安装perl相关模块才能执行这个初始化脚本:
[root@local-test mysql]# yum install perl-devel autoconf -y
安装完成之后再次执行初始化命令:
[root@local-test mysql]# ./scripts/mysql_install_db --basedir=/opt/mysql/ --datadir=/data/mysql/data/ --user=mysql
返回信息如下:限于篇幅,此处不在粘贴,主要注意看是否出现两个OK
......
OK
......
OK
......
此时说明你的mysql服务已经成功的安装到服务器上了,但是为了生产安全我们需要做一些基本的优化如下:
5、新安装MySQL基础优化:
a. my.cnf文件参数设置:
[root@local-test mysql]# vim /etc/my.cnf
[mysqld]
#基础信息
basedir=/opt/mysql
datadir=/data/mysql/data
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
port=3306
lower_case_table_names=1
server_id=1
#连接MySQL服务器时使用的字符编码设置
init_connect="SET collation_connection = utf8mb4_unicode_ci"
init_connect="SET NAMES utf8mb4"
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
skip-character-set-client-handshake
[client]
#clinet的信息要与[mysqld]中的信息保持一致,特别注意socket的位置
port = 3306
socket = /var/lib/mysql/mysql.sock
default_character_set=utf8mb4
[mysqld_safe]
log-error=/var/log/mysql/mysql.log
pid-file=/var/lib/mysql/mysql.pid
[mysql]
default_character_set=utf8mb4
b. 启动脚本优化:
二进制包安装的mysql的启动脚本默认是在/opt/mysql/support-files目录下的mysql.server文件,我一般会把它放到/etc/init.d/下使用chkconfig管理,CentOS7一样适用。
[root@local-test support-files]# cp /opt/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@local-test support-files]# chkconfig --add mysqld
[root@local-test support-files]# chkconfig --list |grep mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
c.启动MySQL服务
[root@local-test support-files]# /etc/init.d/mysqld start
Starting MySQL.Logging to "/var/log/mysql/mysql.log".
200228 03:45:07 mysqld_safe Directory "/var/lib/mysql" for UNIX socket file don"t exists.
ERROR! The server quit without updating PID file (/data/mysql/data/local-test.pid).
这个错误提示我们没有/var/log/mysql这个目录,所以需要新建一个,注意别忘了授权!
[root@local-test support-files]# mkdir /var/lib/mysql
[root@local-test support-files]# chown -R mysql.mysql /var/lib/mysql/
再次启动
[root@local-test support-files]# /etc/init.d/mysqld start
Starting MySQL.Logging to "/var/log/mysql/mysql.log".
.... SUCCESS!
[root@local-test support-files]#
d.登陆mysql
[root@local-test support-files]# mysql
-bash: mysql: command not found #/opt/mysql不在path搜索路径里
[root@local-test support-files]# ln -s /opt/mysql/bin/* /usr/bin/
[root@local-test support-files]# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.6.47 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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>
e.设置mysql管理员初始密码:
mysql>use mysql;
mysql> update user set password=password("root") where user="root";
mysql> flush privileges;
mysql> exit;
#service mysql restart
密码生效;
再次登陆就要使用密码了!
[root@local-test support-files]# mysql -uroot -p
远程连接mysql、Navicat等工具连接的时候、就是远程连接、而不是登陆的账号密码了。
若远程连接账号密码都是对的、但是连接不上、可能就是防火墙没有添加3306端口、添加上就OK了
配置:
登陆mysql
mysql>use mysql;
mysql>GRANT ALL PRIVILEGES ON *.* TO "myuser"@"%" IDENTIFIED BY "mypassword" WITH GRANT OPTION;
mysql> flush privileges;
mysql> exit;