美文网首页我爱编程
阿里云centos7上部署mysql

阿里云centos7上部署mysql

作者: nextliving | 来源:发表于2018-04-22 15:18 被阅读163次

    今天在阿里云上安装了mysql,过程中遭遇了很多意想不到的曲折.比如强制修改密码,去修改的时候又限制了密码的格式,等到远程连接的时候又连不上.在此记录完整过程,即方便其他人参考,也方便自己以后随时查阅.

    centos7.4默认数据库

    centos7开始默认数据库源是mariadb,而不是之前的mysql.可以使用下面的命令查看:

    rpm -qa|grep -i mariadb

    终端输出

    mariadb-libs-5.5.56-2.el7.x86_64

    可以看到默认是mariadb5.5,对应mysql的5.5版本.更多讨论参考CentOS 7为什么放弃了MySQL,而改使用MariaDB腾讯云 Centos 7.2 折腾了一个小时没装成功 MySQL

    注意: 此时并没有安装mariadb,如果安装需要执行$ yum install mariadb-server.

    安装mysql

    安装方案

    根据腾讯云 Centos 7.2 折腾了一个小时没装成功 MySQL中的说法,如果安装了mariadb需要先卸载mariadb,再安装mysql,否则可能会出错.因为并没有安装mariadb,所以直接安装mysql

    添加mysql数据库yum源

    首先下载rpm:

    wget https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm

    然后安装rpm:

    $rpm -ivh mysql57-community-release-el7-11.noarch.rpm

    检查是否安装成功:

    $ rpm -qa|grep -i mysql

    如果终端什么都不输出则说明yum源安装失败,如果输出以下内容则安装成功:

    mysql57-community-release-el7-11.noarch

    mysql的yum源列表参考mysql官网MySQL Yum Repository

    安装mysql

    执行命令安装:

    $ yum install mysql-server

    终端输出

    
    Failed to set locale, defaulting to C
    
    Loaded plugins: fastestmirror
    
    base  | 3.6 kB  00:00     
    
    epel  | 4.7 kB  00:00     
    
    extras  | 3.4 kB  00:00     
    
    mysql-connectors-community  | 2.5 kB  00:00     
    
    mysql-tools-community | 2.5 kB  00:00     
    
    mysql57-community | 2.5 kB  00:00     
    
    updates | 3.4 kB  00:00     
    
    (1/6): epel/x86_64/updateinfo | 904 kB  00:00     
    
    (2/6): extras/7/x86_64/primary_db | 185 kB  00:00     
    
    (3/6): mysql-connectors-community/x86_64/primary_db | 18 kB  00:00     
    
    (4/6): mysql-tools-community/x86_64/primary_db  | 39 kB  00:00     
    
    (5/6): epel/x86_64/primary_db | 6.3 MB  00:00     
    
    (6/6): mysql57-community/x86_64/primary_db  | 134 kB  00:00     
    
    Loading mirror speeds from cached hostfile
    
    Resolving Dependencies
    
    --> Running transaction check
    
    ---> Package mysql-community-server.x86_64 0:5.7.21-1.el7 will be installed
    
    --> Processing Dependency: mysql-community-common(x86-64) = 5.7.21-1.el7 for package: mysql-community-server-5.7.21-1.el7.x86_64
    
    --> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.21-1.el7.x86_64
    
    --> Processing Dependency: libaio.so.1(LIBAIO_0.4)(64bit) for package: mysql-community-server-5.7.21-1.el7.x86_64
    
    --> Processing Dependency: libaio.so.1(LIBAIO_0.1)(64bit) for package: mysql-community-server-5.7.21-1.el7.x86_64
    
    --> Processing Dependency: libaio.so.1()(64bit) for package: mysql-community-server-5.7.21-1.el7.x86_64
    
    --> Running transaction check
    
    ---> Package libaio.x86_64 0:0.3.109-13.el7 will be installed
    
    ---> Package mysql-community-client.x86_64 0:5.7.21-1.el7 will be installed
    
    --> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.21-1.el7.x86_64
    
    ---> Package mysql-community-common.x86_64 0:5.7.21-1.el7 will be installed
    
    --> Running transaction check
    
    ---> Package mariadb-libs.x86_64 1:5.5.56-2.el7 will be obsoleted
    
    --> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
    
    --> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
    
    ---> Package mysql-community-libs.x86_64 0:5.7.21-1.el7 will be obsoleting
    
    --> Running transaction check
    
    ---> Package mysql-community-libs-compat.x86_64 0:5.7.21-1.el7 will be obsoleting
    
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ================================================================================
    
     Package Arch Version Repository Size
    
    ================================================================================
    
    Installing:
    
     mysql-community-libs  x86_64 5.7.21-1.el7  mysql57-community 2.1 M
    
     replacing mariadb-libs.x86_64 1:5.5.56-2.el7
    
     mysql-community-libs-compat x86_64 5.7.21-1.el7  mysql57-community 2.0 M
    
     replacing mariadb-libs.x86_64 1:5.5.56-2.el7
    
     mysql-community-server  x86_64 5.7.21-1.el7  mysql57-community 164 M
    
    Installing for dependencies:
    
     libaio  x86_64 0.3.109-13.el7  base 24 k
    
     mysql-community-client  x86_64 5.7.21-1.el7  mysql57-community  24 M
    
     mysql-community-common  x86_64 5.7.21-1.el7  mysql57-community 272 k
    
    Transaction Summary
    
    ================================================================================
    
    Install 3 Packages (+3 Dependent packages)
    
    Total download size: 192 M
    
    Is this ok [y/d/N]: y
    
    Downloading packages:
    
    (1/6): libaio-0.3.109-13.el7.x86_64.rpm | 24 kB  00:00     
    
    warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.21-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
    
    Public key for mysql-community-common-5.7.21-1.el7.x86_64.rpm is not installed
    
    (2/6): mysql-community-common-5.7.21-1.el7.x86_64.rpm | 272 kB  00:00     
    
    (3/6): mysql-community-libs-5.7.21-1.el7.x86_64.rpm | 2.1 MB  00:00     
    
    (4/6): mysql-community-libs-compat-5.7.21-1.el7.x86_64.rpm | 2.0 MB  00:00     
    
    (5/6): mysql-community-client-5.7.21-1.el7.x86_64.rpm | 24 MB  00:02     
    
    (6/6): mysql-community-server-5.7.21-1.el7.x86_64.rpm | 164 MB  00:17     
    
    --------------------------------------------------------------------------------
    
    Total  10 MB/s | 192 MB 00:19     
    
    Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    Importing GPG key 0x5072E1F5:
    
     Userid  : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
    
     Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
    
     Package : mysql57-community-release-el7-11.noarch (installed)
    
     From  : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    Is this ok [y/N]: y
    
    Running transaction check
    
    Running transaction test
    
    Transaction test succeeded
    
    Running transaction
    
    Warning: RPMDB altered outside of yum.
    
     Installing : mysql-community-common-5.7.21-1.el7.x86_64  1/7 
    
     Installing : mysql-community-libs-5.7.21-1.el7.x86_64  2/7 
    
     Installing : mysql-community-client-5.7.21-1.el7.x86_64  3/7 
    
     Installing : libaio-0.3.109-13.el7.x86_64  4/7 
    
     Installing : mysql-community-server-5.7.21-1.el7.x86_64  5/7 
    
     Installing : mysql-community-libs-compat-5.7.21-1.el7.x86_64 6/7 
    
     Erasing : 1:mariadb-libs-5.5.56-2.el7.x86_64  7/7 
    
     Verifying : mysql-community-client-5.7.21-1.el7.x86_64  1/7 
    
     Verifying : mysql-community-libs-compat-5.7.21-1.el7.x86_64 2/7 
    
     Verifying : mysql-community-libs-5.7.21-1.el7.x86_64  3/7 
    
     Verifying : mysql-community-server-5.7.21-1.el7.x86_64  4/7 
    
     Verifying : libaio-0.3.109-13.el7.x86_64  5/7 
    
     Verifying : mysql-community-common-5.7.21-1.el7.x86_64  6/7 
    
     Verifying : 1:mariadb-libs-5.5.56-2.el7.x86_64  7/7 
    
    Installed:
    
     mysql-community-libs.x86_64 0:5.7.21-1.el7                                    
    
     mysql-community-libs-compat.x86_64 0:5.7.21-1.el7                             
    
     mysql-community-server.x86_64 0:5.7.21-1.el7                                  
    
    Dependency Installed:
    
     libaio.x86_64 0:0.3.109-13.el7                                                
    
     mysql-community-client.x86_64 0:5.7.21-1.el7                                  
    
     mysql-community-common.x86_64 0:5.7.21-1.el7                                  
    
    Replaced:
    
     mariadb-libs.x86_64 1:5.5.56-2.el7                                            
    
    Complete!
    
    

    执行以下命令检查安装结果:

    $ rpm -qa|grep -i mysql

    输出

    
    mysql57-community-release-el7-11.noarch
    
    mysql-community-common-5.7.21-1.el7.x86_64
    
    mysql-community-client-5.7.21-1.el7.x86_64
    
    mysql-community-server-5.7.21-1.el7.x86_64
    
    mysql-community-libs-5.7.21-1.el7.x86_64
    
    mysql-community-libs-compat-5.7.21-1.el7.x86_64
    
    

    mysql操作

    查看mysql状态

    使用$ service mysqld status查看mysql状态:

    
    ● mysqld.service - MySQL Server
    
     Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
    
     Active: inactive (dead)
    
     Docs: man:mysqld(8)
    
     http://dev.mysql.com/doc/refman/en/using-systemd.html
    
    

    可以看到服务未启动.

    启动mysql

    使用$ service mysqld start启动mysql:

    
    Redirecting to /bin/systemctl start mysqld.service
    
    

    此时再执行$ service mysqld status输出

    
    Redirecting to /bin/systemctl status mysqld.service
    
    ● mysqld.service - MySQL Server
    
     Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
    
     Active: active (running) since Wed 2018-04-04 15:43:41 CST; 1min 2s ago
    
     Docs: man:mysqld(8)
    
     http://dev.mysql.com/doc/refman/en/using-systemd.html
    
     Process: 2588 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
    
     Process: 2515 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
    
     Main PID: 2593 (mysqld)
    
     CGroup: /system.slice/mysqld.service
    
     └─2593 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mys...
    
    Apr 04 15:43:36 izbp1g0mp7j4yo2s46yt56z systemd[1]: Starting MySQL Server...
    
    Apr 04 15:43:41 izbp1g0mp7j4yo2s46yt56z systemd[1]: Started MySQL Server.
    
    

    连接mysql

    使用$ mysql -u root登录mysql,结果输出

    
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
    
    

    解决方法是编辑/etc/my.cnf:

    $ vim /etc/my.cnf

    在文件最下面添加

    skip-grant-tables

    停止服务器$ service mysqld stop,然后重新启动``$ service mysqld start`.

    此时使用$ mysql -u root可以直接登录mysql,修改root用户的密码:

    
    $ use mysql;
    
    $ update mysql.user set authentication_string=password('123') where user='root';
    
    $ flush privileges;
    
    

    此时恢复/etc/my.cnf,删除skip-grant-tables.然后使用$ service mysqld restart重启mysql服务.之后可以使用使用$ mysql -u root -p触发登录操作,输入密码"123"即可

    Bug解决

    You must reset your password using ALTER USER statement

    后来执行其它mysql命令时出现错误:

    
    You must reset your password using ALTER USER statement before executing this statement
    
    

    于是执行命令更新了一下密码为"Root@123456":

    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root@123456';

    其它参考Reset MySQL root password using ALTER USER statement after install on Mac

    Your password does not satisfy the current policy requirements

    mysql5.7的默认密码验证等级是1(medium),要求包含数字、大/小写字母和特殊字符.参考ERROR 1819 (HY000): Your password does not satisfy the current policy requirements.密码检验策略有3个取值,0、1和2,对应lower、medium和strong

    解决方法是先按要求改变密码为"Root@123456",参照上一步.

    然后改变validate_password_policy:

    `mysql> set global validate_password_policy=0;

    Query OK, 0 rows affected (0.00 sec)`

    此时密码验证等级为0,只会验证长度.查看长度:

    mysql> select @@validate_password_length;

    输出

    
    +----------------------------+
    
    | @@validate_password_length |
    
    +----------------------------+
    
    | 8 |
    
    +----------------------------+
    
    1 row in set (0.00 sec)
    
    

    修改长度验证为3:

    set global validate_password_length=3;

    此时再用select @@validate_password_length;看密码长度限制:

    
    +----------------------------+
    
    | @@validate_password_length |
    
    +----------------------------+
    
    | 4 |
    
    +----------------------------+
    
    1 row in set (0.00 sec)
    
    

    可以看到无论怎么修改,密码最短必须4位.

    此时修改密码为1234:

    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '1234';

    mysql远程连接

    查看端口

    完成上面所有工作以后,使用navicat连接mysqsl,发现一直连接失败.使用netstat -tln查看服务的端口:

    
    Active Internet connections (only servers)
    
    Proto Recv-Q Send-Q Local Address  Foreign Address  State      
    
    tcp 0 0 0.0.0.0:22 0.0.0.0:*  LISTEN     
    
    tcp6  0 0 :::21  :::* LISTEN     
    
    tcp6  0 0 :::3306  :::* LISTEN
    
    

    发现了3306,说明端口没问题.

    更新权限表

    端口没问题,那就可能是权限表中用户的配置限定了ip,执行下面的命令允许root用户从任意ip访问数据库:

    
    mysql> use mysql;
    
    mysql> update user set host='%' where user='root';
    
    

    host设置为%表示允许从任意ip访问,当然也可以将host指定为某个ip.参考阿里云下配置MySQL远程连接的步骤详解

    最后必须执行下面的命令让刚才的更改立即生效:

    mysql> flush privileges;

    重新使用navicat连接就好了.

    总结: 阿里云上安装的mysql默认只能本机访问,如果需要远程连接需要以上配置才可以.

    参考

    相关文章

      网友评论

        本文标题:阿里云centos7上部署mysql

        本文链接:https://www.haomeiwen.com/subject/qkkclftx.html