阿里云Linux下MySQL5.7.20安装

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hunandexingkong/article/details/78720414
本机是64位,直接用命令安装
去网站看下最新的版本,修改下面命令
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz


此处新建MySQL数据库用户及用户组,信息在   新建用户  中维护

#解压
tar -zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
#复制解压后的mysql目录
cp -r mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql

创建目录:/usr/local/mysql/data/mysql

授权目录:cd /usr/local/mysql/
命令 :chown -R mysql:mysql ./   

初始化MySQL:
执行: ./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/mysql
报错:-bash: ./scripts/mysql_install_db: No such file or directory

原因是MySQL5.7没有scripts目录。
在bin目录下执行:
执行以下命令:/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/mysql
报错:-bash: mysqld: command not found  

原因:这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下,当然会找不到命令,我们需要做的就是映射一个链接到/usr/bin目录下,相当于建立一个链接文件。
首先,进入cd /usr/bin
然后,ln -fs /usr/local/mysql/bin/mysqld /usr/bin

[root@iZ2ze2aj5tzdkvlx1frzi7Z local]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/mysql
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

yum install -y libaio

[root@iZ2ze2aj5tzdkvlx1frzi7Z local]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/mysql
2017-12-04T09:21:15.743573Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-12-04T09:21:17.801346Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-12-04T09:21:18.028754Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-12-04T09:21:18.144432Z 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: 7be64491-d8d4-11e7-bde3-00163e08b9aa.
2017-12-04T09:21:18.147354Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-12-04T09:21:18.148351Z 1 [Note] A temporary password is generated for root@localhost: xxxxxxxxxxxx

xxxxxxxxxxxx  :  root用户的初始密码

设置加密链接
执行mysql_ssl_rsa_setup
-bash: mysql_ssl_rsa_setup: command not found

首先,进入cd /usr/bin
然后,ln -fs /usr/local/mysql/bin/mysql_ssl_rsa_setup /usr/bin

[root@iZ2ze2aj5tzdkvlx1frzi7Z bin]# ./mysql_ssl_rsa_setup
2017-12-04 18:09:11 [ERROR]   Failed to access directory pointed by --datadir. Please make sure that directory exists and is accessible by mysql_ssl_rsa_setup. Supplied value : /var/lib/mysql

修改文件  /etc/my.cnf
[mysqld]
#datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# add start 2017.12.4
datadir=/usr/local/mysql/data/mysql
basedir=/usr/local/mysql
user=mysql
character_set_server=utf8 #设置编码格式,否则出现中文乱码
# add end

# 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=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

再次执行mysql_ssl_rsa_setup
[root@iZ2ze2aj5tzdkvlx1frzi7Z bin]# mysql_ssl_rsa_setup
Generating a 2048 bit RSA private key
........................................................................+++
................................................................................+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
.....+++
.......................................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
.....................+++
...........................................+++
writing new private key to 'client-key.pem'
-----

加入系统服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

修改mysqld文件
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data/mysql

执行:
chkconfig mysqld on # 设置开机自启动
chkconfig --list | grep mysqld
[root@iZ2ze2aj5tzdkvlx1frzi7Z init.d]# chkconfig mysqld on
[root@iZ2ze2aj5tzdkvlx1frzi7Z init.d]# chkconfig --list | grep mysqld

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld             0:off    1:off    2:on    3:on    4:on    5:on    6:off


启动 重启 停止
service mysqld start
[root@iZ2ze2aj5tzdkvlx1frzi7Z init.d]# service mysqld start
Starting MySQL.2017-12-04T15:55:59.263950Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
The server quit without updating PID file (/usr/local/mysql[FAILED]sql/iZ2ze2aj5tzdkvlx1frzi7Z.pid).

创建目录/var/log/mysql  /var/run/mysql  /var/lib/ mysql
ps:mariadb是MySQL的一个分支
chown -R mysql:mysql /var/log/mysql
chown -R mysql:mysql /var/run/mysql

[root@iZ2ze2aj5tzdkvlx1frzi7Z run]# service mysqld start
Starting MySQL...The server quit without updating PID file (/usr/local/mysql/data/mysql/iZ2ze2aj5tzdkvlx1frzi7Z.pid).

service mysqld restart
MySQL server PID file could not be found!                  [FAILED]
漏掉一个授权
chown -R mysql:mysql /var/lib/mysql
执行service mysqld start
[root@iZ2ze2aj5tzdkvlx1frzi7Z lib]# service mysqld start
Starting MySQL.                                            [  OK  ]
[root@iZ2ze2aj5tzdkvlx1frzi7Z lib]# service mysqld restart
Shutting down MySQL..                                      [  OK  ]
Starting MySQL.                                            [  OK  ]
[root@iZ2ze2aj5tzdkvlx1frzi7Z lib]# service mysqld stop
Shutting down MySQL..                                      [  OK  ]

service mysqld restart
service mysqld stop

配置环境变量
export PATH=$PATH:/usr/local/mysql/bin/
echo $PATH  #查看环境变量



