阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL入门教程

180次阅读
没有评论

共计 18717 个字符,预计需要花费 47 分钟才能阅读完成。

操作系统:CentOS 6.7
MySQL 版本:5.6.30

  • 1. 前期准备
  • 2. 系统配置
  • 3.CMake 编译配置
  • 4.make && make install
  • 5. 后期配置和测试
  • Reference

1. 前期准备

首先需要 CMake,可以 yum 直接安装:

yum install cmake

也可以官网 https://cmake.org/ 下载源码编译。
我这里选择了官网下载最新版本 cmake-3.5.2.tar.gz。

# tar -zxvf cmake-3.5.2.tar.gz && cd cmake-3.5.2

# ./configure
部分输出略。-- Build files have been written to: /soft/cmake-3.5.2
---------------------------------------------
CMake has bootstrapped.  Now run gmake.

# gmake 

# make install

2. 系统配置

添加组和用户:

groupadd mysql
useradd -g mysql mysql

vi /etc/security/limits.conf 文件末尾添加:

mysql   soft    nproc   2047
mysql   hard    nproc   16384
mysql   soft    nofile  1024
mysql   hard    nofile  65536

3.CMake 编译配置

解压源码包:

tar zxvf mysql-5.6.30.tar.gz && cd mysql-5.6.30

CMake 编译配置

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DCOMPILATION_COMMENT='JSS for mysqltest' \
-DWITH_READLINE=ON \
-DSYSCONFDIR=/data/mysqldata/3306 \
-DMYSQL_UNIX_ADDR=/data/mysqldata/3306/mysql.sock

遇到以下错误,

-- Could NOT find Curses (missing:  CURSES_LIBRARY CURSES_INCLUDE_PATH) 
CMake Error at cmake/readline.cmake:85 (MESSAGE):
  Curses library not found.  Please install appropriate package,

      remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on RedHat and derivates it is ncurses-devel.
Call Stack (most recent call first):
  cmake/readline.cmake:128 (FIND_CURSES)
  cmake/readline.cmake:202 (MYSQL_USE_BUNDLED_EDITLINE)
  CMakeLists.txt:421 (MYSQL_CHECK_EDITLINE)


-- Configuring incomplete, errors occurred!
See also "/soft/mysql-5.6.30/CMakeFiles/CMakeOutput.log".
See also "/soft/mysql-5.6.30/CMakeFiles/CMakeError.log".
[root@JY-DB mysql-5.6.30]# 

yum 安装提示缺失的包:

yum install ncurses-devel

重新删除配置文件:

rm -rf CMakeCache.txt

然后重新 CMake 工具编译:

CMake Warning:
  Manually-specified variables were not used by the project:

    WITH_READLINE


-- Build files have been written to: /soft/mysql-5.6.30
[root@JY-DB mysql-5.6.30]# 

4.make && make install

[root@JY-DB mysql-5.6.30]# make && make install
大量输出略。

这个时间会比较长,也跟机器性能有关。

5. 后期配置和测试

5.1 打包 MySQL 二进制版本:

[root@JY-DB data]# tar zcvf mysql-5.6.30.tar.gz /usr/local/mysql/

5.2 修改 MySQL 软件所在目录拥有者:

chown -R mysql.mysql /usr/local/mysql

5.3 修改 mysql 用户环境变量:

vi ~/.bash_profile

export LANG=zh_CN.GB18030
export PATH=/usr/local/mysql/bin:$PATH

5.4 创建数据库服务:

# mkdir -p /data/mysqldata/{3306/{data,tmp,binlog},backup,scripts}
# chown -R mysql.mysql /data/mysqldata
# su - mysql
$ more /usr/local/mysql/support-files/my-default.cnf 
$ vi /data/mysqldata/3306/my.cnf

my.cnf 配置文件内容如下:

[client]
port = 3306
socket = /data/mysqldata/3306/mysql.sock

#The MySQL Server
[mysqld]
port = 3306
user = mysql
socket = /data/mysqldata/3306/mysql.sock
pid-file = /data/mysqldata/3306/mysql.pid
basedir = /usr/local/mysql
datadir = /data/mysqldata/3306/data
tmpdir = /data/mysqldata/3306/tmp
open_files_limit = 10240
explicit_defaults_for_timestamp
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

#Buffer
max_allowed_packet = 256M
max_heap_table_size = 256M
net_buffer_length = 8k
sort_buffer_size = 2M
join_buffer_size = 4M
read_buffer_size = 2M
read_rnd_buffer_size = 16M

