技术交流QQ群:①185473046   ②190706903   ③203744115   网站地图
登录

下次自动登录
现在的位置: 首页MySQL>正文
CentOS 8.x下二进制mysql5.5.x/5.6.x/5.7.x/8.0.x多版本共存
2021年11月09日 MySQL 暂无评论 ⁄ 被围观 5,278次+

准备篇

一、防火墙配置

#防火墙请根据个人需求进行选择和设置,下面以iptables防火墙为例

CentOS 8.x默认使用的是firewall作为防火墙,这里改为iptables防火墙。

1、关闭firewall:

systemctl stop firewalld.service #停止firewall

systemctl disable firewalld.service #禁止firewall开机启动

systemctl mask firewalld

systemctl stop firewalld

yum remove firewalld

2、安装iptables防火墙

yum install iptables-services #安装

vi /etc/sysconfig/iptables #编辑防火墙配置文件

# sample configuration for iptables service

# you can edit this manually or use system-config-firewall

# please do not ask us to add additional ports/services to this default configuration

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 3307 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 3308 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 3309 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT

:wq! #保存退出

systemctl restart iptables.service #最后重启防火墙使配置生效

systemctl enable iptables.service #设置防火墙开机启动

/usr/libexec/iptables/iptables.init restart #重启防火墙

二、关闭SELINUX

vi /etc/selinux/config

#SELINUX=enforcing #注释掉

#SELINUXTYPE=targeted #注释掉

SELINUX=disabled #增加

:wq! #保存退出

setenforce 0 #使配置立即生效

三 、安装依赖包

yum -y install perl perl-devel

yum install -y wget libaio-devel numactl-devel libncurses*

四、下载软件包

下载二进制版本

cd /usr/local/src

wget https://cdn.mysql.com/archives/mysql-5.5/mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz

wget https://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz

wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz

wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz

安装篇

一、安装mysql

1、创建mysql运行用户和组

groupadd mysql55

useradd -g mysql55 mysql55 -s /bin/false

groupadd mysql56

useradd -g mysql56 mysql56 -s /bin/false

groupadd mysql57

useradd -g mysql57 mysql57 -s /bin/false

groupadd mysql80

useradd -g mysql80 mysql80 -s /bin/false

2、创建mysql安装目录

mkdir -p /usr/local/mysql55

mkdir -p /usr/local/mysql56

mkdir -p /usr/local/mysql57

mkdir -p /usr/local/mysql80

3、设置mysql数据库存放目录及权限

mkdir -p /data/mysql55

mkdir -p /data/mysql56

mkdir -p /data/mysql57

mkdir -p /data/mysql80

chown -R mysql55:mysql55 /data/mysql55

chown -R mysql56:mysql56 /data/mysql56

chown -R mysql57:mysql57 /data/mysql57

chown -R mysql80:mysql80 /data/mysql80

4、安装mysql

cd /usr/local/src

tar zxvf mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz -C /usr/local/mysql55 --strip-components 1

tar zxvf mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz -C /usr/local/mysql56 --strip-components 1

tar zxvf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz -C /usr/local/mysql57 --strip-components 1

tar xvf mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz -C /usr/local/mysql80 --strip-components 1

二、设置my.cnf配置

find / -name my.cnf #查找my.cnf

rm -rf /etc/my.cnf #如果有请删除

1、设置mysql5.5.x

cp /usr/local/mysql55/support-files/my-huge.cnf /usr/local/mysql55/my.cnf

vi /usr/local/mysql55/my.cnf

# Example MySQL config file for very large systems.

#

# This is for a large system with memory of 1G-2G where the system runs mainly

# MySQL.

#

# MySQL programs look for option files in a set of

# locations which depend on the deployment platform.

# You can copy this option file to one of those

# locations. For information about these locations, see:

# http://dev.mysql.com/doc/mysql/en/option-files.html

#

# In this file, you can use all long options that a program supports.

