一、下载mysql数据库
1、从mysql官网上下载自己适合的mysql版本https://dev.mysql.com/downloads/mysql/5.6.html#downloads,进入mysql官网,依次点击
![](https://img.haomeiwen.com/i15499433/8a288d1c211fd932.png)
![](https://img.haomeiwen.com/i15499433/760396e6aa1beabf.png)
笔者选择MySQL Community Server 5.6.44 Linux-Generic 64位
二、安装
1、将下载好的mysql压缩文件放置在linux的/usr/local文件夹下,解压该压缩文件
tar -zxvf mysql-5.6.44-linux-glibc2.12-x86_64.tar.gz
2、将解压后的文件重命名为mysql
mv mysql-5.6.44-linux-glibc2.12-x86_64 mysql
3、创建mysql用户组及用户
groupadd mysql
useradd -r -g mysql mysql
4、入到mysql目录,执行添加MySQL配置的操作
cp support-files/my-medium.cnf /etc/my.cnf
或:
cp support-files/my-default.cnf /etc/my.cnf
是否覆盖?按y 回车
5、编辑/etc/my.cnf文件;
vi /etc/my.cnf
在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.
[mysqld]
# 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 = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
# server_id = .....
socket = /tmp/mysql.sock
character-set-server = utf8
skip-name-resolve
log-err = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/data/mysql.pid
# 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
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
6、在mysql当前目录下设定目录的访问权限(注意后面的小点,表示当前目录)
chown -R mysql .
chgrp -R mysql .
scripts/mysql_install_db --user=mysql
chown -R root .
chown -R mysql data
7、初始化数据(在mysql/bin或者mysql/scripts下有个 mysql_install_db 可执行文件初始化数据库),进入mysql/bin或者mysql/scripts目录下,执行下面命令
./mysql_install_db --verbose --user=root --defaults-file=/etc/my.cnf --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql --pid-file=/usr/local/mysql/data/mysql.pid --tmpdir=/tmp
8、启动mysql,进入/usr/local/mysql/bin目录,执行下面命令
[root@iz2ze9z5o9dc mysql]# ./bin/mysql -h127.0.0.1 -uroot -proot
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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>
三、踩过的坑
1、问题:本地可以访问,远程访问请求超时
2、尝试的方法
①. 排除网络或防火墙问题
I、先看是否能ping通远程服务器,ping 192.168.1.211,如果不可以就是网络问题。然后,检查端口是否被防火墙挡住了,telnet 192.168.1.211 3306,如果连接失败,配置防火墙。
II、配置防火墙,开启3306端口
vi /etc/sysconfig/iptables
-A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT(允许3306端口通过防火墙)
III、service iptables restart(重启防火墙使配置生效)
或者关闭防火墙
②检查MySQL配置
如果开启了防火墙,telnet还是失败,通过netstat查看3306的端口状态:
[root@iz2ze9z5 mysql]# netstat -apn|grep 3306
tcp 0 0 127.0.0.1:37806 127.0.0.1:3306 TIME_WAIT -
tcp6 0 0 :::3306 :::* LISTEN 1932/mysqld
tcp6 0 0 172.17.134.67:3306 223.72.43.25:21593 ESTABLISHED 1932/mysqld
tcp6 0 0 172.17.134.67:3306 223.72.43.25:21762 ESTABLISHED 1932/mysqld
检查一下my.cnf的配置,这里可以配置绑定ip地址。
bind-address=addr
不配置或者IP配置为0.0.0.0,表示监听所有客户端连接。
③. 检查用户访问权限
MySQL建用户的时候会指定一个host,默认是127.0.0.1/localhost,那么这个用户就只能本机访问, 其它机器用这个用户帐号访问会提示没有权限,host改为%,表示允许所有机器访问。
mysql> use mysql;
Database changed
mysql> update user set host=’%’ where user=’root’;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> select host,user from user;
+-------------------------+------+
| host | user |
+-------------------------+------+
| % | |
| % | root |
| 127.0.0.1 | root |
| ::1 | root |
| iz2ze9z5o9dc8zmb6 | |
| iz2ze9z5o9dc8zmb6 | root |
| localhost | root |
+-------------------------+------+
7 rows in set (0.00 sec)
重启mysql使配置生效。
mysql]# ./support-files/mysql.server restart
尝试以上三步远程连接仍然连接不上,发现阿里云服务器上的安全组端口需要放行
![](https://img.haomeiwen.com/i15499433/6d5a724a90179bfa.png)
![](https://img.haomeiwen.com/i15499433/34c9d7171afc825a.png)
3、问题解决,远程连接成功
只要智商不滑坡,办法总比困难多
网友评论