#Log
log-bin = /data/mysqldata/3306/binlog/mysql-bin
binlog_cache_size = 32M
max_binlog_cache_size = 512M
max_binlog_size = 512M
binlog_format = mixed
log_output = FILE
log-error = ../mysql-error.log
slow_query_log = 1
slow_query_log_file = ../slow_query.log
general_log = 0
general_log_file = ../general_query.log
expire-logs-days = 14

#InnoDB
innodb_data_file_path = ibdata1:2048M:autoextend
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_buffer_pool_size = 1024M

[mysql]
no-auto-rehash
prompt = (\u@\h)[\d]>\_
default-character-set = gbk

初始化 MySQL 数据库:

$ /usr/local/mysql/scripts/mysql_install_db --datadir=/data/mysqldata/3306/data --basedir=/usr/local/mysql

5.5 启动数据库服务:

mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf &

测试连接并查看 MySQL 进程和端口监听状态:

netstat -lnt | grep 3306
ps -ef | grep bin/mysql | grep -v grep

实际操作过程如下:

[root@JY-DB ~]# su - mysql
[mysql@JY-DB ~]$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.30-log JSS for mysqltest

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

(root@localhost)[(none)]> exit
Bye
[mysql@JY-DB ~]$ netstat -lnt |grep 3306
tcp        0      0 :::3306                     :::*                        LISTEN      
[mysql@JY-DB ~]$  
[mysql@JY-DB ~]$ ps -ef | grep bin/mysql | grep -v grep
mysql     6736  1753  0 11:24 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf
mysql     7202  6736  0 11:24 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysqldata/3306/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysqldata/3306/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/data/mysqldata/3306/data/../mysql-error.log --open-files-limit=10240 --pid-file=/data/mysqldata/3306/mysql.pid --socket=/data/mysqldata/3306/mysql.sock --port=3306

Reference

  • 李丙洋. 涂抹 MySQL. 水利水电出版社, 2014.  PDF 下载见 http://www.linuxidc.com/Linux/2016-06/132479.htm
  • dev.mysql.com

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2016-06/132477p2.htm

在上篇文章 MySQL 入门 01-MySQL 源码安装 中,我们介绍了 MySQL 源码安装的方法。
源码安装虽然有着更加灵活和更加优化等诸多优势。但源码编译安装部署的过程相对复杂,而且整个过程所花费的时间很长。
对于一整套系统和硬件完全一样的机器,其实完全可以只在一台机器源码安装,然后打包好编译出来的二进制版本,拷贝到其他机器解压,直接进行一些配置即可。

本篇文章就来介绍一下二进制版本安装配置 MySQL 的整个过程。

  • 1. 上传二进制版本
  • 2. 规划数据存放目录
  • 3. 解压二进制版本的 MySQL
  • 4. 添加组和用户
  • 5. 配置 limits.conf
  • 6. 修改目录所有者
  • 7. 配置环境变量
  • 8. 创建数据库服务

1. 上传二进制版本

首先要将上篇文章中 5.1 小节中,打包好的 MySQL 二进制版本 mysql-5.6.30.tar.gz 上传到 /usr2 目录下。
注:二进制版本的压缩包就有 200M+ 以上的大小。

2. 规划数据存放目录

这里系统是使用 lvm 管理的,vg 名称 vg00,添加一个名称为 lvdata 的 lv,大小设定 100G,然后在系统创建 /data 目录,将新建的 lvdata 挂载到 /data 目录下。具体操作如下:

-- 创建 lvdata
lvcreate -L 100g -n lvdata vg00

-- 格式化为 ext4 文件系统
mkfs.ext4 /dev/mapper/vg00-lvdata

-- 添加为开机自动挂载的目录
vi /etc/fstab 加入一行:/dev/mapper/vg00-lvdata /data                   ext4    defaults        1 2

-- 创建 /data 目录并挂载
mkdir -p /data && mount -a

3. 解压二进制版本的 MySQL

切换到根目录下,解压二进制版本的 MySQL:

cd / && tar zxvf /usr2/mysql-5.6.30.tar.gz

4. 添加组和用户

添加组 mysql 和用户 mysql:

groupadd mysql
useradd -g mysql mysql

5. 配置 limits.conf

vi /etc/security/limits.conf,在文件末尾添加:

