一、Centos 7环境下安装mysql
检查系统是否已经有mysql用户,如果没有则创建
cat /etc/passwd | grep mysql
cat /etc/group | grep mysql
创建mysql用户(但是不能使用mysql账号登陆系统)
groupadd mysql
useradd mysql -g mysql -s /sbin/nologin
mysql版本:mysql-5.6.25
下载mysql
在这里下载mysql: http://dev.mysql.com/downloads/mysql/
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.23.tar.gz
下载完后需要检查文件的MD5,以确认是否从官网下载的原版本(以防被人篡改过该软件)
我从windows下载后上传到linux后,使用md5sum命令来检查:
md5sum mysql-5.6.25.tar.gz
37664399c91021abe070faa700ecd0ed mysql-5.6.25.tar.gz
可以看出,与上图中的MD5是一致的,如果不一致,就要更换一个镜像地址来下载mysql。
安装mysql
准备安装环境
首先检查是否已经安装过mysql:
rpm -qa | grep mysql
有的话就卸载掉以前安装的mysql:
rpm -e --nodeps xxx(xxx是搜索结果)
并删除所有的相关文件:
rm /etc/my.cnf
编译和安装
安装编译代码所需要的包
yum install -y perl-Module-Install.noarch openssh-clients make gcc-c++ cmake bison-devel ncurses-devel libaio
yum install -y libaio libaio-devel net-tools -y perl-Data-Dumper -y
解压安装包
mkdir -p /home/databases
tar xvf mysql-5.6.25.tar.gz
cd mysql-5.6.25
编译安装
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/home/databases \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/home/databases/mysql.sock \
-DMYSQL_TCP_PORT=51089 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
make && make install
配置mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /home/databases
至此,mysql安装完毕
单实例配置
进入安装路径
cd /usr/local/mysql
进入安装路径,执行初始化配置脚本,创建系统自带的数据库和表
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/home/databases --user=mysql
/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqld_safe &
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/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h ecs-9279.novalocal password 'new-password'
Alternatively you can run:
/usr/local/mysql/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/local/mysql/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
New default config file was created as /usr/local/mysql/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings
WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file
argument to
mysqld_safe
when starting the server
|
注意:在CentOS 7版操作系统的最小安装完成后,在/etc目录下会存在一个my.cnf,需要将此文件更名为其他的名字,如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的MySQL的正确配置,造成无法启动。
在使用"yum update"更新系统后,需要检查下/etc目录下是否会多出一个my.cnf,如果多出,将它重命名成别的。否则,MySQL将使用这个配置文件启动,可能造成无法正常启动等问题。
添加服务,拷贝服务脚本到init.d目录,并设置开机启动
#拷贝启动文件到/etc/init.d/下并重命令为mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
mkdir -p /var/log/mariadb/
touch /var/log/mariadb/mariadb.log
设置/etc/my.cnf文件内容
[mysqld]
basedir =/usr/local/mysql/
datadir=/home/databases/
socket=/home/databases/mysql.sock
pid-file=/home/databases/mysql.pid
lower_case_table_names=1
port = 51088
log_error=/home/databases/mysql-error.log
slow_query_log_file=/home/databases/mysql-slow.log
slow_query_log=1
default-storage-engine=INNODB
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
skip-external-locking
key_buffer_size = 16M
m3ax_allowed_packet = 10M
rt = 51088
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
chkconfig mysql on
--启动MySQL
service mysql start
查看mysql是否启动成功
netstat -lntp | grep 3306
/usr/local/mysql/bin/mysqld_safe --user=mysql --skip-grant-tables --skip-networking&
如果不成功,检查/data/3306/log目录下的mysql-error.log日志,逐一排除错误
如果登陆成功,下面就修改登录密码(不建议在shell环境下修改密码,否则别人只要查看命令历史就能看到密码(前提是你没有情况命令历史))
mysql> update mysql.user set password=password("123456") where user='root';
mysql> flush privileges;
#增加执行权限
chmod 755 /etc/init.d/mysql
#检查自启动项列表中没有mysql这个,如果没有就添加mysql
chkconfig --list mysql
chkconfig --add mysql
#设置MySQL在345等级自动启动
chkconfig --level 345 mysql on
#或用这个命令设置开机启动
#启动MySQL服务
service mysql start
#或者
/etc/init.d/mysql start
--------------------------
#mysql服务的启动/重启/停止
#启动mysql服务
service mysqld start
#重启mysql服务
service mysqld restart
#停止mysql服务
service mysqld stop
查看mysql是否启动成功
netstat -lntp | grep 3306
修改文件拥有者和权限
find /home/databases -name mysql -exec chmod 700 {} \;
添加mysql启动路径
echo 'export PATH=$PATH:/usr/local/mysql/bin' >>/etc/profile
source /etc/profile
echo $PATH
如果mysql没有启动成功,到目录/usr/local/mysql/data下查看错误日志
tail localhost.localdomain.err (localhost.localdomain是主机名)
如果没有生成日志目录,则mysql安装没有成功(再重新编译安装一次)
重启mysql
首先杀死mysql进程
pkill mysqld
然后检查是否已经杀死mysql进程
netstat -lntp | grep 3306
此时shell没有任何输出,表明已经杀死了mysql进程
然后重启mysql并再次检查是否启动成功
service mysql start
netstat -lntp | grep 3306
添加防火墙
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
1.假设已经有mysql-5.5.10.tar.gz以及cmake-2.8.4.tar.gz两个源文件
(1)先安装cmake(mysql5.5以后是通过cmake来编译的)
可以直接使用
yum install cmake
或者使用下面的方法进行源码安装
mysql源码下载地址:
http://cdn.mysql.com/Downloads/MySQL-5.5/mysql-5.5.28.tar.gz
[root@ rhel5 local]
#
tar -zxv -f cmake-2.8.4.tar.gz
[root@ rhel5 local]
#
cd cmake-2.8.4
[root@ rhel5 cmake-2.8.4]
#
./configure
[root@ rhel5 cmake-2.8.4]
#
make
[root@ rhel5 cmake-2.8.4]
#
make install
(2)创建mysql的安装目录及数据库存放目录
//安装mysql
的位置
[root@ rhel5~]
#
mkdir -p /usr/local/mysql
//mysql数据库存放数据库
[root@ rhel5~]
#
mkdir -p /usr/local/mysql/data
(3)创建mysql用户及用户组
[root@ rhel5~]groupadd mysql
[root@ rhel5~]useradd -r -g mysql mysql
(4)安装mysql
[root@ rhel5 local]
#
tar -zxv -f mysql-5.5.10.tar.gz
[root@ rhel5 local]
#
cd mysql-5.5.10
[root@ rhel5 mysql-5.5.10]
#
cmake .
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1
[root@ rhel5 mysql-5.5.10]
#
make
[root@ rhel5 mysql-5.5.10]
#
make install
参数说明:
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql //安装目录
-DINSTALL_DATADIR=/usr/local/mysql/data //数据库存放目录
-DDEFAULT_CHARSET=utf8 //使用utf8字符
-DDEFAULT_COLLATION=utf8_general_ci //校验字符
-DEXTRA_CHARSETS=all //安装所有扩展字符集
-DENABLED_LOCAL_INFILE=1 //允许从本地导入数据
注意事项:
重新编译时,需要清除旧的对象文件和缓存信息。
# make clean
# rm -f CMakeCache.txt
# rm -rf /etc/my.cnf
2.配置
(1)设置目录权限
[root@ rhel5~]
#
cd /usr/local/mysql
[root@ rhel5 mysql]
#
chown -R root:mysql . //把当前目录中所有文件的所有者所有者设为root,所属组为mysql
[root@ rhel5 mysql]
#
chown -R mysql:mysql data
(2)
[root@ rhel5 mysql]
#
cp support-files/my-medium.cnf /etc/my.cnf //将mysql的启动服务添加到系统服务中
(3)创建系统数据库的表
[root@ rhel5 mysql]
#
cd /usr/local/mysql
[root@ rhel5 mysql]
#
scripts/mysql_install_db --user=mysql
(4)设置环境变量
[root@ rhel5~]
#
vi /root/.bash_profile
在PATH=
$PATH:
$HOME/bin添加参数为:
PATH=
$PATH:
$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib
[root@ rhel5~]
#
source /root/.bash_profile
(5)手动启动mysql
[root@ rhel5~]
#
cd /usr/local/mysql
[root@ rhel5 mysql]
#
./bin/mysqld_safe --user=mysql & //启动MySQL,但不能停止
启动日志写在此文件下:/usr/local/mysql/data/localhost.err
关闭MySQL服务
[root@ rhel5 mysql]
#
mysqladmin -u root -p shutdown //这里MySQL的root用户还没有配置密码,所以为空值。需要输入密码时,直接点回车键即可。
(6)另一种简单的启动mysql的方法(mysql已经被添加到系统服务中)
[root@ rhel5~]
#
service mysql.server start
[root@ rhel5~]
#
service mysql.server stop
[root@ rhel5~]
#
service mysql.server restart
如果上述命令出现:mysql.server 未识别的服务
则可能mysql还没添加到系统服务中,下面用另一种方法添加:
[root@ rhel5 mysql]# cp support-files/mysql.server /etc/init.d/mysql
//
将mysql的启动服务添加到系统服务中
注 意:主要是将mysql.server拷贝到/etc/init.d中,命名为mysql。在有的系统中,mysql.server在/usr /local/mysql/share/mysql/mysql.server中,而本系统中,mysql.server在/usr/local /mysql/support-files/mysql.server中。
然后再用#service mysql start 来启动mysql即可。
(7)修改MySQL的root用户的密码以及打开远程连接
[root@ rhel5~]
#
mysql -u root mysql
mysql>use mysql;
mysql>desc user;
mysql> GRANT ALL PRIVILEGES ON *.* TO root@
"
%
" IDENTIFIED BY
"
root
"; //为root添加远程连接的能力。
mysql>update user set Password = password('xxxxxx') where User='root';
mysql>select Host,User,Password from user where User='root';
mysql>flush privileges;
mysql>exit
重新登录:mysql -u root -p
若还不能进行远程连接,则关闭防火墙
[root@ rhel5~]
#
/etc/rc.d/init.d/iptables stop
注:如果不能远程连接,出现错误mysql error number 1130,则加入下面语句试试:
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '******' WITH GRANT OPTION;