The MySQL installed through yum under CentOS 6.4 is version 5.1, which is relatively old, so I just want to install the higher version 5.6.30 through the source code.
Use the following command to check whether MySQL Server is installed
rpm -qa | grep mysql
If yes, use the following command Command to uninstall
rpm -e mysql //普通删除模式rpm -e --nodeps mysql // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除
yum -y install make gcc-c++ cmake bison-devel ncurses-devel
wget http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.30.tar.gztar xvf mysql-5.6.14.tar.gzcd 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_cimake && make install
For compilation parameters, please refer to http://dev.mysql.com/doc/refman/5.5/en/source-configuration-options.html.
The whole process takes about 30 minutes...a long wait
Use the following command to check if there is a mysql user and user group
cat /etc/passwd 查看用户列表cat /etc/group 查看用户组列表
Create if there is none
groupadd mysqluseradd -g mysql mysql
Modify /usr/local/mysql permissions
chown -R mysql:mysql /usr/local/mysql
Modify /usr/local/mysql permissions
Enter the installation path
cd /usr/local/mysql
Enter the installation path, execute the initialization configuration script, and create the database and tables that come with the system
scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
Note: When starting the MySQL service, my.cnf will be searched in a certain order, first in the /etc directory, if not found, it will search for "$basedir/my.cnf", in this case /usr/ local/mysql/my.cnf, this is the default location of the configuration file of the new version of MySQL!
注意:在CentOS 6.4版操作系统的最小安装完成后,在/etc目录下会存在一个my.cnf,需要将此文件更名为其他的名字,如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的MySQL的正确配置,造成无法启动。
在使用"yum update"更新系统后,需要检查下/etc目录下是否会多出一个my.cnf,如果多出,将它重命名成别的。否则,MySQL将使用这个配置文件启动,可能造成无法正常启动等问题。
添加服务,拷贝服务脚本到init.d目录,并设置开机启动
cp support-files/mysql.server /etc/init.d/mysqlchkconfig mysql onservice mysql start --启动MySQL
MySQL启动成功后,root默认没有密码,我们需要设置root密码。
设置之前,我们需要先设置PATH,要不不能直接调用mysql
修改/etc/profile文件,在文件末尾添加
PATH=/usr/local/mysql/bin:$PATHexport PATH
关闭文件,运行下面的命令,让配置立即生效
source /etc/profile
现在,我们可以在终端内直接输入mysql进入,mysql的环境了
执行下面的命令修改root密码
mysql -uroot mysql> SET PASSWORD = PASSWORD('123456');
若要设置root用户可以远程访问,执行
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.16.%' IDENTIFIED BY 'password' WITH GRANT OPTION;
红色的password为远程访问时,root用户的密码,可以和本地不同。
防火墙的3306端口默认没有开启,若要远程访问,需要开启这个端口
打开/etc/sysconfig/iptables
在“-A INPUT –m state --state NEW –m tcp –p –dport 22 –j ACCEPT”,下添加:
-A INPUT -m state --state NEW -m tcp -p -dport 3306 -j ACCEPT
然后保存,并关闭该文件,在终端内运行下面的命令,刷新防火墙配置:
service iptables restart
OK,一切配置完毕,你可以访问你的MySQL了~
------------------------------------------------------------------------------------------------------------------
Added on December 2, 2014:
CentOS 7 uses Firewalld as the firewall by default, so after modifying iptables, it will not work at all after restarting the system.
The method of adding a port in Firewalld is as follows:
firewall-cmd --zone=public --add-port=3306/tcp--permanent
firewall-cmd --reload
---------------------------------- -------------------------------------------------- ----------------------------
1: Add user
INSERT INTO mysql.user(Host,User,Password) values("%","dev",password("yihen382465"));
If you want to log in on any computer, replace localhost with %; you can also replace localhost with any IP, which means only allowed Log in with this IP;
If ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value, set the sql_mode in the configuration file to NO_ENGINE_SUBSTITUTION, and then restart mysql
2: Create a database
CREATE DATABASE distributorDEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
3: Grant the database
grant all privileges on distributor.* to dev@localhost identified by '123456';
4: Refresh privileges
flush privileges;