mysql   soft    nproc   2047
mysql   hard    nproc   16384
mysql   soft    nofile  1024
mysql   hard    nofile  65536

6. 修改目录所有者

修改 MySQL 软件所在目录拥有者:

chown -R mysql.mysql /usr/local/mysql

7. 配置环境变量

配置 mysql 用户的环境变量:
需要 su - mysql 切换到 mysql 用户下,
vi ~/.bash_profile

export LANG=zh_CN.GB18030
export PATH=/usr/local/mysql/bin:$PATH

8. 创建数据库服务

8.1 创建前准备:

# mkdir -p /data/mysqldata/{3306/{data,tmp,binlog},backup,scripts}
# chown -R mysql.mysql /data/mysqldata

# su - mysql
$ vi /data/mysqldata/3306/my.cnf

8.2 MySQL 的 my.cnf 配置文件内容如下:

[client]
port = 3306
socket = /data/mysqldata/3306/mysql.sock

#The MySQL Server
[mysqld]
port = 3306
user = mysql
socket = /data/mysqldata/3306/mysql.sock
pid-file = /data/mysqldata/3306/mysql.pid
basedir = /usr/local/mysql
datadir = /data/mysqldata/3306/data
tmpdir = /data/mysqldata/3306/tmp
open_files_limit = 10240
explicit_defaults_for_timestamp
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

#Buffer
max_allowed_packet = 256M
max_heap_table_size = 256M
net_buffer_length = 8k
sort_buffer_size = 2M
join_buffer_size = 4M
read_buffer_size = 2M
read_rnd_buffer_size = 16M

#Log
log-bin = /data/mysqldata/3306/binlog/mysql-bin
binlog_cache_size = 32M
max_binlog_cache_size = 512M
max_binlog_size = 512M
binlog_format = mixed
log_output = FILE
log-error = ../mysql-error.log
slow_query_log = 1
slow_query_log_file = ../slow_query.log
general_log = 0
general_log_file = ../general_query.log
expire-logs-days = 14

#InnoDB
innodb_data_file_path = ibdata1:2048M:autoextend
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_buffer_pool_size = 1024M

[mysql]
no-auto-rehash
prompt = (\u@\h)[\d]>\_
default-character-set = gbk

8.3 初始化 MySQL 数据库:

$ /usr/local/mysql/scripts/mysql_install_db --datadir=/data/mysqldata/3306/data --basedir=/usr/local/mysql

8.4 启动数据库服务:

mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf &

总结:其实在配置完二进制版本的 MySQL 后发现,整个过程跟源码安装并没有什么区别,只是省去了最麻烦和最耗时的 MySQL 源码的编译和安装过程。非常适合相同硬件和系统,对 MySQL 服务的快速部署。

操作系统:CentOS 6.7
MySQL 版本:5.6.30

  • 1. 前期准备
  • 2. 系统配置
  • 3.CMake 编译配置
  • 4.make && make install
  • 5. 后期配置和测试
  • Reference

1. 前期准备

首先需要 CMake,可以 yum 直接安装:

yum install cmake

也可以官网 https://cmake.org/ 下载源码编译。
我这里选择了官网下载最新版本 cmake-3.5.2.tar.gz。

# tar -zxvf cmake-3.5.2.tar.gz && cd cmake-3.5.2

# ./configure
部分输出略。-- Build files have been written to: /soft/cmake-3.5.2
---------------------------------------------
CMake has bootstrapped.  Now run gmake.

# gmake 

# make install

2. 系统配置

添加组和用户:

groupadd mysql
useradd -g mysql mysql

vi /etc/security/limits.conf 文件末尾添加:

mysql   soft    nproc   2047
mysql   hard    nproc   16384
mysql   soft    nofile  1024
mysql   hard    nofile  65536

3.CMake 编译配置

解压源码包:

tar zxvf mysql-5.6.30.tar.gz && cd mysql-5.6.30

CMake 编译配置

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DCOMPILATION_COMMENT='JSS for mysqltest' \
-DWITH_READLINE=ON \
-DSYSCONFDIR=/data/mysqldata/3306 \
-DMYSQL_UNIX_ADDR=/data/mysqldata/3306/mysql.sock

遇到以下错误,

-- Could NOT find Curses (missing:  CURSES_LIBRARY CURSES_INCLUDE_PATH) 
CMake Error at cmake/readline.cmake:85 (MESSAGE):
  Curses library not found.  Please install appropriate package,

      remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on RedHat and derivates it is ncurses-devel.
