本系列课程主要分为如下几小节来讲述
- MySQL在Linux上的安装
- 构建MySQL的主从架构
MySQL在Linux上的安装
一、安装前的配置
基础环境:
- 操作系统:CentOS 7
- MySQL版本:mysql-5.7.24
- 主机名:mysql01
- 硬件配置:2核、2G内存
1.1 设置主机名
> vim /etc/sysconfig/network
配置主机名:
NETWORKING=yes
HOSTNAME=mysql01
1.2 设置IP与主机名的映射
> vim /etc/hosts
做如下配置:
127.0.0.1 mysql01
192.168.1.5 mysql01(这里的IP为本机的IP地址)
1.3 关闭防火墙
查看防火墙状态:
> systemctl status firewalld
关闭防火墙:
> systemctl stop firewalld
为了避免重启机器后,防火墙又开始,这里可以禁用防火墙:
> systemctl disable firewalld
1.4 将selinux
设置为disabled
> vim /etc/selinux/config
> SELINUX=disabled
二、MySQL源码安装
2.1 新增mysql用户组
> groupadd mysql
2.2 新增mysql用户,并添加到mysql用户组
> useradd -r -g mysql mysql
2.3 新建MySQL执行文件目录
该目录会将编译好的mysql程序安装到这个目录:
> mkdir -p /usr/local/mysql
2.4 新建MySQL数据库文件目录
> mkdir -p /home/mysql/data
> mkdir -p /home/mysql/logs
> mkdir -p /home/mysql/temp
注意:正式生产环境,建议数据目录和日志目录都使用单独的分区来挂载,不同分区属于不同的磁盘或磁盘组。
2.5 配置环境变量
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH
保存退出后,执行以下语句,使得配置生效:
source /etc/profile
2.6 安装编译MySQL需要的依赖包
> yum install make cmake gcc gcc-c++ bison bison-devel ncurses ncurses-devel autoconf automake
2.7 开始编译安装mysql-5.7.24
> cd /usr/local/src
> tar -zxvf mysql-boost-5.7.24.tar.gz
> cd mysql-5.7.24
> cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/home/mysql/data \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306 \
-DENABLE_DOWNLOADS=1 \
-DWITH_BOOST=/usr/local/src/mysql-5.7.24/boost
> 回车
> make(编译)
> make install(安装)
> make clean(清除安装临时文件)
2.8 修改mysql目录拥有者为mysql用户
> chown -Rf mysql:mysql /usr/local/mysql
> chown -Rf mysql:mysql /home/mysql
2.9 初始化数据库
> cd /usr/local/mysql/bin
> ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/data
执行成功之后,会出现以下日志:
2018-12-27T05:44:57.679800Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-12-27T05:44:57.879753Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-12-27T05:44:57.924434Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-12-27T05:44:57.978933Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 8b6531a7-099a-11e9-87d5-000c29abb1e8.
2018-12-27T05:44:57.979740Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-12-27T05:44:57.980535Z 1 [Note] A temporary password is generated for root@localhost: Axudfkff3k(Z
可以看到,为root生成的默认密码为:Axudfkff3k(Z
。
2.10 配置MySQL配置文件
> vim /etc/my.cnf
这个文件为MySQL的配置文件,里面可以配置MySQL的各种配置,比如查询缓存区,慢SQL日志等等,我们这里先做最简单的配置,后面根据需要再把其它配置加进去,如下:
[mysqld]
basedir=/usr/local/mysql
datadir=/home/mysql/data
tmpdir=/home/mysql/temp
socket=/usr/local/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/home/mysql/logs/mysql_error.log
pid-file=/home/mysql/mysql.pid
保存后退出。
2.11 启动MySQL服务
首先复制服务启动脚本:
> cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
启动MySQL服务:
> service mysql start
初次启动会在/usr/local/mysql
目录下生产mysql.sock文件。
将MySQL设置开机自启动:
> chkconfig mysql on
启动成功后,可以使用如下命令查看MySQL服务是否启动成功:
> ps -ef|grep mysql
出现以下信息,则表示MySQL服务启动成功:
root 20633 1 0 16:15 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/home/mysql/data --pid-file=/home/mysql/data/localhost.localdomain.pid
mysql 20720 20633 0 16:15 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/home/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/home/mysql/data/localhost.localdomain.pid
root 20752 20445 0 16:16 pts/1 00:00:00 mysql -u root -p
root 21149 18826 0 16:44 pts/0 00:00:00 grep --color=auto mysql
2.12 登录MySQL并修改root的密码
> mysql -u root -p
> (这里输入root的密码)
> (系统会提示修改root的密码)
> alter user 'root'@'localhost' identified by 'root';
修改成功后,可以做其他的操作。
为了能增加远程机器的访问权限,可以执行以下语句:
> grant all privileges on *.* to root@'%' identified by '123456';
> flush privileges;