今天在虚拟机安装mysql ,想玩玩结果踩坑了,所以记录一下哈哈哈
1.在安装mysql
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.35-linux2.6-x86_64.tar.gz
tar -xvf mysql-5.6.35-linux2.6-x86_64.tar.gz
mv mysql-5.6.35-linux2.6-x86_64 /usr/local/mysql
groupadd mysql
useradd -r -g mysql mysql
mkdir -p /data/mysql
chown mysql:mysql -R /data/mysql
2.配置及登录
vim /etc/my.cnf
配置内容:
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306
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
cd /usr/local/mysql/bin/
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --user=mysql --initialize
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
service mysql start
ps -ef|grep mysql
由于我已启了服务 我只是查看一下状态,mysql服务是running状态
mysql -u root -p
输入密码登录
3.外部连接
默认情况下,mysql只允许本地登录,如果要开启远程连接,则需要配置
grant all on *.* to admin@'%' identified by '123456' with grant option;
flush privileges;
【%表示允许任何ip地址用admin帐户和密码(123456)来访问这个mysql server】
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
flush privileges;
安装连接库的客户端,然后连接数据库
先ping一下 是否能通,一般都是通的
因为centos7默认的防火墙是firewalld防火墙,不是使用iptables
因此禁用firewalld服务,或者干脆使用默认的firewalld防火墙。
systemctl stop firewalld
systemctl mask firewalld
#开放443端口(HTTPS)
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
#保存上述规则
service iptables save
#开启服务
systemctl restart iptables.service
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
保存后重启reboot
再次连接成功