Call Stack (most recent call first):
  cmake/readline.cmake:128 (FIND_CURSES)
  cmake/readline.cmake:202 (MYSQL_USE_BUNDLED_EDITLINE)
  CMakeLists.txt:421 (MYSQL_CHECK_EDITLINE)


-- Configuring incomplete, errors occurred!
See also "/soft/mysql-5.6.30/CMakeFiles/CMakeOutput.log".
See also "/soft/mysql-5.6.30/CMakeFiles/CMakeError.log".
[root@JY-DB mysql-5.6.30]# 

yum 安装提示缺失的包:

yum install ncurses-devel

重新删除配置文件:

rm -rf CMakeCache.txt

然后重新 CMake 工具编译:

CMake Warning:
  Manually-specified variables were not used by the project:

    WITH_READLINE


-- Build files have been written to: /soft/mysql-5.6.30
[root@JY-DB mysql-5.6.30]# 

4.make && make install

[root@JY-DB mysql-5.6.30]# make && make install
大量输出略。

这个时间会比较长,也跟机器性能有关。

5. 后期配置和测试

5.1 打包 MySQL 二进制版本:

[root@JY-DB data]# tar zcvf mysql-5.6.30.tar.gz /usr/local/mysql/

5.2 修改 MySQL 软件所在目录拥有者:

chown -R mysql.mysql /usr/local/mysql

5.3 修改 mysql 用户环境变量:

vi ~/.bash_profile

export LANG=zh_CN.GB18030
export PATH=/usr/local/mysql/bin:$PATH

5.4 创建数据库服务:

# mkdir -p /data/mysqldata/{3306/{data,tmp,binlog},backup,scripts}
# chown -R mysql.mysql /data/mysqldata
# su - mysql
$ more /usr/local/mysql/support-files/my-default.cnf 
$ vi /data/mysqldata/3306/my.cnf

my.cnf 配置文件内容如下:

[client]
port = 3306
socket = /data/mysqldata/3306/mysql.sock

#The MySQL Server
[mysqld]
port = 3306
user = mysql
socket = /data/mysqldata/3306/mysql.sock
pid-file = /data/mysqldata/3306/mysql.pid
basedir = /usr/local/mysql
datadir = /data/mysqldata/3306/data
tmpdir = /data/mysqldata/3306/tmp
open_files_limit = 10240
explicit_defaults_for_timestamp
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

#Buffer
max_allowed_packet = 256M
max_heap_table_size = 256M
net_buffer_length = 8k
sort_buffer_size = 2M
join_buffer_size = 4M
read_buffer_size = 2M
read_rnd_buffer_size = 16M

#Log
log-bin = /data/mysqldata/3306/binlog/mysql-bin
binlog_cache_size = 32M
max_binlog_cache_size = 512M
max_binlog_size = 512M
binlog_format = mixed
log_output = FILE
log-error = ../mysql-error.log
slow_query_log = 1
slow_query_log_file = ../slow_query.log
general_log = 0
general_log_file = ../general_query.log
expire-logs-days = 14

#InnoDB
innodb_data_file_path = ibdata1:2048M:autoextend
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_buffer_pool_size = 1024M

[mysql]
no-auto-rehash
prompt = (\u@\h)[\d]>\_
default-character-set = gbk

初始化 MySQL 数据库:

$ /usr/local/mysql/scripts/mysql_install_db --datadir=/data/mysqldata/3306/data --basedir=/usr/local/mysql

5.5 启动数据库服务:

mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf &

测试连接并查看 MySQL 进程和端口监听状态:

netstat -lnt | grep 3306
ps -ef | grep bin/mysql | grep -v grep

实际操作过程如下:

[root@JY-DB ~]# su - mysql
[mysql@JY-DB ~]$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.30-log JSS for mysqltest

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

(root@localhost)[(none)]> exit
Bye
[mysql@JY-DB ~]$ netstat -lnt |grep 3306
tcp        0      0 :::3306                     :::*                        LISTEN      
[mysql@JY-DB ~]$  
[mysql@JY-DB ~]$ ps -ef | grep bin/mysql | grep -v grep
mysql     6736  1753  0 11:24 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf
mysql     7202  6736  0 11:24 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysqldata/3306/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysqldata/3306/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/data/mysqldata/3306/data/../mysql-error.log --open-files-limit=10240 --pid-file=/data/mysqldata/3306/mysql.pid --socket=/data/mysqldata/3306/mysql.sock --port=3306

