阿里云中安装MySQL数据库的正确方式

10 篇文章 1 订阅
yum快速安装mysql
[root@iZwz95awar071umsfgn716Z ~]# cd /  #########进入根目录
[root@iZwz95awar071umsfgn716Z /]# rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm      ####新增yum源
Retrieving http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-release-el7-5    ################################# [100%]
[root@iZwz95awar071umsfgn716Z /]# yum repolist enabled | grep "mysql.*-community.*"   #####查看可用的mysql版本
mysql-connectors-community/x86_64 MySQL Connectors Community                  45
mysql-tools-community/x86_64      MySQL Tools Community                       59
mysql56-community/x86_64          MySQL 5.6 Community Server                 378
[root@iZwz95awar071umsfgn716Z /]# yum -y install mysql-community-server   
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.6.39-2.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.6.39-2.el7 for package: mysql-community-server-5.6.39-2.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.6.10 for package: mysql-community-server-5.6.39-2.el7.x86_64
--> Processing Dependency: perl(Data::Dumper) for package: mysql-community-server-5.6.39-2.el7.x86_64
--> Processing Dependency: perl(DBI) for package: mysql-community-server-5.6.39-2.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.6.39-2.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.6.10 for package: mysql-community-client-5.6.39-2.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.6.39-2.el7 will be installed
---> Package perl-DBI.x86_64 0:1.627-4.el7 will be installed
--> Processing Dependency: perl(RPC::PlServer) >= 0.2001 for package: perl-DBI-1.627-4.el7.x86_64
--> Processing Dependency: perl(RPC::PlClient) >= 0.2000 for package: perl-DBI-1.627-4.el7.x86_64
---> Package perl-Data-Dumper.x86_64 0:2.145-3.el7 will be installed
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.56-2.el7 will be obsoleted
---> Package mysql-community-libs.x86_64 0:5.6.39-2.el7 will be obsoleting
---> Package perl-PlRPC.noarch 0:0.2020-14.el7 will be installed
--> Processing Dependency: perl(Net::Daemon) >= 0.13 for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Net::Daemon::Test) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Net::Daemon::Log) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Compress::Zlib) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Running transaction check
---> Package perl-IO-Compress.noarch 0:2.061-2.el7 will be installed
--> Processing Dependency: perl(Compress::Raw::Zlib) >= 2.061 for package: perl-IO-Compress-2.061-2.el7.noarch
--> Processing Dependency: perl(Compress::Raw::Bzip2) >= 2.061 for package: perl-IO-Compress-2.061-2.el7.noarch
---> Package perl-Net-Daemon.noarch 0:0.48-5.el7 will be installed
--> Running transaction check
---> Package perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 will be installed
---> Package perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 will be installed
--> Finished Dependency Resolution


Dependencies Resolved


============================================================================================
 Package                      Arch        Version              Repository              Size
============================================================================================
Installing:
 mysql-community-libs         x86_64      5.6.39-2.el7         mysql56-community      2.0 M
     replacing  mariadb-libs.x86_64 1:5.5.56-2.el7
 mysql-community-server       x86_64      5.6.39-2.el7         mysql56-community       59 M
Installing for dependencies:
 mysql-community-client       x86_64      5.6.39-2.el7         mysql56-community       19 M
 mysql-community-common       x86_64      5.6.39-2.el7         mysql56-community      257 k
 perl-Compress-Raw-Bzip2      x86_64      2.061-3.el7          base                    32 k
 perl-Compress-Raw-Zlib       x86_64      1:2.061-4.el7        base                    57 k
 perl-DBI                     x86_64      1.627-4.el7          base                   802 k
 perl-Data-Dumper             x86_64      2.145-3.el7          base                    47 k
 perl-IO-Compress             noarch      2.061-2.el7          base                   260 k
 perl-Net-Daemon              noarch      0.48-5.el7           base                    51 k
 perl-PlRPC                   noarch      0.2020-14.el7        base                    36 k


Transaction Summary
============================================================================================
Install  2 Packages (+9 Dependent packages)


