mysql安装、卸载

在Linux上安装MySQL数据库,我们可以去其官网上下载MySQL数据库的rpm包。本文通过yum来进行MySQL数据库的安装的

一、卸载掉原有MySQL

[root@yuiwang-gz-web-waxge ~]# rpm -qa|greo mysql

已经安装的会显示:
在这里插入图片描述
如果有安装的话,要先删除: rpm -e 或者 rpm -e nodeps

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

在删除完以后我们可以通过 rpm -qa | grep mysql 命令来查看mysql是否已经卸载成功

[root@iZ23jffs85aZ /]#  rpm -qa | grep mysql
php-mysql-5.3.3-40.el6_6.x86_64
mysql-libs-5.1.73-3.el6_5.x86_64

二、通过yum来进行MySQL的安装

[root@yuiwang-gz-web-waxge ~]# yum list|grep mysql

可以通过输入 yum install -y mysql-server mysql mysql-devel 命令将mysql mysql-server mysql-devel都安装好(注意:安装MySQL时我们并不是安装了MySQL客户端就相当于安装好了MySQL数据库了,我们还需要安装mysql-server服务端才行)

[root@yuiwang-gz-web-waxge ~]# yum install -y mysql-server mysql mysql-devel

运行后如下:在这里插入图片描述
可以通过如下命令,查看刚安装好的mysql-server的版本:rpm -qi mysql-server

[root@yuiwang-gz-web-waxge ~]# rpm -qi mysql-server

在这里插入图片描述

三、MySQL数据库的初始化及相关配置

通过输入 service mysqld start 命令就可以启动我们的MySQL服务
注意:如果我们是第一次启动MySQL服务,MySQL服务器首先会进行初始化的配置,如:

[root@yuiwang-gz-web-waxge ~]# service mysqld start
Initializing MySQL database:  Installing MySQL system tables...
OK
Filling help tables...
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 yuiwang-gz-web-waxge.vclound.com 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 with the /usr/bin/mysqlbug script!

                                                           [  OK  ]
Starting mysqld:                                           [  OK  ]

第一次启动MySQL服务器以后会提示非常多的信息,目的就是对MySQL数据库进行初始化操作,当我们再次重新启动MySQL服务时,就不会提示这么多信息了,如:

[root@yuiwang-gz-web-waxge /]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]

在使用MySQL数据库时,都得首先启动mysqld服务,我们可以 通过 chkconfig —list | grep mysqld 命令来查看mysql服务是不是开机自动启动,如:

[root@yuiwang-gz-web-waxge /]#  chkconfig --list | grep mysqld
mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off

如果mysqld服务并没有开机自动启动,我们当然可以通过 chkconfig mysqld on 命令来将其设置成开机启动

MySQL数据库安装完以后只会有一个root管理员账号,但是此时的root账号还并没有为其设置密码,在第一次启动mysql服务时,会进行数据库的一些初始化工作,在输出的一大串信息中,我们看到有这样一行信息 :

/usr/bin/mysqladmin -u root password 'new-password'  // 为root账号设置密码

所以我们可以通过 该命令来给我们的root账号设置密码(注意:这个root账号是mysql的root账号,非Linux的root账号)

[root@yuiwang-gz-web-waxge /]# mysqladmin -u root password 'root'

此时我们就可以通过 mysql -u root -p 命令来登录我们的mysql数据库了

[root@yuiwang-gz-web-waxge /]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

四、mysql数据库的主要配置文件

/etc/my.cnf 这是mysql的主配置文件
/var/lib/mysql mysql数据库的数据库文件存放位置

[root@iZ23jffs85aZ /]# cd /var/lib/mysql
[root@iZ23jffs85aZ mysql]# ls -l
total 20488
-rw-rw---- 1 mysql mysql 10485760 Mar 12 14:58 ibdata1
-rw-rw---- 1 mysql mysql  5242880 Mar 12 14:58 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 Mar 12 14:57 ib_logfile1
drwx------ 2 mysql mysql     4096 Mar 12 14:57 mysql  // 这两个是mysql数据库安装时默认的两个数据库文件
srwxrwxrwx 1 mysql mysql        0 Mar 12 14:58 mysql.sock
drwx------ 2 mysql mysql     4096 Mar 12 14:57 test   // 这两个是mysql数据库安装时默认的两个数据库文件
[root@iZ23jffs85aZ mysql]#

可以自己创建一个数据库,来验证一下该数据库文件的存放位置

[root@yuiwang-gz-web-waxge mysql]# cd /
[root@yuiwang-gz-web-waxge /]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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> create database littlebaby;
Query OK, 1 row affected (0.00 sec)

mysql> quit
Bye
[root@yuiwang-gz-web-waxge /]# cd /var/lib/mysql/
[root@yuiwang-gz-web-waxge mysql]# ls -l
total 20492
-rw-rw---- 1 mysql mysql 10485760 Feb 27 19:51 ibdata1
-rw-rw---- 1 mysql mysql  5242880 Feb 27 19:51 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 Feb 27 18:44 ib_logfile1
drwx------ 2 mysql mysql     4096 Feb 27 20:08 littlebaby // 这个就是我们刚自己创建的littlebaby 数据库
drwx------ 2 mysql mysql     4096 Feb 27 18:44 mysql
srwxrwxrwx 1 mysql mysql        0 Feb 27 19:51 mysql.sock
drwx------ 2 mysql mysql     4096 Feb 27 18:44 test