Reference

  • 李丙洋. 涂抹 MySQL. 水利水电出版社, 2014.  PDF 下载见 http://www.linuxidc.com/Linux/2016-06/132479.htm
  • dev.mysql.com

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2016-06/132477p2.htm

一、设定管理员用户和密码

二、处理 test 库权限隐患

三、自定义脚本提升易用性

  • 中间定义文件
  • 启动 MySQL 服务
  • 关闭 MySQL 服务
  • 快捷登录 MySQL

四、设置开机自动启动 MySQL 服务

Reference

一、设定管理员用户和密码

清除不安全的用户信息,设定管理员用户为 system,密码为 mysql。
具体操作步骤如下:

[mysql@JY-DB ~]$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.30-log JSS for mysqltest

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

(root@localhost)[(none)]> 
(root@localhost)[(none)]> select user, host from mysql.user;
+------+----------------+
| user | host           |
+------+----------------+
| root | 127.0.0.1      |
| root | ::1            |
|      | jy-db          |
| root | jy-db          |
|      | localhost      |
| root | localhost      |
+------+----------------+
6 rows in set (0.04 sec)

(root@localhost)[(none)]> delete from mysql.user where (user,host) not in (select 'root', 'localhost');
Query OK, 5 rows affected (0.05 sec)

(root@localhost)[(none)]> update mysql.user set user='system', password=password('mysql');
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0

(root@localhost)[(none)]> flush privileges;
Query OK, 0 rows affected (0.03 sec)

(root@localhost)[(none)]> \q
Bye

上面修改完成并刷新权限后,再次测试 MySQL 数据库连接,就必须需要指定用户名和密码登录了。具体操作步骤如下:

[mysql@JY-DB ~]$ mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[mysql@JY-DB ~]$ mysql -usystem -pmysql
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.30-log JSS for mysqltest

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

(system@localhost)[(none)]> 

二、处理 test 库权限隐患

查看当前 mysql.db 信息:

(system@localhost)[(none)]> select * from mysql.db \G
*************************** 1. row ***************************
                 Host: %
                   Db: test
                 User: 
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: Y
          Create_priv: Y
            Drop_priv: Y
           Grant_priv: N
      References_priv: Y
           Index_priv: Y
           Alter_priv: Y
Create_tmp_table_priv: Y
     Lock_tables_priv: Y
     Create_view_priv: Y
       Show_view_priv: Y
  Create_routine_priv: Y
   Alter_routine_priv: N
         Execute_priv: N
           Event_priv: Y
         Trigger_priv: Y
*************************** 2. row ***************************
                 Host: %
                   Db: test\_%
                 User: 
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: Y
          Create_priv: Y
            Drop_priv: Y
           Grant_priv: N
      References_priv: Y
           Index_priv: Y
           Alter_priv: Y
Create_tmp_table_priv: Y
     Lock_tables_priv: Y
     Create_view_priv: Y
       Show_view_priv: Y
  Create_routine_priv: Y
   Alter_routine_priv: N
         Execute_priv: N
           Event_priv: Y
         Trigger_priv: Y
2 rows in set (0.00 sec)

(system@localhost)[(none)]> 

处理 test 库权限安全隐患:

(system@localhost)[(none)]> truncate table mysql.db;
Query OK, 0 rows affected (0.04 sec)

