mysql环境及配置
简介
MySQL5.7 和以前版本不同点:
*1.初始化工具不同*
MySQL5.6.xx使用的是mysql_install_db,MySQL5.7.6+官方推荐使用mysqld –initialize。
2.初始化数据库不同
MySQL5.6.xx初始化之后存在mysql,information_schema,performance_schema,test四个数据库,MySQL5.7.6+初始化之后存在mysql,information_schema,performance_schema,sys四个数据库。
3.初始化用户不同
MySQL5.6.xx初始化之后存在root@localhost,root@'::1',root@'hostname',''@'localhost',''@'hostname'五个用户,MySQL5.7.6+初始化之后存在mysql.sys,root@localhost用户
4.初始化root密码
MySQL5.6.xx初始化之后root用户密码为空,MySQL5.7.6+初始化之后会为root@localhost用户生成随机密码。
# 安装
# 在线安装【推荐】
yum -y install wget
# 安装mysql安装源
wget https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
yum -y localinstall mysql57-community-release-el7-11.noarch.rpm
# 安装MySQL
yum -y install mysql-community-server
# 这个配置在线安装太慢了; 要搞成本地源安装;
#优化后;
rpm -Uvh https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm \
&& yum -y install mysql-community-server
cp -rf /srv/app/soft/mysql/my.cnf /etc/my.cnf
2
3
4
5
6
7
8
9
10
11
12
systemctl start mysqld
# 设置开机启动
systemctl enable mysqld
systemctl daemon-reload
2
3
4
也是跟下面安装源码的包一样;
# rpm方式安装
# 卸载mariadb
检查是否安装了mariadb(CentOS7默认安装),如存在则卸载mariadb;如果你是CentOS6,则默认是MySQL,那么你就卸载MySQL即可;删除CentOS7自带的mariadb数据库
rpm -e --nodeps mariadb-libs marisa
rpm -qa | grep mariadb
rpm -e mariadb-libs-5.5.68-1.el7.x86_64 --nodeps
2
3
4
# 安装所需依赖
一般都是最新版了,以防万一后面安装出错,缺少依赖
yum install -y libaio net-tools numactl
# 安装MySQL包并启动
下载:wget http://dev.mysql.com/get/mysql-5.7.35-1.el7.x86_64.rpm-bundle.tar
安装:
安装时必须严格遵守安装顺序 依赖关系依次为 common → libs → client → server
注:ivh中, i-install安装;v-verbose进度条;h-hash哈希校验
rpm -ivh mysql-community-common-5.7.35-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.35-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.35-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.35-1.el7.x86_64.rpm
# 安装后查询安装的MySQL版本
mysqladmin --version
2
3
4
5
6
7
统一封装:
# install mysql
RUN yum install -y libaio net-tools numactl
# RUN wget http://dev.mysql.com/get/mysql-5.7.35-1.el7.x86_64.rpm-bundle.tar \
RUN cd /srv/app/soft/mysql \
&& tar -zxvf mysql-5.7.35.tar.gz \
&& cd mysql-5.7.35 \
&& rpm -ivh mysql-community-common-5.7.35-1.el7.x86_64.rpm \
&& rpm -ivh mysql-community-libs-5.7.35-1.el7.x86_64.rpm \
&& rpm -ivh mysql-community-client-5.7.35-1.el7.x86_64.rpm \
&& rpm -ivh mysql-community-server-5.7.35-1.el7.x86_64.rpm
2
3
4
5
6
7
8
9
10
默认安装位置
/var/lib/mysql/ #数据库目录
/usr/share/mysql #配置文件目录
/usr/bin #相关命令目录
/etc/init.d/mysql #启动脚本
/var/lib/mysql/ #数据文件:
/usr/share/mysql #配置文件模板:
/usr/bin mysql #客户端工具目录:
/var/log/ #日志目录:
/tmp/ pid, #sock文件目录:
2
3
4
5
6
7
8
9
10
创建及权限目录:
useradd -s /sbin/nologin mysql
rm -rf /var/lib/mysql
mkdir -p /var/lib/mysql/data
chown -R root /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
chown -R mysql /var/lib/mysql/data
2
3
4
5
6
# 初始化命令
/etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# 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
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#这部分是新加的内容;默认先跳过配置
#skip-grant-tables
character_set_server=utf8mb4
init_connect='SET NAMES utf8mb4'
lower_case_table_names=1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 初始化
#初始化mysql
rm -rf /var/lib/mysql/*
mysqld --initialize --user=mysql --basedir=/var/lib/mysql
#--datadir=/var/lib/mysql/data/
#初始化完成,并给root用户生成了临时密码,这里要把密码记录下来,稍后需要
cat /var/log/mysqld.log | grep password #查看临时密码
#清理一下日志文件
echo ''->/var/log/mysqld.log
#service mysqld start
#mysqld start --user root &
mysqld --defaults-file=/etc/my.cnf --user root &
#./support-files/mysql.server start
2
3
4
5
6
7
8
9
10
11
12
13
14
# 启动及查询状态
启动MySQL
mysqld --defaults-file=/etc/my.cnf &
#mysqld_safe --defaults-file=/etc/my.cnf &
#或者
service mysqld start
# 此时可以通过ps -ef | grep mysql看到相关进程
2
3
4
5
# 查询MySQL在系统的状态
[root@VM-8-15-centos ~]# systemctl status mysqld
# 启动MySQL数据库
[root@VM-8-15-centos ~]# systemctl start mysqld
# 关闭MySQL数据库
[root@VM-8-15-centos ~]# systemctl stop mysqld
# 重启MySQL数据库
[root@VM-8-15-centos ~]# systemctl restart mysqld
# 查看MySQL进程
[root@VM-8-15-centos ~]# ps -ef | grep mysql
2
3
4
5
6
7
8
9
10
# sql脚本操作
设置密码
mysql -uroot –p
SET PASSWORD = PASSWORD('123456'); #设置密码为123456
exit
mysql -uroot -p123456xxx
2
3
4
5
设置允许 root 从任意 ip 登陆; 允许远程登陆
mysql -uroot -p
use mysql;
#create user root@'192.168.0.2' identified by '123456';
#grant all privileges on databas_name.* to root@'192.168.0.2';
#ALTER USER 'root'@'192.168.0.2' IDENTIFIED WITH mysql_native_password BY '123456';
# select host,user from user;修改了密码,这里不做查询处理;要不然会报错提示;
update user set password=password('123456') where user='root';
update user set host='%' where user='root' and host='localhost'; # 原值为localhost
flush privileges;
#亦可使用:【推荐用这个脚本】
#set password for root@localhost = password('root123xx');
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root123xx' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'root123xx' WITH GRANT OPTION;
flush privileges;
#其中 “ *.* ” 代表所有资源所有权限, “ 'root'@% ” 其中 root 代表账户名,% 代表所有的访问地址,也可以使用一个唯一的地址进行替换,只有一个地址能够访问。如果是某个网段的可以使用地址与%结合的方式,如 10.0.42.%。IDENTIFIED BY 'root',这个 root 是指访问密码。WITH GRANT OPTION 允许级联授权。
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
优化简写:
mysql
update mysql.user set authentication_string=password('root123') where user='root';
flush privileges;
2
3
屏蔽跳过密码后,重启后;
# 关闭
systemctl stop mysqld.service
# 修改完毕密码后,需要把免密码认证取消:
vi /etc/my.cnf
skip-grant-tables这一行注释掉
#启动:
#systemctl start mysqld.service
2
3
4
5
6
7
再次设置远程连接登录;
select user,host from mysql.user;
set password for root@localhost = password('root-123');
grant all privileges on *.* to 'root'@'%' identified by 'root-123' with grant option;
grant all privileges on *.* to 'root'@'localhost' identified by 'root-123' with grant option;
flush privileges;
2
3
4
5
脚本设置处理;
# 编码查询
查看字符集和数据文件位置
show variables like '%collation%';
show variables like '%char%';
show variables like '%dir%';
2
3
# docker内部源码安装【重要】
# 源码安装
RUN yum install -y libaio net-tools numactl
RUN useradd -s /sbin/nologin mysql \
&& mkdir -p /var/lib/mysql/data \
&& chown -R root /var/lib/mysql \
&& chown -R mysql:mysql /var/lib/mysql \
&& chown -R mysql /var/lib/mysql/data
RUN cd /srv/app/soft/mysql \
&& tar -zxvf mysql-5.7.35.tar.gz \
&& cd mysql-5.7.35 \
&& rpm -ivh mysql-community-common-5.7.35-1.el7.x86_64.rpm \
&& rpm -ivh mysql-community-libs-5.7.35-1.el7.x86_64.rpm \
&& rpm -ivh mysql-community-client-5.7.35-1.el7.x86_64.rpm \
&& rpm -ivh mysql-community-server-5.7.35-1.el7.x86_64.rpm
RUN cp -rf /srv/app/soft/mysql/my.cnf /etc/my.cnf
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 启动及初始化
entrypoint.sh
rm -rf /var/lib/mysql/* \
&& mysqld --initialize --user=mysql --basedir=/var/lib/mysql
mysqld --defaults-file=/etc/my.cnf --user root &
sleep 10
# cat /var/log/mysqld.log | grep password
# 第二种替换方案;后面就不用sed替换了;【确保my.cnf中的配置修改没有设置跳过设置】
# mysqld --defaults-file=/etc/my.cnf --user root --skip-grant-tables &
mysql -uroot mysql < /srv/app/soft/mysql/setpwd.sql
ps -ef|grep mysqld |grep -v grep | awk '{print "kill -9",$2}'|sh
sed -i "s/\(skip-grant-tables\).*/#skip-grant-tables/" /etc/my.cnf
mysqld --defaults-file=/etc/my.cnf --user root &
sleep 10
# netstat -antlp
# 核心:--connect-expired-password
mysql -uroot -prootxxx --connect-expired-password mysql < /srv/app/soft/mysql/init.sql
# Please use --connect-expired-password option or invoke mysql in interactive mode.
# mysql -uroot -pxxxxx2 && source /srv/app/soft/mysql/init.sql
mysql -uroot -pxxxxx2 mysql < /srv/app/soft/mysql/dataportal.sql
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 相关脚本及配置
setpwd.sql
-- 初始化脚本
-- show databases;
-- mysqladmin -uroot -p create dataportal
-- create database dataportal default character set utf8mb4 collate utf8mb4_unicode_ci;
update mysql.user set authentication_string=password('rootxxx') where user='root';
flush privileges;
2
3
4
5
6
init.sql
-- 初始化脚本
set password for root@localhost = password('xxxxx2');
grant all privileges on *.* to 'root'@'%' identified by 'xxxxx2' with grant option;
grant all privileges on *.* to 'root'@'localhost' identified by 'xxxxx2' with grant option;
flush privileges;
2
3
4
5
my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#默认先跳过配置
skip-grant-tables
character_set_server=utf8mb4
init_connect='SET NAMES utf8mb4'
lower_case_table_names=1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 源码安装
这边源码编译安装不推荐,编译安装时间时间很慢,直接rpm安装会快很多;
# 版本
下面使用mysql-boost5.7.30.tar.gz进行安装,选择源码Source Code,选择带Boost的版本,点击Download开始下载。
注:Mysql5.7版本更新后有很多变化,连编译安装都有变化,而MySQL5.7的安装必须要boost库,不过MySQL的官网源码有带boost库的源码和不带boost库的源码两种,因此有两种安装方式,其实都是一样的,如果选择不带boost库源码的需要在安装MySQL之前单独安装boost,并在编译时指定boost的安装目录,选择带boost的版本,则不需要提前安装boost,只需要在cmake编译时带上参数-DWITH_BOOST=boost即可。
从 MySQL 5.7.5 开始 Boost 库是必需的,mysql 源码中用到了 C++的 Boost 库,要求必须安装 boost1.59.0 或以上版本, 这个选择源码安装;
删除CentOS7自带的mariadb数据库
rpm -e --nodeps mariadb-libs
rpm -e --nodeps marisa
2
# 安装依赖
yum install -y libaio cmake gcc-c++ bison-devel ncurses-devel bison perl perl-devel perl perl-devel
安装 libaio, libaio 包的作用是为了支持同步I/O,对于数据库之类的系统特别重要。
安装 cmake,CMake是一个跨平台的安装(编译)工具。
# 编译安装
解压安装包
tar -zxvf mysql-boost5.7.30.tar.gz
#添加mysql组及用户
#groupadd mysql
#useradd -r -g mysql mysql
useradd -s /sbin/nologin mysql
#设置权限
mkdir -p /usr/local/mysql/data
chown -R root /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql /usr/local/mysql/data
cd /usr/local/resource/mysql-5.7.30
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_BOOST=boost -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_ci
make&& make install
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
这里再写一份换行版方便查看参数,执行时选择上面这行命令
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_BOOST=boost
-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_ci
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cmake参数解释如下:
DCMAKE_INSTALL_PREFIX=/usr/local/mysql:安装路径
DMYSQL_DATADIR=/data/mysql:数据文件存放位置
DSYSCONFDIR=/etc:my.cnf路径
DWITH_MYISAM_STORAGE_ENGINE=1:支持MyIASM引擎
DWITH_INNOBASE_STORAGE_ENGINE=1:支持InnoDB引擎
DMYSQL_UNIX_ADDR=/data/mysql/mysqld.sock:连接数据库socket路径
DMYSQL_TCP_PORT=3306:端口
DENABLED_LOCAL_INFILE=1:允许从本地导入数据
DWITH_PARTITION_STORAGE_ENGINE=1:安装支持数据库分区
DEXTRA_CHARSETS=all:安装所有的字符集
DDEFAULT_CHARSET=utf8:默认字符
DWITH_EMBEDDED_SERVER=1:嵌入式服务器
# 配置初始化
设置全局命令
ln -s /usr/local/mysql/bin/mysqld /usr/bin/mysqld
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
2
也可以设置; 在/etc/profile 中新增一行
PATH=/usr/local/mysql/bin:$PATH
source /etc/profile
2
配置文件
配置自定义my.cnf
修改点;可以用默认的;cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/data #/usr/local/mysql/data TODO
pid-file = /data/mysql/mysql.pid
log_error = /data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql-slow.log
skip-external-locking
key_buffer_size = 32M
max_allowed_packet = 1024M
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 8K
read_buffer_size = 768K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 16
query_cache_size = 16M
tmp_table_size = 32M
performance_schema_max_table_instances = 1000
explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log_bin=mysql-bin
binlog_format=mixed
server_id = 232
expire_logs_days = 10
early-plugin-load = ""
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_buffer_pool_size = 128M
innodb_log_file_size = 32M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 768K
read_buffer = 2M
write_buffer = 2M
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
修改常用的配置:
vi /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
port=3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
symbolic-links=0
max_connections=400
innodb_file_per_table=1
#表名大小写不明感,敏感为
lower_case_table_names=1
2
3
4
5
6
7
8
9
10
skip-grant-tables
character_set_server=utf8mb4
init_connect='SET NAMES utf8mb4'
lower_case_table_names=1
2
3
4
初始化MySQL
#初始化MySQL
mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ --initialize
#初始化完成,并给root用户生成了临时密码,这里要把密码记录下来,稍后需要
cat /var/log/mysqld.log | grep password #查看临时密码
# 初始化MySQL,切记--defaults-file=/etc/my.cnf要放在参数的第一位,初始化信息可以在MySQL的errorlog中查看,并且在errorlog会生成一个root的随机密码,该随机密码仅仅为root@localhost用户所有。
./mysqld --defaults-file=/etc/my.cnf --initialize --basedir=/usr/local/web/mysql --datadir=/home/mysql/data --user=mysql
2
3
4
5
6
7
设置密码再打开验证
修改完毕密码后,需要把免密码认证取消:
update mysql.user set authentication_string=password('xxx') where user='root';
vi /etc/my.cnf
skip-grant-tables这一行注释掉
2
3
查看目前 mysql 字符集
#支持的所有的字符集
mysql> SHOW CHARACTER SET;
mysql> SHOW CHARACTER SET LIKE 'latin%';
#查看默认字符集
mysql> show variables like 'collation_%';
mysql> show variables like 'character_set_%';
2
3
4
5
6
# 设置服务启动
可选命令步骤; 添加 MySQL 服务到 systemd
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
#授予可执行的权限
chmod +x /etc/init.d/mysqld
#设置为开机启动
systemctl enable mysqld
2
3
4
5
6
检查是否启动;
#启动MySQL
mysqld_safe --defaults-file=/etc/my.cnf &
# 或者 service mysqld start
service mysqld start --user=root
#查看全部端口
netstat -antlp
ps -ef|grep mysql
ps -ef|grep mysqld
2
3
4
5
6
7
8
9
10
11
# 连接配置权限
#cd /usr/local/mysql/bin
#输入刚刚的密码
#./mysql -u root -p
mysql -u root -p
#修改MySQL用户root的密码为root
mysql> set password for root@localhost = password('root');
#赋予权限并设置可以远程访问
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
#刷新权限
mysql> FLUSH PRIVILEGES;
#查看权限
mysql> select user,host from mysql.user;
2
3
4
5
6
7
8
9
10
11
12
13
# 封装成docker方式安装
Dockerfile
RUN bash /install_mysql.sh # 触发安装脚本
install_mysql.sh
#!/bin/bash
rpm -e --nodeps mariadb-libs
yum install -y cmake gcc gcc-c++ bison bison-devel ncurses ncurses-devel perl perl-devel libaio cmake
pkgName="mysql-5.7.35.tar.gz"
# useradd -M -s /sbin/nologin mysql
groupadd mysql
useradd -r -g mysql mysql
mkdir -p /usr/local/mysql/data
chown -R root /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql /usr/local/mysql/data
# mkdir -p /mysql/data
# chown -R mysql:mysql /usr/local/mysql
# chown -R mysql.mysql /srv/dataIno/mysql
# chmod 750 /mysql/data
cd /srv/app/soft/mysql \
&& tar -zxvf mysql-5.7.35.tar.gz \
&& cd mysql-5.7.35 \
&& cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_BOOST=boost -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=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci \
&& make && make install
ln -s /usr/local/mysql/bin/mysqld /usr/bin/mysqld
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
# cd /usr/local/mysql/bin && ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ --initialize
# set password for root@localhost = password('x x');
# GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
# FLUSH PRIVILEGES;
# select user,host from mysql.user;
#容器内部启动
mysqld start --user root &
mysqladmin -u root -p shutdown
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# docker容器安装
# 启动容器
docker run -p 3306:3306 --name mysql-test \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
#-v /etc/mysql:/etc/mysql/conf.d
#-v $PWD/data:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=root123 \
-d mysql:5.7
####################################
# 若未成功可能为权限问题导致,运行 su root 提升至管理员权限
# 参数说明
# -p 3306:3306: 将容器的3306端口映射到主机的3306端口
# -v /mydata/mysql/log:/var/log/mysql 将日志文件夹挂在到主机
# -v /mydata/mysql/data:/var/lib/mysql 将配置文件夹挂在到主机
# -v /mydata/mysql/conf:/etc/mysql 将配置文件夹挂载到主机
# -e MYSQL_ROOT_PASSWORD=root123 初始化root用户的密码; 设置环境变量,这里设置的是root密码
# -d mysql:5.7 以后台方式运行
# 默认的配置文件位置为/etc/mysql/my.cnf,自定义的配置文件位置可以为/etc/mysql/conf.d或/etc/mysql/mysql.conf.d,因此将宿主机的/etc/mysql挂载到容器的/etc/mysql/conf.d,MySQL会自动合并默认的配置文件/etc/mysql/my.cnf与自定义的配置文件(这里是/etc/mysql/conf.d/my.cnf)。
###################################
docker exec -it mysql-test /bin/bash
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
database:
build: ./mysql/
command: mysqld --user=root --verbose
restart: always
environment:
MYSQL_DATABASE: "web_level3_sqli"
MYSQL_USER: "web_level3_sqli"
MYSQL_PASSWORD: "thisisasecurepassword123"
MYSQL_ROOT_PASSWORD: "root"
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
2
3
4
5
6
7
8
9
10
# 开机自启动
管理员权限运行命令
#sudo docker run --name=mysql --restart=always -e MYSQL_ROOT_PASSWORD=mypassword -v /path/to/directory:/var/lib/mysql -d mysql
docker update mysql --restart=always
2
# 修改MySQL配置
将拉丁编码修改为utf8编码
在cmd中输入命令 vi /mydata/mysql/conf/my.cnf
在 my.cnf
中添加
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
init_connect='SET collation_connection = utf8mb4_unicode_ci'
init_connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
2
3
4
5
6
7
8
9
10
11
12
13
运行命令docker restart mysql
重启mysql服务;
# 初始化
#查看一下原来容器的字符集
show variables like '%character%';
#登录mysql及修改密码
mysql -u root -p
#授权
set password = password(‘你的密码’);
user mysql
grant all privileges on . to ‘root’@’%’ identified by ‘root’ with grant option;
flush privileges;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxx!';
#添加远程登录用户
CREATE USER 'samy'@'%' IDENTIFIED WITH mysql_native_password BY 'xxxx!';
GRANT ALL PRIVILEGES ON *.* TO 'samy'@'%';
flush privileges;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 启动相关参数
MYSQL_ROOT_PASSWORD 该变量是强制性的,并指定将为MySQL root超级用户帐户设置的密码。在上面的例子中,它被设置为my-secret-pw。
MYSQL_DATABASE 此变量是可选的,并允许您指定要在映像启动时创建的数据库的名称。如果提供了用户/密码(见下文),那么该用户将被授予对该数据库的超级用户访问(对应于GRANT ALL)。
MYSQL_USER, MYSQL_PASSWORD 这些变量是可选的,可用于创建新用户并设置该用户的密码。该用户将被授予超级用户权限(见上文),该权限由MYSQL_DATABASE变量指定。这两个变量都是创建用户所必需的。
请注意,不需要使用此机制来创建超级用户的root用户,该用户默认情况下会使用MYSQL_ROOT_PASSWORD变量指定的密码创建。
MYSQL_ALLOW_EMPTY_PASSWORD 这是一个可选变量。设置为yes允许容器以root用户的空密码启动。注意:yes除非您真的知道您在做什么,否则不要将此变量设置为“不推荐”,因为这将使您的MySQL实例完全不受保护,从而允许任何人获得完整的超级用户访问权限。
MYSQL_RANDOM_ROOT_PASSWORD 这是一个可选变量。设置yes为为root用户(使用pwgen)生成随机初始密码。生成的root密码将打印到stdout(GENERATED ROOT PASSWORD: .....)。
MYSQL_ONETIME_PASSWORD 一旦初始化完成,将root用户(不是用户指定的用户MYSQL_USER)设置为已过期,强制首次登录时更改密码。注:该功能仅在MySQL 5.6+上受支持。在MySQL 5.5上使用此选项将在初始化期间引发相应的错误。
# Mac dmg 安装
# 安装步骤
记下数据库临时密码,一路下一步完成安装;
# 启动mysql
# 两种启动 mysql 的方式
偏好设置下一键启动
命令行启动
#启动MySQL服务
sudo mysql.server start
#停止MySQL服务
sudo mysql.server stop
#重启MySQL服务
sudo mysql.server restart
#查看MySQL服务状态
sudo mysql.server status
#登陆MySQL完整指令
mysql -h 127.0.0.1 -P 3306 -u root -p
2
3
4
5
6
7
8
9
10
11
12
13
14
# 修改密码
sudo mysql.server start
mysql -u root -p
set password = password('1234');
2
3
也可以直接用命令行修改:
mysqladmin -u root -p password newpass
连接后,工具直接提示你修改新密码:
记住密码时修改密码
如果你记得密码,登录之后,直接修改就可以了(root就是你的密码)
alter user root@localhost identified by 'root';
没记住密码时修改密码
a. 先停掉mysql的所有服务和进程
如果在偏好设置那里点击stop mysql没响应,可以直接kill掉,终端执行:
ps -ef|grep mysql
sudo kill -9 16034 #此处16034是MySQL的进程pid(第二列数字)
2
b. 执行命令以安全模式启动MySQL
cd /usr/local/mysql/bin
sudo ./mysqld_safe --skip-grant-tables
2
# 配置变量
vim .bash_profile
或者vim .zshrc
export PATH=$PATH:/usr/local/mysql/bin
export PATH=$PATH:/usr/local/mysql/support-files
2
source ~/.bash_profile
echo $PATH
mysql --version
2
3
alias mysql=/usr/local/mysql/bin/mysql
alias mysqladmin=/usr/local/mysql/bin/mysqladmin
2
# 配置文件
在/etc 目录下新建 my.cnf 配置文件,并赋予读写权限;
# 配置
# 配置防火墙
设置3306端口开放
firewall-cmd --zone=public --add-port=3306/tcp --permanent
重启防火墙
firewall-cmd --reload
看到success表示添加成功。
验证3306是否开放成功
firewall-cmd --zone=public --query-port=3306/tcp
# 启动及关闭mysql 服务
有两种方式,但必须组合使用,不能混用:
- 使用 systemctl (针对CentOS 7.0及以上版本,7.0 以下版本使用 service 命令)
systemctl start mysqld
systemctl stop mysqld
2
- 使用 mysql 程序自带的命令
# 由于云服务器默认root登陆,需要加上 --user root
# & 号表示后台运行,否则会阻塞
mysqld start --user root &
mysqladmin -u root -p shutdown
2
3
4
MySQL启动和关闭命令总结
#----启动MySQL
mysqld --defaults-file=/etc/my.cnf --user root & # docker中rpm安装的用这个启动;
mysqld --defaults-file=/etc/my.cnf &
mysqld_safe --defaults-file=/etc/my.cnf --user=mysql &
service mysql start
/etc/init.d/mysql start
mysqld_multi start #多实例
net start mysql #Windows
cat /var/log/mysqld.log | grep password #查看临时密码
#----关闭MySQL
mysqladmin shutdown -uroot -p #这种停止,修改密码后停止有问题;该用后面的直接杀死处理;
#mysqladmin shutdown -uroot -p -S /var/lib/mysql/mysql.sock
ps -ef|grep mysqld |grep -v grep | awk '{print "kill -9",$2}'|sh #[推荐]
service mysql stop
/etc/init.d/mysql stop
mysqld_multi stop #多实例
net stop mysql #Windows
#--杀死mysql
killall mysqld
killall -9 mysqld
kill -9 mysqld
#查看全部端口
netstat -antlp
ps -ef|grep mysql
ps -ef|grep mysqld
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
监控服务mysql是否启动
#while ! nslookup mysql-0 > /dev/null || ! nc -w1 mysql-0 3306 > /dev/null; do
while ! nslookup {{ .Values.wdpserver.mysqlName }} > /dev/null || ! nc -w1 {{ .Values.wdpserver.mysqlName }} {{ .Values.wdpserver.mysqlPort }} > /dev/null; do
tput cuu1
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [INFO] Waiting for WDP to Come up!"
sleep 0.1
done
sleep 10
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [INFO] Mysql is up, Will be starting WDP"
2
3
4
5
6
7
8
# 参考链接
- 官方自定义容器实现 (opens new window)
- https://www.cnblogs.com/louis-liu-oneself/p/13504270.html
- https://wangyeyixia.com/article/35548.html
- 安装过程
- http://blog.csdn.net/sungsasong/article/details/76278361
- https://segmentfault.com/a/1190000012099346
- http://www.centoscn.com/mysql/2016/0315/6844.html
- https://www.cnblogs.com/liqing1009/p/7879945.html
- make && makeinstall 时报错参考:http://www.yunweijilu.com/index.php/home/index/article/aid/135
- centos 7 防火墙设置参考 :https://jingyan.baidu.com/article/adc81513944addf723bf73af.html
- MySQL5.7初始密码查看及重置:http://blog.csdn.net/t1anyuan/article/details/51858911
- 源码安装后卸载再装:https://www.jianshu.com/p/e54ff5283f18
- MySQL常见错误解决方法:http://www.360doc.com/content/15/0201/13/3200886_445438524.shtml
- MYSQL MY.CNF 参数优化参考:http://www.cnblogs.com/yangchunlong/p/8478275.html