# If you want to know which options a program supports, run the program

# with the "--help" option.

# The following options will be passed to all MySQL clients

[client]

#password = your_password

port = 3306

socket = /data/mysql55/mysql.sock

# Here follows entries for some specific programs

# The MySQL server

[mysqld]

user=mysql55

port = 3306

socket = /data/mysql55/mysql.sock

basedir = /usr/local/mysql55

datadir = /data/mysql55

pid-file= /data/mysql55/mysql.pid

skip-external-locking

key_buffer_size = 384M

max_allowed_packet = 1M

table_open_cache = 512

sort_buffer_size = 2M

read_buffer_size = 2M

read_rnd_buffer_size = 8M

myisam_sort_buffer_size = 64M

thread_cache_size = 8

query_cache_size = 32M

# Try number of CPU's*2 for thread_concurrency

thread_concurrency = 8

# Don't listen on a TCP/IP port at all. This can be a security enhancement,

# if all processes that need to connect to mysqld run on the same host.

# All interaction with mysqld must be made via Unix sockets or named pipes.

# Note that using this option without enabling named pipes on Windows

# (via the "enable-named-pipe" option) will render mysqld useless!

#

#skip-networking

# Replication Master Server (default)

# binary logging is required for replication

log-bin=mysql-bin

# required unique id between 1 and 2^32 - 1

# defaults to 1 if master-host is not set

# but will not function as a master if omitted

server-id = 1

# Replication Slave (comment out master section to use this)

#

# To configure this host as a replication slave, you can choose between

# two methods :

#

# 1) Use the CHANGE MASTER TO command (fully described in our manual) -

# the syntax is:

#

# CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,

# MASTER_USER=<user>, MASTER_PASSWORD=<password> ;

#

# where you replace <host>, <user>, <password> by quoted strings and

# <port> by the master's port number (3306 by default).

#

# Example:

#

# CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,

# MASTER_USER='joe', MASTER_PASSWORD='secret';

#

# OR

#

# 2) Set the variables below. However, in case you choose this method, then

# start replication for the first time (even unsuccessfully, for example

# if you mistyped the password in master-password and the slave fails to

# connect), the slave will create a master.info file, and any later

# change in this file to the variables' values below will be ignored and

# overridden by the content of the master.info file, unless you shutdown

# the slave server, delete master.info and restart the slaver server.

# For that reason, you may want to leave the lines below untouched

# (commented) and instead use CHANGE MASTER TO (see above)

#

# required unique id between 2 and 2^32 - 1

# (and different from the master)

# defaults to 2 if master-host is set

# but will not function as a slave if omitted

#server-id = 2

#

# The replication master for this slave - required

#master-host = <hostname>

#

# The username the slave will use for authentication when connecting

# to the master - required

#master-user = <username>

#

# The password the slave will authenticate with when connecting to

# the master - required

#master-password = <password>

#

# The port the master is listening on.

# optional - defaults to 3306

#master-port = <port>

#

# binary logging - not required for slaves, but recommended

#log-bin=mysql-bin

#

# binary logging format - mixed recommended

#binlog_format=mixed

# Uncomment the following if you are using InnoDB tables

#innodb_data_home_dir = /usr/local/mysql/data

#innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend

#innodb_log_group_home_dir = /usr/local/mysql/data

# You can set .._buffer_pool_size up to 50 - 80 %

# of RAM but beware of setting memory usage too high

#innodb_buffer_pool_size = 384M

#innodb_additional_mem_pool_size = 20M

# Set .._log_file_size to 25 % of buffer pool size

#innodb_log_file_size = 100M

#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

# Remove the next comment character if you are not familiar with SQL

#safe-updates

[myisamchk]

key_buffer_size = 256M

sort_buffer_size = 256M

read_buffer = 2M

write_buffer = 2M

[mysqlhotcopy]

interactive-timeout

:wq! #保存退出

2、设置mysql5.6.x

