Cent OS 用Yum安装MySql并配置远程登录

  • 准备工作

yum 命令:用于添加/删除/更新RPM包,自动解决包的依赖问题以及系统更新升级
yum --help # 只是部分 查看全部请自己动手
-y # 自动应答yes
update[RPM包] # 更新包
list # 可安装和可更新的RPM包

来源: <http://linux.51yip.com/search/yum>

检查软甲是否安装,如果已存在卸载该软件


rpm -qa|grep mysql # 检查软件是否安装

卸载

rpm -e mysql  # 普通删除模式
rpm -e --nodeps mysql  # 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除

查看yum上提供的mysql数据库可下载的版本:



yum list|grep mysql # 得到yum服务器上mysql数据库的可下载版本信息


  • 开始安装


yum install -y mysql-server mysql mysql-deve



过程解析: 注:安装时选择-y参数会自动默认选择yes,不会中断,以下信息为不加-y操作



[root@localhost ~]# yum install mysql-server mysql mysql-deve
# 这里加载fastestmirror插件 用于测试选择最快的yum源
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * addons: mirrors.163.com
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
Setting up Install Process
No package mysql-deve available.
# 检查并解决rpm包的依赖性 
Resolving Dependencies
--> Running transaction check
---> Package mysql.i386 0:5.0.95-5.el5_9 set to be updated
--> Processing Dependency: perl(DBI) for package: mysql
---> Package mysql.x86_64 0:5.0.95-5.el5_9 set to be updated
---> Package mysql-server.x86_64 0:5.0.95-5.el5_9 set to be updated
--> Processing Dependency: perl-DBD-MySQL for package: mysql-server
--> Running transaction check
---> Package perl-DBD-MySQL.x86_64 0:3.0007-2.el5 set to be updated
---> Package perl-DBI.x86_64 0:1.52-2.el5 set to be updated
--> Finished Dependency Resolution

# 汇总依赖性解决结果
Dependencies Resolved

================================================================================
 Package                 Arch            Version                 Repository
================================================================================
Installing:
 mysql                   i386            5.0.95-5.el5_9          base          4
 mysql                   x86_64          5.0.95-5.el5_9          base          4
 mysql-server            x86_64          5.0.95-5.el5_9          base          9
Installing for dependencies:
 perl-DBD-MySQL          x86_64          3.0007-2.el5            base          1
 perl-DBI                x86_64          1.52-2.el5              base          6

Transaction Summary
================================================================================
Install       5 Package(s)
Upgrade       0 Package(s)

Total download size: 20 M
Is this ok [y/N]:
键入y继续
Downloading Packages:
(1/5): perl-DBD-MySQL-3.0007-2.el5.x86_64.rpm                 | 148 kB     00:00
(2/5): perl-DBI-1.52-2.el5.x86_64.rpm                         | 600 kB     00:00
(3/5): mysql-5.0.95-5.el5_9.i386.rpm                          | 4.9 MB     00:02
(4/5): mysql-5.0.95-5.el5_9.x86_64.rpm                        | 4.9 MB     00:02
(5/5): mysql-server-5.0.95-5.el5_9.x86_64.rpm                                                                                                             | 9.9 MB     00:12
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                            982 kB/s |  20 MB     00:21
warning: rpmts_HdrFromFdno: Header V3 DSA signature: NOKEY, key ID e8562897
base/gpgkey                                                                                                                                               | 1.5 kB     00:00
Importing GPG key 0xE8562897 "CentOS-5 Key (CentOS 5 Official Signing Key) <[email protected]>" from /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
# 这里报了个警告 我Google了一下 没看懂,只好求助度娘
# 原因是:yum安装了旧版本的GPG keys造成的
# 解决方案:rpm --import /etc/pki/rpm-gpg/RPM* 
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : perl-DBI         1/5
  Installing     : mysql            2/5
  Installing     : perl-DBD-MySQL   3/5
  Installing     : mysql            4/5
  Installing     : mysql-server     5/5

Installed:
  mysql.i386 0:5.0.95-5.el5_9                            
  mysql.x86_64 0:5.0.95-5.el5_9                        
  mysql-server.x86_64 0:5.0.95-5.el5_9

Dependency Installed:
  perl-DBD-MySQL.x86_64 0:3.0007-2.el5                                                        
  perl-DBI.x86_64 0:1.52-2.el5

