本文共 4775 字,大约阅读时间需要 15 分钟。
之前,我们在CentOS中安装了JDK、Tomcat、Eclipse;接下来,我们在CentOS中安装MySQL数据库。
在CentOS中安装MySQL之前需要检查当前系统是已经安装MySQL。若有,则需要先删除旧版本再安装新版本。
检查是否已安装MySQL,语法如下:
rpm -qa | grep mysql
示例如下:
[root@MyCentOS /]# rpm -qa | grep mysqlmysql-libs-5.1.73-7.el6.x86_64[root@MyCentOS /]#在此,我们发现设备已经安装MySQL;故需要将其卸载。
普通卸载MySQL,语法如下:
rpm -e mysql-libs
强制卸载MySQL,语法如下:
rpm -e --nodeps mysql-libs
示例如下:
至此,我们已经卸载了系统中原有的MySQL数据库。在此,详细介绍MySQL的安装过程
在安装MySQL之前,我们先来安装编译代码需要的包即GCC环境。
命令如下:
yum -y install make gcc-c++ cmake bison-devel ncurses-devel此过程,约耗时2分钟;请耐心等待。
使用XFTP将MySQL压缩包上传至/opt
进入/opt目录将上传的MySQL压缩包解压。
[root@MyCentOS /]# cd /opt/[root@MyCentOS opt]# ls -l总用量 549888drwxr-xr-x. 9 root root 4096 6月 11 11:12 apache-tomcat-7.0.70-rw-r--r--. 1 root root 8924465 6月 11 11:08 apache-tomcat-7.0.70.tar.gzdrwxrwxr-x. 9 root users 4096 6月 11 13:08 eclipse-rw-r--r--. 1 root root 287110893 6月 11 12:48 eclipse-jee-mars-2-linux-gtk-x86_64.tar.gz-r--r--r--. 1 root root 77493120 6月 10 10:16 firefox-45.0.1-1.el6.centos.x86_64.rpmdrwxr-xr-x. 2 root root 4096 6月 3 22:56 homedrwxr-xr-x. 8 uucp 143 4096 4月 11 2015 jdk1.7.0_79-rw-r--r--. 1 root root 153512879 6月 11 10:13 jdk-7u79-linux-x64.gz-rw-r--r--. 1 root root 36005278 6月 11 16:01 mysql-5.6.14.tar.gzdrwxr-xr-x. 2 root root 4096 3月 26 2015 rhdrwxr-xr-x. 3 root root 4096 6月 3 22:39 temp[root@MyCentOS opt]# tar -zxvf mysql-5.6.14.tar.gz
进入解压后的MySQL,准备编译安装
[root@MyCentOS opt]# cd mysql-5.6.14[root@MyCentOS mysql-5.6.14]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci此过程,约耗时3分钟;请耐心等待。
指令如下:
make && make install此过程,约耗时50分钟;请耐心等待。
在此,详细介绍MySQL的配置过程。
在Linux中由专门的用户和组对MySQL进行管理;所以,我们需创建管理MySQL的用户及其所在组。
步骤如下:
代码如下:
[root@MyCentOS mysql-5.6.14]# groupadd mysql[root@MyCentOS mysql-5.6.14]# useradd -g mysql mysql[root@MyCentOS mysql-5.6.14]# chown -R mysql:mysql /usr/local/mysql[root@MyCentOS mysql-5.6.14]#
请进入安装路径再执行初始化配置脚本从而创建系统自带的数据库和表
代码如下:
[root@MyCentOS mysql-5.6.14]# cd /usr/local/mysql[root@MyCentOS mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
请在 /usr/local/mysql下拷贝服务脚本到init.d目录,并设置开机启动
代码如下:
[root@MyCentOS mysql]# cd /usr/local/mysql[root@MyCentOS mysql]# cp support-files/mysql.server /etc/init.d/mysql[root@MyCentOS mysql]# chkconfig mysql on[root@MyCentOS mysql]# service mysql start
默认情况下,MySQL有一个root用户;但是该用户密码为空。所以,我们需要在/usr/local/mysql/bin目录下利用root账户先登录再修改其密码;例如设置其密码亦为root
代码如下:
[root@MyCentOS mysql]# cd /usr/local/mysql/bin[root@MyCentOS bin]# ./mysql -urootWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.6.14 Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> SET PASSWORD = PASSWORD('root');Query OK, 0 rows affected (0.00 sec)mysql> quitBye[root@MyCentOS bin]#再设置密码之后利用quit命令退出MySQL.
在/usr/local/mysql/bin目录下利用./mysql -u root -p重新登录MySQL
代码如下:
[root@MyCentOS bin]# cd /usr/local/mysql/bin[root@MyCentOS bin]# ./mysql -u root -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.6.14 Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
语句如下:
-- 创建数据库DROP DATABASE IF EXISTS mydb;CREATE DATABASE mydb;USE mydb;-- 创建student表CREATE TABLE student ( sid CHAR(6), sname VARCHAR(50), age INT, gender VARCHAR(50) DEFAULT 'male');-- 向student表插入数据INSERT INTO student (sid,sname,age,gender) VALUES ('S_1001', 'lili', 14, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1002', 'wang', 15, 'female');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1003', 'tywd', 16, 'male');-- 从student表查询数据SELECT * FROM student;