/var/log mysql数据库的日志输出存放位置
我们的mysql数据库的一些日志输出存放位置都是在/var/log这个目录下

[root@yuiwang-gz-web-waxge mysql]# cd /var/log
[root@yuiwang-gz-web-waxge log]# ls
anaconda.ifcfg.log    anaconda.storage.log  audit     cloud-init.log         cron       dracut.log     iptraf   messages      ntpstats  sa       tallylog
anaconda.log          anaconda.syslog       boot.log  cloud-init-output.log  dmesg      elasticsearch  lastlog  miguel_agent  prelink   secure   wtmp
anaconda.program.log  anaconda.yum.log      btmp      ConsoleKit             dmesg.old  glusterfs      maillog  mysqld.log    puppet    spooler  yum.log

其中mysqld.log 这个文件就是我们存放我们跟mysql数据库进行操作而产生的一些日志信息,通过查看该日志文件,我们可以从中获得很多信息。
因为我们的mysql数据库是可以通过网络访问的,并不是一个单机版数据库,其中使用的协议是 tcp/ip 协议,我们都知道mysql数据库绑定的端口号是 3306 ,所以我们可以通过 netstat -anp 命令来查看一下,Linux系统是否在监听 3306 这个端口号:

[root@yuiwang-gz-web-waxge mysql]# netstat -anp|more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:10050               0.0.0.0:*                   LISTEN      1371/zabbix_agentd  
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      4126/mysqld         //这里就是Linux系统监听的3306端口号就是我们的mysql数据库。
tcp        0      0 0.0.0.0:10029               0.0.0.0:*                   LISTEN      1333/python2.7      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1309/sshd           
tcp        0     52 10.199.188.221:22           10.100.71.50:50476          ESTABLISHED 3553/sshd           
tcp        0      0 ::ffff:10.199.188.221:8066  :::*                        LISTEN      3446/java           
tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN      3446/java           
tcp        0      0 :::55687                    :::*                        LISTEN      3446/java           
tcp        0      0 :::43820                    :::*                        LISTEN      3446/java           
tcp        0      0 :::10029                    :::*                        LISTEN      1333/python2.7      
tcp        0      0 :::8080                     :::*                        LISTEN      3446/java           
tcp        0      0 ::ffff:10.199.188.221:9200  :::*                        LISTEN      25008/java      

五、mysql的启停小脚本

1)命令:service mysqld start | stop
2)可使用脚本:

#!/bin/sh
. /etc/init.d/functions
 
if [ "$1" = "stop" ] ; then
   /usr/bin/mysqladmin --defaults-file=/etc/my.cnf -uroot  shutdown  > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        success; echo "Stopping mysqld:"
    else
        failure; echo "Stopping mysqld:"
    fi
elif [ "$1" = "restart" ]; then
   /usr/bin/mysqladmin  --defaults-file=/etc/my.cnf -uroot  shutdown  > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        success; echo "Stopping mysqld:"
    else
        failure; echo "Stopping mysqld:"
    fi
   /usr/bin/mysqld_safe --defaults-file=/etc/my.cnf  > /dev/null 2>&1 &
    if [ $? -eq 0 ]; then
        success; echo "Starting mysqld:"
    else
        failure; echo "Starting mysqld:"
    fi
elif [ "$1" = "start" ]; then
   /usr/bin/mysqld_safe --defaults-file=/etc/my.cnf  > /dev/null 2>&1 &
    if [ $? -eq 0 ]; then
        success; echo "Starting mysqld:"
    else
        failure; echo "Starting mysqld:"
fi
elif [ "$1" = "status" ];then
        if [ -n "`/sbin/pidof mysqld`" ]; then
                echo -n "mysqld: already running."
        else
                echo -n "mysqld: is stopped."
        fi
        echo
else
        echo "usage: $0 start|stop|restart|status"
fi

六、mysql导入、导出

在已部署的机器导出需要的数据库(源数据库)

   #  cd /root
   #  mysqldump -uroot  a_wap > wap.sql
   #  ls

将wap.sql文件拷贝至部署的机器,进入mysql,创建b_wap数据库,导入wap.sql文件

mysql> create database b_wap;
mysql> use b_wap;
mysql> source ~/wap.sql;
mysql> show tables;      #导入成功后查看导入的表

七、设置远程客户端的访问权限

当访问数据库出现如下错误时

需要进行授权
账号:ATEST
密码:BTEST
使用语句进行授权:
GRANT ALL PRIVILEGES ON . TO ‘ATEST’@’%’ IDENTIFIED BY BTEST’ WITH GRANT OPTION;
flush privileges;

grant all privileges on  *.* TO 'ATEST'@'%' IDENTIFIED BY 'BTEST' WITH GRANT OPTION
flush privileges;

mysql> grant all privileges on *.* to 'ATEST'@'%' identified by 'BTEST' with grant option;
Query OK, 0 rows affected (0.00 sec)

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

mysql> exit
Bye

授权成功后,远程客户端能正常连接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值