Total download size: 82 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql56-community/packages/mysql-community-common-5.6.39-2.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Public key for mysql-community-common-5.6.39-2.el7.x86_64.rpm is not installed
(1/11): mysql-community-common-5.6.39-2.el7.x86_64.rpm               | 257 kB  00:00:00     
(2/11): mysql-community-libs-5.6.39-2.el7.x86_64.rpm                 | 2.0 MB  00:00:00     
(3/11): perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm               |  32 kB  00:00:00     
(4/11): perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm                |  57 kB  00:00:00     
(5/11): perl-Data-Dumper-2.145-3.el7.x86_64.rpm                      |  47 kB  00:00:00     
(6/11): perl-IO-Compress-2.061-2.el7.noarch.rpm                      | 260 kB  00:00:00     
(7/11): perl-Net-Daemon-0.48-5.el7.noarch.rpm                        |  51 kB  00:00:00     
(8/11): perl-DBI-1.627-4.el7.x86_64.rpm                              | 802 kB  00:00:00     
(9/11): perl-PlRPC-0.2020-14.el7.noarch.rpm                          |  36 kB  00:00:00     
(10/11): mysql-community-client-5.6.39-2.el7.x86_64.rpm              |  19 MB  00:00:03     
(11/11): mysql-community-server-5.6.39-2.el7.x86_64.rpm              |  59 MB  00:00:07     
--------------------------------------------------------------------------------------------
Total                                                       9.0 MB/s |  82 MB  00:00:09     
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    : mysql-community-release-el7-5.noarch (installed)
 From       : file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : perl-Data-Dumper-2.145-3.el7.x86_64                                     1/12 
  Installing : mysql-community-common-5.6.39-2.el7.x86_64                              2/12 
  Installing : mysql-community-libs-5.6.39-2.el7.x86_64                                3/12 
  Installing : mysql-community-client-5.6.39-2.el7.x86_64                              4/12 
  Installing : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64                             5/12 
  Installing : perl-Net-Daemon-0.48-5.el7.noarch                                       6/12 
  Installing : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64                              7/12 
  Installing : perl-IO-Compress-2.061-2.el7.noarch                                     8/12 
  Installing : perl-PlRPC-0.2020-14.el7.noarch                                         9/12 
  Installing : perl-DBI-1.627-4.el7.x86_64                                            10/12 
  Installing : mysql-community-server-5.6.39-2.el7.x86_64                             11/12 
  Erasing    : 1:mariadb-libs-5.5.56-2.el7.x86_64                                     12/12 
warning: file /usr/lib64/mysql/plugin/mysql_clear_password.so: remove failed: No such file or directory
warning: file /usr/lib64/mysql/plugin/dialog.so: remove failed: No such file or directory
warning: file /usr/lib64/mysql/libmysqlclient.so.18.0.0: remove failed: No such file or directory
warning: file /etc/my.cnf.d/mysql-clients.cnf: remove failed: No such file or directory
  Verifying  : mysql-community-server-5.6.39-2.el7.x86_64                              1/12 
  Verifying  : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64                              2/12 
  Verifying  : perl-Net-Daemon-0.48-5.el7.noarch                                       3/12 
  Verifying  : mysql-community-common-5.6.39-2.el7.x86_64                              4/12 
  Verifying  : perl-Data-Dumper-2.145-3.el7.x86_64                                     5/12 
  Verifying  : mysql-community-libs-5.6.39-2.el7.x86_64                                6/12 
  Verifying  : perl-PlRPC-0.2020-14.el7.noarch                                         7/12 
  Verifying  : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64                             8/12 
  Verifying  : perl-DBI-1.627-4.el7.x86_64                                             9/12 
  Verifying  : perl-IO-Compress-2.061-2.el7.noarch                                    10/12 
  Verifying  : mysql-community-client-5.6.39-2.el7.x86_64                             11/12 
  Verifying  : 1:mariadb-libs-5.5.56-2.el7.x86_64                                     12/12 


Installed:
  mysql-community-libs.x86_64 0:5.6.39-2.el7  mysql-community-server.x86_64 0:5.6.39-2.el7 


Dependency Installed:
  mysql-community-client.x86_64 0:5.6.39-2.el7 mysql-community-common.x86_64 0:5.6.39-2.el7
  perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 
  perl-DBI.x86_64 0:1.627-4.el7                perl-Data-Dumper.x86_64 0:2.145-3.el7       
  perl-IO-Compress.noarch 0:2.061-2.el7        perl-Net-Daemon.noarch 0:0.48-5.el7         
  perl-PlRPC.noarch 0:0.2020-14.el7           


Replaced:
  mariadb-libs.x86_64 1:5.5.56-2.el7                                                        


