一.源码安装,优点是安装包比较小,只有十多M,缺点是安装依赖的库多,安装编译时间长,安装步骤复杂容易出错;
二.使用官方编译好的二进制文件安装,优点是安装速度快,安装步骤简单,缺点是安装包很大,300M左右。
三.yum安装
四.rpm安装
安装方法一:
请看我的另一篇文章:mysql源码脚本安装
安装方法二:(Redhat5.5 32位下安装mysql 5.6.35)
进入安装包所在目录
[root@localhost ~]# cd /usr/local
[root@localhost local]# tar -zxvf mysql-5.6.35-linux-glibc2.5-i686.tar.gz
[root@localhost local]# rm -rf mysql-5.6.35-linux-glibc2.5-i686.tar.gz
[root@localhost local]# mv mysql-5.6.35-linux-glibc2.5-i686/ mysql
添加系统mysql组和mysql用户:
[root@localhost local]# groupadd mysql
[root@localhost local]# useradd -r -g mysql mysql
进入安装mysql软件目录:
[root@localhost local]# cd mysql/
修改当前目录拥有者为mysql用户:
[root@localhost mysql]# chown -R mysql:mysql ./
安装数据库:
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql
在RedHat6.6和6.5 64位安装时遇到了这些问题:
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql
sh: ./bin/my_print_defaults: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory网上有的说是版本的原因,mysql是32位的,而系统是64位。可是我后来在64位上也安装成功了啊
FATAL ERROR: Neither host 'h149' nor 'localhost' could be looked up with
./bin/resolveip
Please configure the 'hostname' command to return a correct
hostname.
If you want to solve this at a later stage, restart this script
with the --force option
补充:Linux 下查看系统是32位 还是64 位的方法
[root@localhost ~]# uname -a (RedHat6.6 64位)
Linux localhost.localdomain 2.6.32-504.el6.x86_64 #1 SMP Tue Sep 16 01:56:35 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# uname -a (RedHat5.5 32位)
Linux localhost.localdomain 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:43 EDT 2010 i686 i686 i386 GNU/Linux
解决:yum -y install ld-linux.so.2 libaio.so.1 libstdc++.so.6 libncurses.so.5
Centos7.2 64位:
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql
-bash: ./scripts/mysql_install_db: /usr/bin/perl: bad interpreter: No such file or directory
解决:[root@localhost mysql]# yum -y install perl autoconf ld-linux.so.2 libstdc++* libstdc++.so.6 libaio.so.1 libncurses.so.5
修改当前目录拥有者为root用户:
[root@localhost mysql]# chown -R root:root ./
修改当前data目录拥有者为mysql用户:
[root@localhost mysql]# chown -R mysql:mysql data
(这些赋权限的也不知道有啥用,我后来试了一下,即使都没给他们赋权最后mysql也安装成功了,但为了怕以后有什么问题,还是都按步骤来吧)
添加开机启动:
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysql
启动mysql服务:
[root@localhost mysql]# service mysql start
Centos7.2 64位:
[root@localhost mysql]# service mysql start
Starting MySQL.170810 19:43:05 mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).
解决:修改/etc/my.cnf中的这两行为
[root@localhost mysql]# vi /etc/my.cnf
log-error=/var/log/mysql/mysql.log
pid-file=/var/run/mysql/mysql.pid
执行命令:ps -ef|grep mysql 看到mysql服务说明启动成功
修改mysql的root用户密码,root初始密码为空:
[root@localhost mysql]# ./bin/mysqladmin -u root password '123456'
Warning: Using a password on the command line interface can be insecure.
(翻译过来是:在命令行界面上使用密码是不安全的)
RedHat6.6和6.5 64位到这步的时候报这个错:
./bin/mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
解决:查看/etc/my.cnf中有socket=/var/lib/mysql/mysql.sock
[root@localhost mysql]# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
[root@localhost mysql]# ./bin/mysqladmin -u root password '123456'//这个步骤可忽略,如果执行了这一步,等登录时mysql -u root -p就得输入该密码,如果没执行制一步登录的时候回车即可
把mysql客户端放到默认路径:
[root@localhost mysql]# ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
注意:建议使用软链过去,不要直接包文件复制,便于系统安装多个版本的mysql
[root@localhost mysql]# mysql -u root -p(如果没有执行./bin/mysqladmin -u root password '123456'则默认密码为空,直接回车即可)
Enter password: 123456
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35 |
+-----------+
1 row in set (0.00 sec)
如果你想修改root密码的话可执行下面的语句:
mysql> set password for root@localhost = password('bigdata');
Query OK, 0 rows affected (0.06 sec)
Centos7.2安装mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz:
[root@localhost ~]# cd /usr/local
[root@localhost local]# tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
[root@localhost local]# rm -rf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
[root@localhost local]# mv mysql-5.7.17-linux-glibc2.5-x86_64/ mysql或者ln -s mysql-5.7.17-linux-glibc2.5-x86_64 mysql
1.创建用户和组
groupadd mysql
useradd -g mysql -s /sbin/nologin mysql
2.配置PATH
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
3.数据库目录规划
文件类型 | 实例3306 | 软链 |
数据datadir | /usr/local/mysql/data | /data/mysql/data |
参数文件my.cnf | /usr/local/mysql/etc/my.cnf | |
错误日志log-error | /usr/local/mysql/log/mysql_error.log | |
二进制日志log-bin | /usr/local/mysql/binlogs/mysql-bin | /data/mysql/binlogs/mysql-bin |
慢查询日志slow_query_log_file | /usr/local/mysql/log/mysql_slow_query.log | |
套接字socket文件 | /usr/local/mysql/run/mysql.sock | |
pid文件 | /usr/local/mysql/run/mysql.pid |
mkdir -p /data/mysql/{data,binlogs,log,etc,run}
ln -s /data/mysql/data /usr/local/mysql/data
ln -s /data/mysql/binlogs /usr/local/mysql/binlogs
ln -s /data/mysql/log /usr/local/mysql/log
ln -s /data/mysql/etc /usr/local/mysql/etc
ln -s /data/mysql/run /usr/local/mysql/run
chown -R mysql.mysql /data/mysql/
chown -R mysql.mysql /usr/local/mysql/{data,binlogs,log,etc,run}
也可以只对数据目录和二进制日志目录软链
mkdir -p /usr/local/mysql/{log,etc,run}
mkdir -p /data/mysql/{data,binlogs}
ln -s /data/mysql/data /usr/local/mysql/data
ln -s /data/mysql/binlogs /usr/local/mysql/binlogs
chown -R mysql.mysql /usr/local/mysql/{data,binlogs,log,etc,run}
chown -R mysql.mysql /data/mysql
4.配置my.cnf参数文件
删除系统自带的my.cnf
rm -f /etc/my.cnf
在/usr/local/mysql/etc/下创建my.cnf文件,加入如下参数,其他参数根据需要配置
[client]
port = 3306
socket = /usr/local/mysql/run/mysql.sock
[mysqld]
port = 3306
socket = /usr/local/mysql/run/mysql.sock
pid_file = /usr/local/mysql/run/mysql.pid
datadir = /usr/local/mysql/data
default_storage_engine = InnoDB
max_allowed_packet = 512M
max_connections = 2048
open_files_limit = 65535
skip-name-resolve
lower_case_table_names=1
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 2048M
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 0
key_buffer_size = 64M
log-error = /usr/local/mysql/log/mysql_error.log
log-bin = /usr/local/mysql/binlogs/mysql-bin
slow_query_log = 1
slow_query_log_file = /usr/local/mysql/log/mysql_slow_query.log
long_query_time = 5
tmp_table_size = 32M
max_heap_table_size = 32M
query_cache_type = 0
query_cache_size = 0
server-id=1
5.初始化数据库
cd mysql
执行:./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize(会卡在这里一段时间,请耐心等待)打开另一个终端在日志文件里会提示一个临时密码,记录这个密码(等待的时间会有点长)
grep 'temporary password' /usr/local/mysql/log/mysql_error.log
2017-03-12T13:26:30.619610Z 1 [Note] A temporary password is generated for root@localhost: b#uhQy*=d7yH
6.生成ssl,给数据库加密
mysql_ssl_rsa_setup --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/
Generating a 2048 bit RSA private key
.....................................................................................................+++
...................................................+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
..........................................................................................+++
.....................................................................................................................................................................................................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
...............................+++
......+++
writing new private key to 'client-key.pem'
-----
7.设置启动项
CentOS 6
cd /usr/local/mysql
cp support-files/mysql.server /etc/init.d/mysql.server
chkconfig --add mysql.server
chkconfig mysql.server on
chkconfig --list
CentOS 7
cd /usr/lib/systemd/system
touch mysqld.service
编辑内容如下vi mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/usr/local/mysql/run/mysqld.pid
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Needed to create system tables
#ExecStartPre=/usr/bin/mysqld_pre_systemd
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/run/mysqld.pid $MYSQLD_OPTS
# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 65535
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
加载
systemctl daemon-reload
systemctl enable mysqld.service
systemctl is-enabled mysqld
8. 启动mysql
systemctl start mysqld.service
mysql -u root -p
Enter password:(输入前面生成的临时密码)
mysql> select version();
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> set password=password('A123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> select version();
+------------+
| version() |
+------------+
| 5.7.17-log |
+------------+
1 row in set (0.00 sec)
9设置远程访问
1,在远程访问之前需先打开centos7的端口:
1):开启端口:
[root@localhost ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
2):重启防火墙:
[root@localhost ~]# firewall-cmd --reload
success
(不知道前面防火墙的两步有什么用,一开始我没执行这两步直接进行下面操作好像也没有什么影响啊。但命令比较新,写出来以后可参考)
授权(grant all privileges on *.* to 远程访问用户名@'%' identified by '用户密码'; )
mysql> grant all privileges on *.* to root@'%' identified by 'A123456';参考自:http://www.cnblogs.com/xiaoqiangzhaitai/p/6561708.html
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select host,user from user;(多出1条远程登录用户记录)
+-----------+-----------+
| host | user |
+-----------+-----------+
| % | root |
| localhost | mysql.sys |
| localhost | root |
+-----------+-----------+
3 rows in set (0.00 sec)
去官网下载包的速度特别的慢,这里给你们一个下载快的地址:http://mirrors.sohu.com/mysql/
安装方法三:
Redhat5.5和6.5用yum -y install mysql*就行
而Centos7.2还得参考这篇文章http://blog.csdn.net/u010246789/article/details/52382632(就是下载的时候特别慢)
安装方法四:
请参考http://www.jb51.net/article/103562.htm和http://blog.csdn.net/kuluzs/article/details/51923995