cp /usr/local/mysql56/support-files/my-default.cnf /usr/local/mysql56/my.cnf

vi /usr/local/mysql56/my.cnf

# For advice on how to change settings please see

# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the

# *** default location during install, and will be replaced if you

# *** upgrade to a newer version of MySQL.

[client]

port = 3307

socket=/data/mysql56/mysql.sock

[mysql]

port = 3307

socket = /data/mysql56/mysql.sock

[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

# These are commonly set, remove the # and set as required.

user=mysql56

port = 3307

basedir = /usr/local/mysql56

datadir = /data/mysql56

socket = /data/mysql56/mysql.sock

pid-file= /data/mysql56/mysql.pid

server_id = 1

character-set-server = utf8

default-storage-engine = InnoDB

tmpdir = /tmp

# 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

[mysqld_safe]

log-error = /data/mysql56/error.log

:wq! #保存退出

3、设置mysql5.7.x

vi /usr/local/mysql57/my.cnf

# For advice on how to change settings please see

# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the

# *** default location during install, and will be replaced if you

# *** upgrade to a newer version of MySQL.

[client]

port = 3308

socket=/data/mysql57/mysql.sock

[mysql]

port = 3308

socket = /data/mysql57/mysql.sock

[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

# These are commonly set, remove the # and set as required.

user=mysql57

port = 3308

basedir = /usr/local/mysql57

datadir = /data/mysql57

socket = /data/mysql57/mysql.sock

pid-file= /data/mysql57/mysql.pid

server_id = 1

character-set-server = utf8

default-storage-engine = InnoDB

tmpdir = /tmp

# 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

default_authentication_plugin=mysql_native_password

explicit_defaults_for_timestamp=true

[mysqld_safe]

log-error = /data/mysql57/error.log

:wq! #保存退出

4、设置mysql8.0.x

vi /usr/local/mysql80/my.cnf

# For advice on how to change settings please see

# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the

# *** default location during install, and will be replaced if you

# *** upgrade to a newer version of MySQL.

[mysql]

port = 3309

socket = /data/mysql80/mysql.sock

[client]

port = 3309

socket=/data/mysql80/mysql.sock

[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

# These are commonly set, remove the # and set as required.

user=mysql80

port = 3309

basedir = /usr/local/mysql80

datadir = /data/mysql80

socket = /data/mysql80/mysql.sock

pid-file= /data/mysql80/mysql.pid

server_id = 1

character-set-server = utf8

default-storage-engine = InnoDB

tmpdir = /tmp

skip-ssl

# 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

default_authentication_plugin=mysql_native_password

explicit_defaults_for_timestamp=true

[mysqld_safe]

log-error = /data/mysql80/error.log

:wq! #保存退出

三、初始化mysql数据库

配置文件my.cnf必须在其它参数之前

1、初始化mysql5.5.x

/usr/local/mysql55/scripts/mysql_install_db --defaults-file=/usr/local/mysql55/my.cnf --user=mysql55 --basedir=/usr/local/mysql55 --datadir=/data/mysql55 --socket=/data/mysql55/mysql.sock --pid-file=/data/mysql55/mysql.pid

2、初始化mysql5.6.x

/usr/local/mysql56/scripts/mysql_install_db --defaults-file=/usr/local/mysql56/my.cnf --user=mysql56 --basedir=/usr/local/mysql56 --datadir=/data/mysql56 --socket=/data/mysql56/mysql.sock --pid-file=/data/mysql56/mysql.pid

3、初始化mysql5.7.x

/usr/local/mysql57/bin/mysqld --defaults-file=/usr/local/mysql57/my.cnf --user=mysql57 --initialize-insecure --basedir=/usr/local/mysql57 --datadir=/data/mysql57 --socket=/data/mysql57/mysql.sock --pid-file=/data/mysql57/mysql.pid

4、初始化mysql8.0.x

/usr/local/mysql80/bin/mysqld --defaults-file=/usr/local/mysql80/my.cnf --user=mysql80 --initialize-insecure --basedir=/usr/local/mysql80 --datadir=/data/mysql80 --socket=/data/mysql80/mysql.sock --pid-file=/data/mysql80/mysql.pid

四、启动mysql数据库

配置文件my.cnf必须在其它参数之前

1、启动mysql5.5.x数据库

/usr/local/mysql55/bin/mysqld_safe --defaults-file=/usr/local/mysql55/my.cnf --user=mysql55 --port=3306 --basedir=/usr/local/mysql55 --datadir=/data/mysql55 --socket=/data/mysql55/mysql.sock --pid-file=/data/mysql55/mysql.pid &

2、启动mysql5.6.x数据库

/usr/local/mysql56/bin/mysqld_safe --defaults-file=/usr/local/mysql56/my.cnf --user=mysql56 --port=3307 --basedir=/usr/local/mysql56 --datadir=/data/mysql56 --socket=/data/mysql56/mysql.sock --pid-file=/data/mysql56/mysql.pid &

3、启动mysql5.7.x数据库

/usr/local/mysql57/bin/mysqld_safe --defaults-file=/usr/local/mysql57/my.cnf --user=mysql57 --port=3308 --basedir=/usr/local/mysql57 --datadir=/data/mysql57 --socket=/data/mysql57/mysql.sock --pid-file=/data/mysql57/mysql.pid &

4、启动mysql8.0.x数据库

/usr/local/mysql80/bin/mysqld_safe --defaults-file=/usr/local/mysql80/my.cnf --user=mysql80 --port=3309 --basedir=/usr/local/mysql80 --datadir=/data/mysql80 --socket=/data/mysql80/mysql.sock --pid-file=/data/mysql80/mysql.pid &

五、关闭mysql数据库

配置文件my.cnf必须在其它参数之前

1、关闭mysql5.5.x数据库

/usr/local/mysql55/bin/mysqladmin -uroot -S /data/mysql55/mysql.sock shutdown -p

2、关闭mysql5.6.x数据库

/usr/local/mysql56/bin/mysqladmin -uroot -S /data/mysql56/mysql.sock shutdown -p

3、关闭mysql5.7.x数据库

/usr/local/mysql57/bin/mysqladmin -uroot -S /data/mysql57/mysql.sock shutdown -p

4、关闭mysql8.0.x数据库

/usr/local/mysql80/bin/mysqladmin -uroot -S /data/mysql80/mysql.sock shutdown -p

六、设置mysql开机启动

1、设置mysql5.5.x开机启动

cp /usr/local/mysql55/support-files/mysql.server /etc/rc.d/init.d/mysqld55

chmod 755 /etc/init.d/mysqld55 #增加执行权限

chkconfig mysqld55 on #加入开机启动

vi /etc/rc.d/init.d/mysqld55 #编辑

basedir=/usr/local/mysql55 #MySQL程序安装路径

datadir=/data/mysql55 #MySQl数据库存放目录

conf=/usr/local/mysql55/my.cnf #修改mysql配置文件路径

:wq! #保存退出

service mysqld55 start #启动

service mysqld55 stop

service mysqld55 restart

2、设置mysql5.6.x开机启动

cp /usr/local/mysql56/support-files/mysql.server /etc/rc.d/init.d/mysqld56

chmod 755 /etc/init.d/mysqld56 #增加执行权限

chkconfig mysqld56 on #加入开机启动

vi /etc/rc.d/init.d/mysqld56 #编辑

basedir=/usr/local/mysql56 #MySQL程序安装路径

datadir=/data/mysql56 #MySQl数据库存放目录

conf=/usr/local/mysql56/my.cnf #修改mysql配置文件路径

:wq! #保存退出

service mysqld56 start #启动

service mysqld56 stop

service mysqld56 restart

3、设置mysql5.7.x开机启动

cp /usr/local/mysql57/support-files/mysql.server /etc/rc.d/init.d/mysqld57

chmod 755 /etc/init.d/mysqld57 #增加执行权限

chkconfig mysqld57 on #加入开机启动

vi /etc/rc.d/init.d/mysqld57 #编辑

basedir=/usr/local/mysql57 #MySQL程序安装路径

datadir=/data/mysql57 #MySQl数据库存放目录

conf=/usr/local/mysql57/my.cnf #修改mysql配置文件路径

:wq! #保存退出

service mysqld57 start #启动

service mysqld57 stop

service mysqld57 restart

4、设置mysql8.0.x开机启动

cp /usr/local/mysql80/support-files/mysql.server /etc/rc.d/init.d/mysqld80

chmod 755 /etc/init.d/mysqld80 #增加执行权限

chkconfig mysqld80 on #加入开机启动

vi /etc/rc.d/init.d/mysqld80 #编辑

basedir=/usr/local/mysql80 #MySQL程序安装路径

datadir=/data/mysql80 #MySQl数据库存放目录

conf=/usr/local/mysql80/my.cnf #修改mysql配置文件路径

:wq! #保存退出

service mysqld80 start #启动

service mysqld80 stop

service mysqld80 restart

七、把mysql服务加入系统环境变量,在最后一行添加

vi /etc/profile #把mysql服务加入系统环境变量:在最后添加下面这一行

export PATH=$PATH:/usr/local/mysql55/bin

export PATH=$PATH:/usr/local/mysql56/bin

export PATH=$PATH:/usr/local/mysql57/bin

export PATH=$PATH:/usr/local/mysql80/bin

:wq! #保存退出

source /etc/profile #使配置立即生效

八、初始化设置MySQL密码,根据提示设置密码

1、设置mysql5.5.x密码

vi /usr/local/mysql55/bin/mysql_secure_installation #编辑

#因为我们修改了默认的sock的位置/tmp/mysql.sock,所以需要把修改过的sock路径添加上去,否则会出错

#在make_config部分添加echo "socket=/data/mysql55/mysql.sock" >>$config

make_config() {

echo "# mysql_secure_installation config file" >$config

echo "[mysql]" >>$config

echo "user=root" >>$config

esc_pass=`basic_single_escape "$rootpass"`

echo "password='$esc_pass'" >>$config

#sed 's,^,> ,' < $config # Debugging

echo "socket=/data/mysql55/mysql.sock" >>$config

}

:wq! #保存退出

#执行设置命令,根据提示向导进行设置

/usr/local/mysql55/bin/mysql_secure_installation --socket=/data/mysql55/mysql.sock

#或者执行这个命令设置

/usr/local/mysql55/bin/mysqladmin -u root password '123456' --socket=/data/mysql55/mysql.sock

#以上两种方法选一个进行操作

2、设置mysql5.6.x密码

vi /usr/local/mysql56/bin/mysql_secure_installation #编辑

#因为我们修改了默认的sock的位置/tmp/mysql.sock,所以需要把修改过的sock路径添加上去,否则会出错

#在make_config部分删除参数"connect-expired-password"

#在make_config部分添加socket=/data/mysql56/mysql.sock"

sub make_config {

my $password = shift;

my $esc_pass = basic_single_escape($rootpass);

write_file($config,

"# mysql_secure_installation config file",

"[mysql]",

"user=root",

"password='$esc_pass'",

"socket=/data/mysql56/mysql.sock");

}

:wq! #保存退出

#执行设置命令

/usr/local/mysql56/bin/mysql_secure_installation --socket=/data/mysql56/mysql.sock

#或者执行这个命令设置

/usr/local/mysql56/bin/mysqladmin -u root password --socket=/data/mysql56/mysql.sock

[root@www mysql56]# /usr/local/mysql56/bin/mysqladmin -u root password --socket=/data/mysql56/mysql.sock

New password:

Confirm new password:

[root@www mysql56]#

#以上两种方法选一个进行操作

3、设置mysql5.7.x密码

/usr/local/mysql57/bin/mysql_secure_installation --socket=/data/mysql57/mysql.sock

y使用密码验证插件

选择密码规则(一般选择第二项:1 = MEDIUM)

#长度大于等于8

#长度大于等于8,数字、大小写字母、特殊符号

#长度大于等于8,数字、大小写字母、特殊符号和字典文件(慎选!)

设置新密码12345678Abc++

y删除匿名用户

y禁止root远程登录

y删除测试数据库

y重新加载权限表

4、设置mysql8.0.x密码

/usr/local/mysql80/bin/mysql_secure_installation --socket=/data/mysql80/mysql.sock

[root@www mysql80]# /usr/local/mysql80/bin/mysql_secure_installation --socket=/data/mysql80/mysql.sock

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords

and improve security. It checks the strength of password

and allows the users to set only those passwords which are

secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW Length >= 8

MEDIUM Length >= 8, numeric, mixed case, and special characters

STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 100

Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

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) : y

Success.

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!

[root@www mysql80]#

九、进入mysql控制台

1、进入mysql5.5.x控制台

/usr/local/mysql55/bin/mysql -uroot -p123456 -S /data/mysql55/mysql.sock

2、进入mysql5.6.x控制台

/usr/local/mysql56/bin/mysql -uroot -p123456 -S /data/mysql56/mysql.sock

3、进入mysql5.7.x控制台

/usr/local/mysql57/bin/mysql -uroot -p12345678Abc++ -S /data/mysql57/mysql.sock

4、进入mysql8.0.x控制台

/usr/local/mysql80/bin/mysql -uroot -p12345678Abc++ -S /data/mysql80/mysql.sock

十、创建数据库并设置权限

数据库名称:mydatabase

数据库用户名:mydatabaseuser

数据库密码:123456Abc++++

进入mysql控制台,执行以下命令

#mysql5.5.x/mysql5.6.x/mysql5.7.x

Create DATABASE IF NOT EXISTS mydatabase default charset utf8 COLLATE utf8_general_ci; #创建数据库

GRANT ALL PRIVILEGES ON mydatabase.* TO mydatabaseuser@'192.168.21.1' IDENTIFIED BY '123456Abc++++' WITH GRANT OPTION; #创建用户并授权

flush privileges; #刷新系统授权表

#mysql8.0.x

Create DATABASE IF NOT EXISTS mydatabase default charset utf8 COLLATE utf8_general_ci; #创建数据库

CREATE USER 'mydatabaseuser'@'192.168.21.1' IDENTIFIED WITH mysql_native_password BY '123456Abc++++'; #创建用户,加密方式为mysql_native_password

GRANT ALL PRIVILEGES ON mydatabase.* TO mydatabaseuser@'192.168.21.1' WITH GRANT OPTION; #授权用户

flush privileges; #刷新系统授权表

#mysql8.0.x版本把创建账户和授权用户分开为两个步骤

#mysql5.7.x/mysql8.0.x版本中如果对用户授权后只能连接数据库并无任何操作权限,请设置该用户对所有数据库有SUPER权限

grant SUPER on *.* to 'mydatabaseuser'@'192.168.21.1' ; #授予用户对所有数据库有SUPER权限

flush privileges; #刷新系统授权表

WITH GRANT OPTION #参数表示该用户可以将自己拥有的权限授权给其他用户

#设置root用户访问权限,默认只允许localhost连接

use mysql;

update user set host='%' where user='root';

grant all privileges on mydatabase.* to root@'%';

grant all privileges on *.* to root@'%';

十一、测试mysql数据库

使用mysql客户端进行连接测试,如下图

扩展阅读:

在MySQL中,允许存在多个my.cnf配置文件,有的能对整个系统环境产生影响,例如:/etc/my.cnf,有的则只能影响个别用户,例如:~/.my.cnf。

MySQL读取my.cnf配置文件的先后顺序是:

/etc/my.cnf

/etc/mysql/my.cnf

/usr/local/mysql/etc/my.cnf

~/.my.cnf

其他自定义路径下的my.cnf,例如:/usr/local/mysql80/my.cnf

不管是mysqld服务器端程序,还是mysql客户端程序,都可以采用下面两个参数来自行指定要读取的配置文件路径:

--defaults-file=/usr/local/mysql80/my.cnf 只读取指定的文件(不再读取其他配置文件)

-–defaults-extra-file=/usr/local/mysql80/my.cnf 从其他优先级更高的配置文件中/etc/my.cnf读取全局配置后,再读取指定的配置文件(有些选项可以覆盖掉全局配置中的设定值)

[root@www ~]# /usr/local/mysql80/bin/mysqld --help --verbose |grep my.cnf

/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf

my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default

#默认情况下,MySQL启动时会依次读取以上配置文件,如果有重复选项,会以最后一个文件设置的为准。

但是,如果启动时加入了--defaults-file=/usr/local/mysql80/my.cnf,以上的所有配置文件文件都不会读取.

二进制安装mysql使用总结:

1、mysql使用指定路径的配置文件my.cnf,放在默认/etc/my.cnf目录下,socket使用自定义路径--socket=/data/mysql57/mysql.sock,在mysql安装目录/usr/local/mysql57/bin下登录mysql控制台./mysql -uroot -p,不需要指定socket路径

2、mysql使用指定路径的配置文件my.cnf,放在自定义目录/usr/local/mysql57/my.cnf下,socket使用自定义路径--socket=/data/mysql57/mysql.sock,在mysql安装目录/usr/local/mysql57/bin下登录mysql控制台需指定socket文件路径./mysql -uroot -p -S /data/mysql57/mysql.sock

3、mysql使用指定路径的配置文件my.cnf,放在自定义目录/usr/local/mysql57/my.cnf下,socket使用默认路径--socket=/tmp/mysql.sock,在mysql安装目录/usr/local/mysql57/bin下登录mysql控制台./mysql -uroot -p,不需要指定socket路径

总结:

在mysql安装目录/usr/local/mysql57/bin下登录mysql控制台./mysql -uroot -p,默认会先去查找/etc/my.cnf文件里面设置的socket路径,并且默认使用此路径来登录,此时不需要指定socket路径;

如果没有/etc/my.cnf文件,会使用默认的--socket=/tmp/mysql.sock路径,此刻要想成功登录mysql控制台,就必须使用/usr/local/mysql57/my.cnf文件里面自定义的sock路径--socket=/data/mysql57/mysql.sock;

如果此时/usr/local/mysql57/my.cnf文件里面自定义的sock路径是默认的--socket=/tmp/mysql.sock,登录mysql控制台不需要指定socket路径。

socket路径的优先级顺序

1、/etc/my.cnf目录下配置的socket路径

2、默认socket路径--socket=/tmp/mysql.sock(包括自定义目录/usr/local/mysql57/my.cnf下配置的默认--socket=/tmp/mysql.sock路径)

除此之外,在mysql安装目录/usr/local/mysql57/bin下登录mysql控制台./mysql -uroot -p,都需要指定socket路径./mysql -uroot -p -S /data/mysql57/mysql.sock才能成功登录

至此,CentOS 8.x下二进制mysql5.5.x/5.6.x/5.7.x/8.0.x多版本共存教程完成。

     

  系统运维技术交流QQ群:①185473046 系统运维技术交流□Ⅰ ②190706903 系统运维技术交流™Ⅱ ③203744115 系统运维技术交流™Ⅲ

给我留言

您必须 [ 登录 ] 才能发表留言!



Copyright© 2011-2024 系统运维 All rights reserved
版权声明:本站所有文章均为作者原创内容,如需转载,请注明出处及原文链接
陕ICP备11001040号-3