Complete!



  • 后续配置之MySql安装配置


rpm -qi mysql-server # 查看安装好的mysql-server的版本



第一次启动mysql服务



[root@localhost ~]# service mysqld start
# 初始化mysql数据库
Initializing MySQL database:  Installing MySQL system tables...
OK
Filling help tables...
OK

# 在启动的时候开启mysql服务的提示
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

# 在这里有设置root密码的提示
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

# 这是mysql提供的方便我们配置的脚本
Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
                                                           [  OK  ]
Starting mysqld:                                           [  OK  ]



设置开机启动

chkconfig --list|grep mysqld # 检查mysqld开机启动状态
chkconfig mysqld on # 设置开机启动
chkconfig --list|grep mysqld # 检查mysqld开机启动状态 again



安装后还会有一些数据库的配置,这里我们借助mysql提供的脚本进行配置


cat /usr/bin/mysql_secure_installation # mysql提供的脚本



开始配置


[root@localhost ~]# 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.

# 这里输入本机的linux密码,如果你现在是root身份登录直接回车即可,我第一次安装的时候 傻傻的输了半天
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.

# 设置mysql的root密码 (输入两遍的)
Set root password? [Y/n] Y
New password:
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] n
 ... skipping.

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] n
 ... skipping.

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] Y
 - Dropping test database...
 ... Success!
 - 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] Y
 ... Success!

Cleaning up...

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

Thanks for using MySQL!



设置完毕以后 可以重启下mysql服务 你会发现 不会有第一次启动时候的message


[root@localhost ~]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]



mysql参数


mysql --help # 信息量太大了 自己看看吧 我英语不及格就不献丑了
-u, --user=name     User for login if not current user.
-P, --port=#        Port number to use for connection or 0 for default to, in
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
                      /etc/services, built-in default (3306).



建一个测试库


mysql -u root -p # 从默认的端口root用户访问数据库 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 rows in set (0.01 sec)
mysql> create database pengfei
    -> ; # 创建测试库 大家千万别忘记" ; "
Query OK, 1 row affected (0.00 sec)
mysql> quit
Bye # 太友好了
[root@localhost mysql]# cd /var/lib/mysql # 数据库文件存放目录
[root@localhost mysql]# ls
ibdata1  ib_logfile0  ib_logfile1  mysql  mysql.sock  pengfei 
[root@localhost mysql]# cd pengfei/
[root@localhost pengfei]# ls
db.opt # 数据库文件



安装完了,从客户机测试发现不能连接失败...


  • 后续配置之MySql远程访问

mysql配置


mysql -u root -p # 登录
use mysql; # 选择数据库
select host, user from user; # 查询用户表
update user set host = '%' where user = 'root'; # 修改主机
# 报了个错 ERROR 1062 (23000): Duplicate entry '%-root' for key 1
select host , user from user; # 不过又查询了一下 发现确实改过一个
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your password' WITH GRANT OPTION; # 授权
FLUSH PRIVILEGES; # 刷新MySQL的系统权限相关表,否则会出现拒绝访问 重启mysql也可以的



CentOS 防火墙配置



cd /etc/init.d # 切换到iptables所在目录
iptables -I INPUT 2 -p tcp -m tcp --dport 3306 -j ACCEPT # MySql
iptables -I INPUT 2 -p tcp -m tcp --dport 1521 -j ACCEPT # Oracle
./iptables save # 保存规则
./iptables restart # 重启防火墙
./iptables states # 查看规则



iptables 参数解释:
–I 参数就看成是插入一条规则
–p 指定是什么协议,我们常用的tcp 协议,当然也有udp,例如53端口的DNS
–dport 就是目标端口,当数据从外部进入服务器为目标端口
–sport 数据从服务器出去,则为数据源端口使用 
–j 就是指定是 ACCEPT -接收 或者 DROP 不接收



到现在为止,我们就可以通过客户间远程访问mysql数据库了,小弟已测试通过,如果您安装时遇到错误,欢迎留言讨论,或Google一下,推荐Google一下,更准确.



本文参考:

http://wpguru.co.uk/2012/04/how-to-install-mysql-on-centos-2/
http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html
http://linux.51yip.com/


不积跬步,无以至千里;不积小流,无以成江海。

你可能感兴趣的:(Cent OS 用Yum安装MySql并配置远程登录)