登录MySQL
如果丢失临时密码,找回方法: 找到datadir目录进入,cd /usr/local/mysql/data/mysql/     删除所有,重新初始化,生成新的临时密码。

登录命令:mysql -u root -p
[root@iZ2ze2aj5tzdkvlx1frzi7Z mysql]# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
解决办法:
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

重新登录  输入临时密码:scjJ74qI*%%e   登录成功
[root@iZ2ze2aj5tzdkvlx1frzi7Z mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20

Copyright (c) 2000, 2017, 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>

临时密码过于复杂,修改密码:
set password for 'root'@'localhost' =password('123456');//更改root密码的sql语句,123456是新的root密码(别忘了结尾的;号)
mysql> set password for 'root'@'localhost' =password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>

重新登录:
1.quit
2.mysql -u root -p     
3.123456

查看端口号:show global variables like 'port';
mysql> show global variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)


使用本地Navicat连接远程数据库:
1.创建用户用来远程连接
    GRANT ALL PRIVILEGES ON *.* TO 'devtest'@'%' IDENTIFIED BY 'devtest' WITH GRANT OPTION;
  (第一个devtest表示用户名,%表示所有的电脑都可以连接,也可以设置某个ip地址运行连接,第二个devtest表示密码)。
2.执行 flush privileges;命令立即生效
    
3.查询数据库用户(看到用户创建成功了)
    SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'devtest'@'%' IDENTIFIED BY 'devtest' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
+------------------------------------+
| query                              |
+------------------------------------+
| User: 'devtest'@'%';               |
| User: 'mysql.session'@'localhost'; |
| User: 'mysql.sys'@'localhost';     |
| User: 'root'@'localhost';          |
+------------------------------------+
4 rows in set (0.00 sec)
依旧连接不上。。。。

------------------------------找原因 ------------------------------
CentOS 7.0默认使用的是firewall作为防火墙
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running) 目前是关闭状态
[root@iZ2ze2aj5tzdkvlx1frzi7Z mysql]# firewall-cmd --state
not running
从centos7开始使用systemctl来管理服务和程序,包括了service和chkconfig。
systemctl list-unit-files|grep firewalld.service       #防火墙处于关闭状态    目前是关闭状态
或者systemctl status firewalld.service                     
[root@iZ2ze2aj5tzdkvlx1frzi7Z mysql]# systemctl list-unit-files|grep firewalld.service
firewalld.service                             disabled

关闭防火墙:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动

启动一个服务:systemctl start firewalld.service
关闭一个服务:systemctl stop firewalld.service
重启一个服务:systemctl restart firewalld.service
显示一个服务的状态:systemctl status firewalld.service
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service;echo $?
查看已启动的服务列表:systemctl list-unit-files|grep enabled

Centos 7 firewall 命令:
查看已经开放的端口:firewall-cmd --list-ports
[root@iZ2ze2aj5tzdkvlx1frzi7Z mysql]# firewall-cmd --list-ports
FirewallD is not running
开启端口:firewall-cmd --zone=public --add-port=3306/tcp --permanent
命令含义:
–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效

查看3306端口使用的什么协议:netstat -antp | grep 3306
[root@iZ2ze2aj5tzdkvlx1frzi7Z mysql]# netstat -antp | grep 3306
tcp        0      0 172.17.49.149:3306      0.0.0.0:*               LISTEN      603/mysqld

执行:firewall-cmd --zone=public --add-port=3306/tcp --permanent
[root@iZ2ze2aj5tzdkvlx1frzi7Z mysql]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
FirewallD is not running
防火墙没开启,开启防火墙:systemctl start firewalld.service   开启启动防火墙:systemctl enable firewalld.service
再次执行:firewall-cmd --zone=public --add-port=3306/tcp --permanent
查看已经开放的端口:firewall-cmd --list-ports   此命令执行后没有结果,应该是开启端口和开放端口意义不同,此处不需要开启,只要开放。

CentOS 7 版本 iptables 命令 ---------开放端口
设置 iptables service   :   yum -y install iptables-services

要修改防火墙配置,如增加防火墙端口3306
    vim /etc/sysconfig/iptables

增加规则
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

保存退出后
    systemctl restart iptables.service #重启防火墙使配置生效
    systemctl enable iptables.service #设置防火墙开机启动

最后重启系统使设置生效即可。
    systemctl start iptables.service #打开防火墙
    systemctl stop iptables.service #关闭防火墙

firewall-cmd --list-ports   依旧没有结果
检查本机能ping通远程,远程不能ping通本地。。。。。。。。。。

登录阿里云控制台授权端口号
云服务器ECS--实例--管理--本实例安全组--配置规则--添加安全组规则
使用ping 、nc 测试连通性
ping 47.93.31.140 
或nc -z -v 47.93.31.140 3306
已经能够连接3306端口,Navicat连接成功。 





猜你喜欢

转载自blog.csdn.net/hunandexingkong/article/details/78720414