1.完成基础配置

参考CentOS7的初始化建议(未完待续)

2.检查系统版本:

略过

3.使用wget下载MySQL并解压:

清华大学源:mysql-5.6.44

中国科技大学源:mysql-5.6.44

华为源:mysql-5.6.44

4.清理系统自带的软件

yum -y remove mysql-server mysql mysql-libs php-mysql

5.进入解压后的mysql目录执行一下命令,创建make并安装(如果此处提示cmake未安装,请参照文章开头的链接初始化centos)

其中make -j的参数是按照逻辑cpu个数来定的,添加j参数可以是make进行并行编译。

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1

make -j 4 && make install

6.安装完成后,添加mysql用户

groupadd mysql

useradd -s /sbin/nologin -M -g mysql mysql

7.修改配置文件

vim /etc/my.cnf

可以参考以下配置文件,实测需要修改的部分有[mysqld]中bind-address,改为自己ip

# Example MySQL config file for medium systems.

# The following options will be passed to all MySQL clients

[client]

#password   = your_password

port        = 3306

socket      = /tmp/mysql.sock

default-character-set=utf8mb4

# Here follows entries for some specific programs

# The MySQL server

[mysqld]

#bind-address=127.0.0.1

port        = 3306

socket      = /tmp/mysql.sock

datadir = /usr/local/mysql/var

collation-server     = utf8mb4_general_ci

character-set-server = utf8mb4

skip-external-locking

key_buffer_size = 16M

max_allowed_packet = 1M

table_open_cache = 64

sort_buffer_size = 512K

net_buffer_length = 8K

read_buffer_size = 256K

read_rnd_buffer_size = 512K

myisam_sort_buffer_size = 8M

# Replication Master Server (default)

# binary logging is required for replication

log-bin=mysql-bin

# binary logging format - mixed recommended

binlog_format=mixed

# required unique id between 1 and 2^32 - 1

# defaults to 1 if master-host is not set

# but will not function as a master if omitted

server-id   = 1

lower-case-table-names=1

# Uncomment the following if you are using InnoDB tables

innodb_data_home_dir = /usr/local/mysql/var

innodb_data_file_path = ibdata1:10M:autoextend

innodb_log_group_home_dir = /usr/local/mysql/var

# You can set .._buffer_pool_size up to 50 - 80 %

# of RAM but beware of setting memory usage too high

innodb_buffer_pool_size = 16M

innodb_additional_mem_pool_size = 2M

# Set .._log_file_size to 25 % of buffer pool size

innodb_log_file_size = 5M

innodb_log_buffer_size = 8M

innodb_flush_log_at_trx_commit = 1

innodb_lock_wait_timeout = 50

[mysqldump]

quick

max_allowed_packet = 16M

[mysql]

no-auto-rehash

# Remove the next comment character if you are not familiar with SQL

#safe-updates

default-character-set=utf8mb4

[myisamchk]

key_buffer_size = 20M

sort_buffer_size = 20M

read_buffer = 2M

write_buffer = 2M

[mysqlhotcopy]

interactive-timeout

8.初始化MySQL,启动MySQL,并查看服务是否启动,如果看到有mysql服务,则说明成功

/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --user=mysql

chown -R mysql /usr/local/mysql/var && chgrp -R mysql /usr/local/mysql/.

cp support-files/mysql.server /etc/init.d/mysql

chmod 755 /etc/init.d/mysql

cat > /etc/ld.so.conf.d/mysql.conf<<EOF

/usr/local/mysql/lib

/usr/local/lib

EOF

ldconfig

/etc/init.d/mysql start

ps -ef|grep mysql

9.后期配置

ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql && ln -s /usr/local/mysql/include/mysql /usr/include/mysql && ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql &&ln -s /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump&&ln -s /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk&&ln -s /usr/local/mysql/bin/mysqld_safe /usr/bin/mysqld_safe

登录MySQL,修改密码

mysql -uroot -p

use mysql;

update user set password=password('$mysqlrootpwd') where user='root';

flush privileges;

*添加开机自启动

cd /etc/init.d/

chmod +x mysql

chkconfig --add mysql

*设置外网访问(该步骤按照实际情况决定是否进行)

1.配置MySQL

登录MySQL执行以下命令,下面命令意思是允许使用root账户和你设置的密码在任意ip下访问mysql,并刷新权限使其立即生效。

grant all privileges on . to root@"%" identified by "密码";   

flush privileges;

2.设置防火墙

以下命令的意思是说,首先开启firewalld防火墙(默认centos7防火墙是关闭的),检查firewalld状态,设置允许通过3306(mysql默认端口)端口进行TCP通信,重启防火墙

systemctl start firewalld

systemctl status firewalld

firewall-cmd --zone=public --add-port=3306/tcp --permanent

firewall-cmd --reload

最后使用下面的命令检查防火墙规则是否成功添加

firewall-cmd --query-port=3306/tcp

----------

可能出现的问题(持续更新):

Q:做完以上设置,还是无法通过外网访问,怎么办?

A:检查mysql配置文件中,bind-address=127.0.0.1项是否注释掉,注释掉以后请重启mysql再试一次,如果还不行,建议卸载linux上的防火墙(我就是卸载了centos上的防火墙就可以用了,hhhh),并检查本地防火墙。