Linux环境下安装mysql

1、确认并卸载mysql

安装mysql之前首先确认系统是否已安装了mysql,如已安装,则可先卸载之。

[root@hezhixiong ~]# rpm -qa | grep mysql  // 查看系统是否已安装mysql
mysql-libs-5.1.71-1.el6.x86_64             // 查询结果
[root@hezhixiong ~]# rpm -e --nodeps mysql-libs-5.1.71-1.el6.x86_64  // 强制删除
[root@hezhixiong ~]# rpm -qa | grep mysql  // 已成功删除了mysql,所以查不到mysql了
[root@hezhixiong ~]#

备注:mysql删除模式有2种:
rpm -e mysqlname        // 普通删除模式
rpm -e --nodeps mysqlname // 强制删除模式,如果普通删除模式提示有依赖文件,则用该命令可以对其进行强制删除

2、mysql安装

在Linux上安装mysql,安装方法有多种,一般是去mysql官网下载(http://dev.mysql.com/downloads/mysql/5.6.html#downloads)并安装之。还有另一种比较方便的方法就是通过yum来安装mysql。

[root@hezhixiong mysql]# yum list | grep mysql  // 查看可供安装mysql版本
……查询到的内容省略……
[root@hezhixiong mysql]# yum install -y mysql-server mysql mysql-devel // 安装mysql服务器和客户端
……安装时输出内容省略……
[root@hezhixiong mysql]# rpm -qi mysql-server // 查看安装后的版本号

3、配置mysql

启动mysql服务

[root@hezhixiong mysql]# service mysql start  // 启动mysql服务
备注:
关闭mysql服务:service mysqld stop
重启mysql服务:service mysqld restart

查看并配置开机自动启动mysql

[root@hezhixiong mysql]# chkconfig --list | grep mysqld             // 查看mysql是否开机自启动
mysqld          0:关闭  1:关闭  2:关闭  3:关闭  4:关闭  5:关闭  6:关闭  // mysql开机不启动
[root@hezhixiong mysql]# chkconfig mysqld on                        // 配置mysql开机启动
[root@hezhixiong mysql]# chkconfig --list | grep mysqld
mysqld          0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭

安装后的mysql只有一个用户root,而此时root的密码为空,安全起见需要为root创建一个密码

[root@hezhixiong mysql]# mysqladmin -u root password 'tom'       // 为root创建密码为tom
[root@hezhixiong mysql]# mysqladmin -uroot -ptom password 'root' // 修改root的秘密为root
[root@hezhixiong mysql]# mysql -uroot -proot                     // 连接mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 12
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>
mysql> show databases;  // 显示所有数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

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

    1> /etc/my.cnf 是mysql的主配置文件

[root@hezhixiong mysql]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

    2> /var/lib/mysql 是mysql数据库存放位置

[root@hezhixiong mysql]# pwd
/var/lib/mysql
[root@hezhixiong mysql]# ll
总用量 20488
-rw-rw----. 1 mysql mysql 10485760 7月  13 19:16 ibdata1
-rw-rw----. 1 mysql mysql  5242880 7月  13 19:16 ib_logfile0
-rw-rw----. 1 mysql mysql  5242880 7月  13 18:21 ib_logfile1
drwx------. 2 mysql mysql     4096 7月  13 18:21 mysql // 数据库mysql和test是安装mysql之后就有的默认数据库
srwxrwxrwx. 1 mysql mysql        0 7月  13 19:16 mysql.sock
drwx------. 2 mysql mysql     4096 7月  13 18:21 test  // 数据库mysql和test是安装mysql之后就有的默认数据库

    3> /var/log 是mysql数据库存日志输出存放位置

[root@hezhixiong log]# pwd
/var/log
[root@hezhixiong log]# ls
anaconda.ifcfg.log    audit          dracut.log         secure
anaconda.log          boot.log       lastlog            secure-20150713
anaconda.program.log  btmp           maillog            spooler
anaconda.storage.log  cron           maillog-20150713   spooler-20150713
anaconda.syslog       cron-20150713  messages           tallylog
anaconda.xlog         dmesg          messages-20150713  wtmp
anaconda.yum.log      dmesg.old      mysqld.log         yum.log

    4> 查看mysql数据库的端口(默认端口:3306)

[root@hezhixiong log]# netstat -anp | grep mysql
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      1769/mysqld
unix  2      [ ACC ]     STREAM     LISTENING     16929  1769/mysqld         /var/lib/mysql/mysql.sock
原文地址:https://www.cnblogs.com/hezhixiong/p/4642595.html