Complete!
[root@iZwz95awar071umsfgn716Z /]# systemctl enable mysqld #######启动mysql服务进程
[root@iZwz95awar071umsfgn716Z /]# systemctl start mysqld #######配置mysql(设置密码等)

[root@iZwz95awar071umsfgn716Z ~]# mysql_secure_installation  #######配置初始信息





NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.


Enter current password for root (enter for none): <–初次运行直接回车
OK, successfully used password, moving on...


Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.


Set root password? [Y/n] Y<– 是否设置root用户密码,输入y并回车或直接回车
New password:  <– 设置root用户的密码
Re-enter new password:  <– 再输入一次你设置的密码
Password updated successfully!
Reloading privilege tables..
 ... Success!




By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.


Remove anonymous users? [Y/n]  <– 是否删除匿名用户,生产环境建议删除,所以直接回车
 ... Success!


Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.


Disallow root login remotely? [Y/n]  <–是否禁止root远程登录,根据自己的需求选择Y/n并回车,建议禁止
 ... Success!


By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.


Remove test database and access to it? [Y/n]  <– 是否删除test数据库,直接回车
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - Removing privileges on test database...
 ... Success!


Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.


Reload privilege tables now? [Y/n] <– 是否重新加载权限表,直接回车
 ... Success!








All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.


Thanks for using MySQL!




Cleaning up...

以上已经完成安装。

配置和启动


        数据库字符集设置为utf8:
        MySQL 配置文件/etc/my.cnf中加入default-character-set=utf8
    
        启动 MySQL 服务: 
        service mysqld start或者/etc/init.d/mysqld start


        设置开机启动:
        添加开机启动:chkconfig --add mysqld;
        开机启动:chkconfig mysqld on;

        查看开机启动设置是否成功:

chkconfig --list | grep mysql* mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭停止:



        停止 MySQL 服务: 

        service mysqld stop


MySQL 登陆等权限设置


        登录 MySQL 数据库:
        mysql -u root password 123456(刚刚安装时设置的密码)


        首次使用创建root管理员和密码:
        (mysql -u root -p 直接加上密码。或者是回车后输入密码都可以)
        mysql -u root;
        use mysql ;
        update user set password=password("123456") where user="root";
        flush privileges;


        忘记密码:
        service mysqld stop;
        mysqld_safe --user=root --skip-grant-tables;
        这一步骤执行的时候不会出现新的命令行,你需要重新打开一个窗口执行下面的命令
        mysql -u root;
        use mysql ;
        update user set password=password("123456") where user="root";  -->修改root 的密码为123456
        flush privileges;  


        如果需要外网可以访问需要再做如下设置:        授权用户可以从远程登陆
        grant all PRIVILEGES on *.* to root@'%'  identified by 'pwd123';
        flush privileges ;

[root@iZwz95awar071umsfgn716Z ~]# mysql -u root -p  ####进入mysql
Enter password:         ####输入刚刚设置的密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.6.39 MySQL Community Server (GPL)


Copyright (c) 2000, 2018, 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> use mysql;       ####使用mysql数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A


Database changed
mysql> select host,user,password from user;       #####查看user表
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *80A84E4759FEC49969DA411756A51BB3FEC9B72C |
| 127.0.0.1 | root | *80A84E4759FEC49969DA411756A51BB3FEC9B72C |
| ::1       | root | *80A84E4759FEC49969DA411756A51BB3FEC9B72C |
+-----------+------+-------------------------------------------+
3 rows in set (0.00 sec)

可以看到在user表中已创建的root用户。host字段表示登录的主机,其值可以用IP,也可用主机名,

   (1)有时想用本地IP登录,那么可以将以上的Host值改为自己的Ip即可。

实现远程连接(授权法)
   将host字段的值改为%就表示在任何客户端机器上能以root用户登录到mysql服务器,建议在开发时设为%。   
   update user set host = '%' where user = 'root';  

   将权限改为ALL PRIVILEGES


//修改访问权限,让其他计算机也能访问

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION; -->yourpassword 此处输入自己设置的密码
Query OK, 0 rows affected (0.00 sec)


mysql> select host,user,password from user;
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| %         | root | *D4CECBA84F0A507325CD8AA82FCDE04EA8BE56B0 |
| 127.0.0.1 | root | *80A84E4759FEC49969DA411756A51BB3FEC9B72C |
| ::1       | root | *80A84E4759FEC49969DA411756A51BB3FEC9B72C |
+-----------+------+-------------------------------------------+
3 rows in set (0.00 sec)





  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值