注意:5.7.20版本之后,默认不带my.cnf配置文件!
安装epel更新源(对应系统版本)
下载epel文件epel-release-latest-7.noarch.rpm
wget https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
安装
rpm -ivh epel-release-latest-7.noarch.rpm
安装编译环境
yum groupinstall -y "Development tools"
更新bash和openssl漏洞以及基本软件
yum install -y bash openssl* ntp vim wget telnet nscd
yum clean all
更新服务器时间
ntpdate ntp1.aliyun.com
优化系统
vim /etc/sysctl.conf
net.ipv4.tcp_mem = 3097431 4129911 6194862
net.ipv4.tcp_rmem = 4096 87380 6291456
net.ipv4.tcp_wmem = 4096 65536 4194304
net.ipv4.tcp_max_tw_buckets = 262144
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.core.somaxconn = 65535
重新加载下
sysctl -p
关闭selinux
vim /etc/selinux/config
SELINUX=disabled
注意:修改文件之后,需要系统才行生效。如果不方便重启系统,使用下面的命令
临时关闭selinxu
setenforce 0
mysql安装
mysql 5.7编译cmake要求版本最低为2.8
升级cmake
yum -y install ncurses-devel cmake
yum clean all
创建用户和用户组
groupadd mysql
useradd -g mysql mysql
usermod -s /sbin/nologin mysql
创建目录
mkdir -p /data/3306/data /data/3306/logs
解压软件包
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.22.tar.gz
cd /opt/
wget https://jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
tar zxvf mysql-5.7.22.tar.gz -C /usr/src/
cd /usr/src/mysql-5.7.22/
注意boost_1_59_0.tar.gz的路径,这里我是放在/opt下的。它必须依赖boots
编译
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/3306/data -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/data/3306/mysqld.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all -DWITH_EMBEDDED_SERVER=1 -DWITH_SSL=bundled -DWITH_DEBUG=0 -DENABLE_DOWNLOADS=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/opt/boost_1_59_0.tar.gz
安装
make && make install
请保证机器的内存足够大(建议3~4G),否则报错
make[1]: *** [libmysqld/CMakeFiles/sql_embedded.dir/all] Error 2
设置权限
chown mysql:mysql -R /data/3306 /usr/local/mysql
初始化
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/3306/data
以root初始化操作时要加–user=mysql参数,生成一个随机密码(注意保存登录时用)
2017-12-11T03:54:23.999606Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-12-11T03:54:24.644107Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-12-11T03:54:24.699612Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-12-11T03:54:24.818426Z 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: fa567738-de26-11e7-9c63-000c29e367e9.
2017-12-11T03:54:24.821586Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-12-11T03:54:24.825428Z 1 [Note] A temporary password is generated for root@localhost: K&Al1pUsfkl8
以上信息可以看到随机密码为 K&Al1pUsfkl8
备份配置文件
cd /etc/
mv my.cnf my.cnf.bak
编辑配置文件
vim my.cnf
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /data/3306/data
port = 3306
server_id = 2
socket = /data/3306/mysqld.sock
pid-file = /data/3306/mysql.pid
log_error = /data/3306/logs/error.log
max_allowed_packet=16M
手动创建日志文件
touch /data/3306/error.log
设置目录权限
chown mysql:mysql -R /data/3306 /usr/local/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
添加到开机启动项中
chkconfig --add mysqld
chkconfig --level 2345 mysqld on
编辑开机启动文件
vim /etc/rc.local
最后一行添加
/etc/init.d/mysqld start
添加权限
chmod +x /etc/rc.d/rc.local
systemctl enable rc-local.service
编辑环境变量
vim /etc/profile
最后一行添加
export PATH=$PATH:/usr/local/mysql/bin
重新加载环境变量,启动mysql
source /etc/profile
启动数据库
/etc/init.d/mysqld start
输出以下信息,表示启动成功!
Starting MySQL. SUCCESS!
进入Mysql
mysql -u root -p
输入密码:K&Al1pUsfkl8
修改root用户的密码为root。by后面是要更改的密码!
alter user 'root'@'localhost' identified by 'root';
flush privileges;
quit;
再次进入
mysql -u root -p
输入更改的密码
执行命令,就可以了
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
exit;
如果使用Navicat Premium连接MySQL,需要授权
比如root用户的密码为root,命令如下
mysql -u root -proot
mysql> grant all PRIVILEGES on *.* to root@'%' identified by 'root';
mysql> flush privileges;
mysql> exit;
root@'%' root表示用户,%表示允许所有IP地址连接
by后面是密码
最后使用Navicat Premium连接,就可以了。
登录服务器查看ip地址,使用ifconfig命令,如果报错
-bash: ifconfig: command not found
使用以下命令安装
yum install -y net-tools
再次查看
[root@localhost ~]# ifconfig
eth0: flags=4163 mtu 1500
inet 192.168.11.35 netmask 255.255.255.0 broadcast 192.168.11.255
inet6 fe80::f1b9:361d:10c9:94b6 prefixlen 64 scopeid 0x20 ether 08:00:27:06:0f:70 txqueuelen 1000 (Ethernet)
RX packets 34469 bytes 13949448 (13.3 MiB)
RX errors 0 dropped 30 overruns 0 frame 0
TX packets 25681 bytes 7433569 (7.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback)
RX packets 4 bytes 344 (344.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4 bytes 344 (344.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
如果使用软件连接失败,查看防火墙规则
[root@localhost ~]# iptables -xnL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
INPUT_direct all -- 0.0.0.0/0 0.0.0.0/0
INPUT_ZONES_SOURCE all -- 0.0.0.0/0 0.0.0.0/0
INPUT_ZONES all -- 0.0.0.0/0 0.0.0.0/0
DROP all -- 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
直接清空列表
iptables -F
关闭自带的防火墙
//临时关闭
systemctl stop firewalld
//禁止开机启动
systemctl disable firewalld
再次连接,就可以了