(system@localhost)[(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

(system@localhost)[(none)]> select * from mysql.db \G
Empty set (0.00 sec)

(system@localhost)[(none)]> 

三、自定义脚本提升易用性

3.1 中间定义文件

创建中间定义文件,提高脚本的复用性。
vi /data/mysqldata/scripts/mysql_env.ini

# set env
MYSQL_USER=system
MYSQL_PASS='mysql'

# check parameter
if [$# -ne 1 ]
then
    HOST_PORT=3306
else
    HOST_PORT=$1
fi

由于文件包含密码等敏感信息,所以为了安全性,必须要修改文件的权限:

chmod 600 /data/mysqldata/scripts/mysql_env.ini

当然,如果对密码安全性要求很高,这里的配置文件中的密码可以置空,后续调用脚本手工输入密码即可。

3.2 启动 MySQL 服务

vi /data/mysqldata/scripts/mysql_db_startup.sh

 

#!/bin/sh

source /data/mysqldata/scripts/mysql_env.ini

echo "Startup MySQL Service: localhost_"${HOST_PORT}
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysqldata/${HOST_PORT}/my.cnf &

3.3 关闭 MySQL 服务

vi /data/mysqldata/scripts/mysql_db_shutdown.sh

#!/bin/sh

source /data/mysqldata/scripts/mysql_env.ini

echo "Shutdown MySQL Service: localhost_"${HOST_PORT}
/usr/local/mysql/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PASS} -S /data/mysqldata/${HOST_PORT}/mysql.sock shutdown

3.4 快捷登录 MySQL

vi /data/mysqldata/scripts/mysqlplus.sh

#!/bin/sh

source /data/mysqldata/scripts/mysql_env.ini

echo "Login MySQL Service:localhost_"${HOST_PORT}
/usr/local/mysql/bin/mysql -u${MYSQL_USER} -p${MYSQL_PASS} -S /data/mysqldata/${HOST_PORT}/mysql.sock $2

最后,统一授予所有自定义脚本执行的权限:

chmod u+x /data/mysqldata/scripts/*.sh

配置 mysql 用户的环境变量,追加一行:

echo "export PATH=/data/mysqldata/scripts:\$PATH" >> ~/.bash_profile
source ~/.bash_profile

至此,就可以在任意路径下执行脚本,提升了 MySQL 操作的易用性。

四、设置开机自动启动 MySQL 服务

在上述配置完成的基础上,
就可以直接在 root 用户下编辑 /etc/rc.local 文件,追加内容:

 

# autostart MySQL
sudo -i -u mysql /data/mysqldata/scripts/mysql_db_startup.sh 3306 > /home/mysql/mysql_db_startup.log 2>&1

Reference

  • 李丙洋. 涂抹 MySQL. 水利水电出版社, 2014. PDF 下载见 http://www.linuxidc.com/Linux/2016-06/132479.htm
  • dev.mysql.com
  • 1. 主节点授权同步用户
  • 2. 主节点修改配置文件
  • 3. 从节点修改配置文件
  • 4. 主节点锁定后查看状态
  • 5. 从节点配置并启动同步、查看状态
  • 6. 主节点解锁并验证从节点是否正常同步

环境:CentOS 6.7 + MySQL 5.6.30
主节点:192.168.56.102
从节点:192.168.56.103
已经分别安装好单机 MySQL,现在配置两台 MySQL 的主从复制。

1. 主节点授权同步用户

官方文档是分两步进行的:

mysql> CREATE USER ‘repl’@’%.mydomain.com’ IDENTIFIED BY ‘slavepass’;

mysql> GRANT REPLICATION SLAVE ON . TO ‘repl’@’%.mydomain.com’;

实际上一条命令即可:

grant replication slave,reload,super on *.* to 'repl'@'192.168.56.103' identified by 'asdfjkl';

2. 主节点修改配置文件

vi /data/mysqldata/3306/my.cnf

添加内容,其实主要是 server-id=102,另外对于 InnoDB,官档建议加两个参数:

[mysqld]
server-id=102
#binlog-ignore-db = mysql
#replicate-do-db = xxdb

#InnoDB
innodb_flush_log_at_trx_commit=1
sync_binlog=1

主节点重启 MySQL:

shell> mysqladmin shutdown
shell> mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf &

3. 从节点修改配置文件

vi /data/mysqldata/3306/my.cnf

添加:

[mysqld]
server-id=103
#replicate-do-db = xxdb

从节点重启 MySQL:

shell> mysqladmin shutdown
shell> mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf &

4. 主节点锁定后查看状态

主节点上锁:

mysql> FLUSH TABLES WITH READ LOCK;

主节点查看状态:

(root@localhost)[(none)]>  show master status
    -> ;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000011 |      432 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

5. 从节点配置并启动同步、查看状态

从节点配置:

mysql> CHANGE MASTER TO
    MASTER_HOST='192.168.56.102',
    MASTER_USER='repl',
    MASTER_PASSWORD='asdfjkl',
    MASTER_LOG_FILE='mysql-bin.000011',
    MASTER_LOG_POS=432;

这里的日志文件和当前位置是根据上一步查到的主节点状态填写的。

启动 SLAVE:

mysql> START SLAVE;

查看从节点状态:

mysql> show slave status \G

6. 主节点解锁并验证从节点是否正常同步

主节点解锁:

mysql> UNLOCK TABLES;

主节点建库:

mysql> create database xxdb;

从节点查看是否同步:

mysql> show databases;

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-06/132477.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计18717字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中