第一步:卸载原有MySQL
[root@localhost ~]# rpm -qa| grep mysql //这个命令就会查看该操作系统上是否已经安装了mysql数据库
有的话,我们就通过 rpm -e 命令 或者 rpm -e --nodeps 命令来卸载掉
注意:请替换成自己linux系统自带的mysql版本
[root@localhost ~]# rpm -qa| grep mysql
mysql-libs-5.1.71-1.el6.x86_64
[root@localhost ~]# rpm -e --nodeps mysql-libs-5.1.71-1.el6.x86_64
在执行完删除命令后,再次检查是否删除完成,如下显示则表示删除成功
[root@localhost ~]# rpm -qa| grep mysql
[root@localhost ~]#
到这里,删除就完成了,接下来开始安装自己的mysql
第二步:使用yum来进行mysql5.5的安装
首先我们使用yum list | grep mysql 查看可供下载的mysql版本
[root@localhost ~]# yum list | grep mysql
可供下载的版本如下所示:
[root@localhost ~]# yum list | grep mysql
apr-util-mysql.x86_64 1.3.9-3.el6_0.1 base
bacula-director-mysql.x86_64 5.0.0-13.el6 base
bacula-storage-mysql.x86_64 5.0.0-13.el6 base
dovecot-mysql.x86_64 1:2.0.9-22.el6 base
freeradius-mysql.x86_64 2.2.6-7.el6_9 base
libdbi-dbd-mysql.x86_64 0.8.3-5.1.el6 base
mod_auth_mysql.x86_64 1:3.0.0-11.el6_0.1 base
mysql.x86_64 5.1.73-8.el6_8 base
mysql-bench.x86_64 5.1.73-8.el6_8 base
mysql-connector-java.noarch 1:5.1.17-6.el6 base
mysql-connector-odbc.x86_64 5.1.5r1144-7.el6 base
mysql-devel.i686 5.1.73-8.el6_8 base
mysql-devel.x86_64 5.1.73-8.el6_8 base
mysql-embedded.i686 5.1.73-8.el6_8 base
mysql-embedded.x86_64 5.1.73-8.el6_8 base
mysql-embedded-devel.i686 5.1.73-8.el6_8 base
mysql-embedded-devel.x86_64 5.1.73-8.el6_8 base
mysql-libs.i686 5.1.73-8.el6_8 base
mysql-libs.x86_64 5.1.73-8.el6_8 base
mysql-server.x86_64 5.1.73-8.el6_8 base
mysql-test.x86_64 5.1.73-8.el6_8 base
pcp-pmda-mysql.x86_64 3.10.9-9.el6 base
php-mysql.x86_64 5.3.3-49.el6 base
qt-mysql.i686 1:4.6.2-28.el6_5 base
qt-mysql.x86_64 1:4.6.2-28.el6_5 base
rsyslog-mysql.x86_64 5.8.10-12.el6 base
rsyslog7-mysql.x86_64 7.4.10-7.el6 base
[root@localhost ~]#
安装服务端
[root@localhost ~]# yum install -y mysql-server mysql mysql-deve
出现如下画面,表示安装ok
Installed:
mysql.x86_64 0:5.1.73-8.el6_8 mysql-server.x86_64 0:5.1.73-8.el6_8
Dependency Installed:
mysql-libs.x86_64 0:5.1.73-8.el6_8 perl-DBD-MySQL.x86_64 0:4.013-3.el6 perl-DBI.x86_64 0:1.609-4.el6
Complete!
[root@localhost ~]#
此时我们使用 rpm -qi mysql-server 查看安装的mysql版本
[root@localhost ~]# rpm -qi mysql-server
Name : mysql-server Relocations: (not relocatable)
Version : 5.1.73 Vendor: CentOS
卧槽版本居然是5.1,看到这里有人心想,说好的5.5呢?别急,往下看
现在去新增两个repo
第一个:rpm -Uvh http://mirror.steadfast.net/epel/6/i386/epel-release-6-8.noarch.rpm
[root@localhost ~]# rpm -Uvh http://mirror.steadfast.net/epel/6/i386/epel-release-6-8.noarch.rpm
Retrieving http://mirror.steadfast.net/epel/6/i386/epel-release-6-8.noarch.rpm
warning: /var/tmp/rpm-tmp.kotdxw: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
1:epel-release ########################################### [100%]
[root@localhost ~]#
第二个:rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
[root@localhost ~]# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
Retrieving http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
warning: /var/tmp/rpm-tmp.iZmOBA: Header V4 DSA/SHA1 Signature, key ID 00f97f56: NOKEY
Preparing... ########################################### [100%]
1:remi-release ########################################### [100%]
[root@localhost ~]#
.现在查看是否增加成功了
[root@localhost ~]# yum --enablerepo=remi,remi-test list mysql mysql-server
出现以下画面,表示安装成功
[root@localhost ~]# yum --enablerepo=remi,remi-test list mysql mysql-server
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
epel/metalink | 5.2 kB 00:00
* base: mirrors.njupt.edu.cn
* epel: mirrors.yun-idc.com
* extras: mirrors.aliyun.com
* remi: mirrors.tuna.tsinghua.edu.cn
* remi-safe: mirrors.tuna.tsinghua.edu.cn
* remi-test: mirrors.tuna.tsinghua.edu.cn
* updates: mirrors.huaweicloud.com
epel | 3.2 kB 00:00
epel/primary | 3.2 MB 00:00
epel 12515/12515
remi | 2.9 kB 00:00
remi/primary_db | 2.2 MB 00:01
remi-safe | 2.9 kB 00:00
remi-safe/primary_db | 1.1 MB 00:00
remi-test | 2.9 kB 00:00
remi-test/primary_db | 513 kB 00:00
Installed Packages
mysql.x86_64 5.1.73-8.el6_8 @base
mysql-server.x86_64 5.1.73-8.el6_8 @base
Available Packages
mysql.x86_64 5.5.61-1.el6.remi remi
mysql-server.x86_64 5.5.61-1.el6.remi remi
[root@localhost ~]#
接下来就是安装5.5了
[root@localhost ~]# yum --enablerepo=remi,remi-test install mysql mysql-server
出现以下画面,输入 y
出现如下画面,表示安装ok
Dependency Installed:
compat-mysql51.x86_64 0:5.1.73-1.el6.remi
Updated:
mysql.x86_64 0:5.5.61-1.el6.remi mysql-server.x86_64 0:5.5.61-1.el6.remi
Dependency Updated:
mysql-libs.x86_64 0:5.5.61-1.el6.remi postfix.x86_64 2:2.6.6-8.el6
Complete!
[root@localhost ~]#
此时我们再来检测mysql的版本
[root@localhost ~]# rpm -qi mysql-server
Name : mysql-server Relocations: (not relocatable)
Version : 5.5.61 Vendor: Remi Collet
第三步:mysql数据库的初始化及相关配置
启动mysql
[root@localhost ~]# service mysqld start
注意:如果我们是第一次启动mysql服务,mysql服务器首先会进行初始化的配置,如:
[root@localhost ~]# service mysqld start
Initializing MySQL database: Installing MySQL system tables...
181021 5:22:28 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
181021 5:22:28 [Note] /usr/libexec/mysqld (mysqld 5.5.61) starting as process 25874 ...
OK
Filling help tables...
181021 5:22:28 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
181021 5:22:28 [Note] /usr/libexec/mysqld (mysqld 5.5.61) starting as process 25881 ...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
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'
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 /usr/mysql-test ; perl mysql-test-run.pl
Please report any problems at http://bugs.mysql.com/
[ OK ]
Starting mysqld: [ OK ]
[root@localhost ~]#
再次重新启动mysql服务时,就不会提示这么多信息了
[root@localhost ~]# service mysqld restart //首先重启mysql服务
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
[root@localhost ~]# service mysqld start //第二次,重新启动
Starting mysqld: [ OK ]
[root@localhost ~]#
我们使用mysql,每次都得开启服务,太麻烦接下来设置开机自启。
首先,检查mysql服务是否是开机自启
[root@localhost ~]# chkconfig --list | grep mysqld
mysqld 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@localhost ~]#
很明显,没有开启开机自启,接下来设置自启,出现如下画面,表示设置成功
[root@localhost ~]# chkconfig mysqld on //开机自启的命令
[root@localhost ~]# chkconfig --list | grep mysqld //查看是否开启成功
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost ~]#
mysql数据库安装完以后只会有一个root管理员账号,但是此时的还并没有为root账号设置密码
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.61 MySQL Community Server (GPL) by Remi
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>
进入mysql,为root用户设置密码
mysql> set password=password('root'); //在这里输入你自己的密码即可
Query OK, 0 rows affected (0.00 sec)
[root@localhost ~]#
此时再输入mysql会报错,表示设置密码成功
[root@localhost ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@localhost ~]#
使用刚才设置的密码,登陆mysql,出现如下画面,表示登陆成功
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.61 MySQL Community Server (GPL) by Remi
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>
第四步:远程连接mysql
查看user表,此时很明显root用户只允许本机访问
mysql> use 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 user,password,host from user;
+------+-------------------------------------------+-----------------------+
| user | password | host |
+------+-------------------------------------------+-----------------------+
| root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | localhost |
| root | | localhost.localdomain |
| root | | 127.0.0.1 |
| root | | ::1 |
| | | localhost |
| | | localhost.localdomain |
+------+-------------------------------------------+-----------------------+
接下来给root用户赋权,不然远程无法访问
mysql> use mysql
Database changed
mysql> grant all privileges on *.* to root@'%' identified by "root"; //选择到mysql数据库,进行赋权操作
Query OK, 0 rows affected (0.00 sec)
mysql>
再次去查看root的权限,很明显赋权成功
mysql> select user,password,host from user;
+------+-------------------------------------------+-----------------------+
| user | password | host |
+------+-------------------------------------------+-----------------------+
| root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | localhost |
| root | | localhost.localdomain |
| root | | 127.0.0.1 |
| root | | ::1 |
| | | localhost |
| | | localhost.localdomain |
| root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | % |
+------+-------------------------------------------+-----------------------+
7 rows in set (0.01 sec)
mysql>
到这里,你任然无法远程连接
接下来查看防火墙是否开放3306端口,很明显并没有
[root@localhost ~]# vi /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
接下来按 a,进入编辑模式
新增一条:-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
使用Esc退出编辑模式,输入 :(冒号),然后输入wq保存文件。
接下来重启防火墙
[root@localhost ~]# /etc/rc.d/init.d/iptables restart
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: iptables-restore: line 11 failed
[FAILED]
[root@localhost ~]#
第五步:使用SQLyog来远程连接mysql
使用 ifconfig 查看ip
[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:71:89:7D
inet addr:192.168.176.128 Bcast:192.168.176.255 Mask:255.255.255.0
登陆SQLyog,出现以下画面,表示连接成功