CentOS7 环境 MySQL5.7 安装

By liuzhijun, 2016-08-11, 分类: 数据库

mysql

MySQL 在 Linux 环境中的安装方式有多种,比较常见的安装方式是使用 MySQL 二进制文件安装,无须编译,直接解压即可。

下载地址

官方下载地址:http://dev.mysql.com/downloads/mysql/ 选择 Linux Generic 版本,也可以在搜狐镜像地址下载 http://mirrors.sohu.com/mysql。

mysql-download

安装前确保系统中没有其它版本的MySQL,删除 MySQL 相关的配置文件,如/etc/my.cnf,/etc/mysql 等目录。同时,MySQL需要依赖libaio库,如果没有安装此库,MySQL在Data目录初始化和随后的服务器启动过程中会失败。

yum install libaio  # Yum-based systems
apt-get install libaio1 # APT-based systems

安装初始化

MySQL下载后解压安装在/usr/local目下:

cd /usr/local
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
tar -zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.18-linux-glibc2.5-x86_64 mysql

解压后目录文件结构为:

    .
├── bin
├── COPYING
├── docs
├── include
├── lib
├── man
├── README
├── share
└── support-files

创建mysql用户/用户组:

groupadd mysql  # 添加组
useradd -r -g mysql -s /bin/false mysql  # 添加系统用户mysql, 同时归属mysql用户组,禁用登录
cd /usr/local/mysql
mkdir mysql-files
chmod 750 mysql-files
chown -R mysql .
chgrp -R mysql .
bin/mysqld --initialize --user=mysql  # 初始化mysql
# 初始化完成后,生成临时密码

    2017-05-16T09:29:54.914039Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2017-05-16T09:29:56.230632Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2017-05-16T09:29:56.458902Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2017-05-16T09:29:56.525449Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 396f6ef0-3a1a-11e7-ac12-00163e08ad97.
    2017-05-16T09:29:56.527918Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
    2017-05-16T09:29:56.528403Z 1 [Note] A temporary password is generated for root@localhost: tXH34JDf;EoE

mkdir data  # 创建数据目录
chown -R root .
chown -R mysql data mysql-files
bin/mysqld_safe --user=mysql & (还可以指定mysql的配置文件 --defaults-file=file_name,此选项要放在第一位)
cp support-files/mysql.server /etc/init.d/mysql.server

/etc/init.d/mysql.server start  # 启动mysql

安全设置

启动之后,为MySQL处理一些安全相关的设置。

[root@iZwz9h34kd1ead45xeg4o2Z bin]# ./mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 
Error: Access denied for user 'root'@'localhost' (using password: YES)
[root@iZwz9h34kd1ead45xeg4o2Z bin]# ./mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : No

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

创建数据库、用户名、远程连接MySQL

## CREATE DATABASE ##
mysql> CREATE DATABASE webdb;

## CREATE USER ##
mysql> CREATE USER 'webdb_user'@'10.0.15.25' IDENTIFIED BY 'password123'; # 如果是允许所有ip远程连过来使用 ‘%’代替ip

## GRANT PERMISSIONS ##
mysql> GRANT ALL ON webdb.* TO 'webdb_user'@'10.0.15.25';

##  FLUSH PRIVILEGES, Tell the server to reload the grant tables  ##
mysql> FLUSH PRIVILEGES;

如果希望root用户远程登录操作,那么可以修改:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> FLUSH PRIVILEGES;

默认情况下CentOS开启了防火墙,远程客户端没法链接过来的,因此需要添加新的防火墙规则。

systemctl stop firewalld
systemctl mask firewalld
yum install iptables-services
systemctl enable iptables

vim /etc/sysconfig/iptables
# 添加一行
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

重启iptables

service iptables restart
## OR ##
/etc/init.d/iptables restart

当然你也可以直接把防火墙关闭,但是不建议生成环境这样做,非常不安全。

# 禁止firewall开机启动
systemctl disable firewalld
# 停止防火墙
systemctl stop firewalld
# 防火墙状态
systemctl status firewalld
# 设置防火墙开机启动
systemctl enable firewalld
# 启动防火墙
systemctl start firewalld

启动成功后,为MySQL的用户指定密码

mysql> SET PASSWORD FOR 'root'@'%' = PASSWORD('new_password');

参考:

  1. https://dev.mysql.com/doc/refman/5.6/en/default-privileges.html
  2. https://dev.mysql.com/doc/refman/5.6/en/binary-installation.html

Windows安装MySQL

MySQL官网给出的安装包有两种格式,一个是msi格式,一个是zip格式的。很多人下了zip格式的解压发现没有setup.exe,面对一堆文件一头雾水,不知如何安装。下面笔者将介绍MySQL在Windows系统中的安装方法与启动方式

下载地址

# ZIP压缩版
windows x32
http://mirror.bit.edu.cn/mysql/Downloads/MySQL-5.7/mysql-5.7.12-winx32.zip

windows x64
http://mirror.bit.edu.cn/mysql/Downloads/MySQL-5.7/mysql-5.7.12-winx64.zip

# 安装版
http://mirror.bit.edu.cn/mysql/Downloads/MySQLInstaller/mysql-installer-community-5.7.12.0.msi

安装MySQL

  1. 下载之后解压到一个目录下,例如 D:\MySQL
  2. 打开文件夹,复制一份 my-default.ini,重命名为 my.ini 放相同目录下。
  3. 修改 my.ini 文件
    [mysqld]
    basedir = D:/Programs/mysql
    datadir = D:/Programs/mysql/data
    port = 3306
    # server_id = .....
    
    character-set-server = utf8
    default-storage-engine = INNODB
    
    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
    # join_buffer_size = 128M
    # sort_buffer_size = 2M
    # read_rnd_buffer_size = 2M
    
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
    

    指定MySQL根目录和数据目录:

    basedir = D:/Programs/mysql
    datadir = D:/Programs/mysql/data
    
    1. 把MySQL的安装路径加入到环境变量

      path=...;D:\Programs\mysql\bin 5. 以管理员身份运行CMD,初始化数据库系统

      mysqld --initialize 在data目录会生成系统用户表还有root用户的初始密码,密码在一个以电脑主机名命名的err文件中: 2016-08-11T01:51:53.352953Z 1 [Note] A temporary password is generated for root@localhost: sh!Od0p3Hrgq

启动MySQL

  1. 安装MySQL服务 mysqld --install
  2. 卸载MySQL服务 mysqld --remove
  3. 启动MySQL服务 net start mysql
  4. 停止MySQL服务 net stop mysql
  5. 重启MySQL服务 net restart mysql
  6. 登录 mysql -uroot -p
  7. 修改密码:SET PASSWORD FOR 'abe'@'host_name' = PASSWORD('eagle');

参考链接:
https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html http://312461613.blog.51cto.com/965442/1713529


关注公众号「Python之禅」(id:vttalk)获取最新文章 python之禅

猜你喜欢

2017-04-27
基于Mycat分布式MySQL数据库部署实践指南
2016-03-03
MySQL启动报错问题排查:InnoDB: Unable to lock ./ibdata1 error
2014-02-17
MySQL分表分区
2014-02-16
系统学习MySQL
2014-04-03
MySQL慢查询配置
2016-05-13
centOS重新安装MySQL5.6