Centos7.x 版本环境下安装Mysql5.7操作记录
Centos7.x版本下针对Mysql的安装和使用多少跟之前的Centos6之前版本有所不同的,废话就不多赘述了,下面介绍下在centos7.x环境里安装mysql5.7的几种方法:
一、yum方式安装
Centos7.x版本下针对Mysql的安装和使用多少跟之前的Centos6之前版本有所不同的,废话就不多赘述了,下面介绍下在centos7.x环境里安装mysql5.7的几种方法:
一、yum方式安装
从CentOS 7.0发布以来,yum源中开始使用Mariadb来代替MySQL的安装。即使你输入的是yum install -y mysql , 显示的也是Mariadb的安装内容。
使用源代码进行编译安装又太麻烦。因此,如果想使用yum安装MySQL的话,就需要去下载官方指定的yum源.
yum下载网址为:https://dev.mysql.com/downloads/repo/yum/
找到Red Hat Enterprise Linux 7 / Oracle Linux 7 (Architecture Independent), RPM Package,单击后面的Download,
在新的页面中单击最下面的No thanks, just start my download.就可以下载到yum源了。
1)安装MySQL YUM资源库
[root@kevin ~]# yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
2)安装MySQL 5.7
[root@kevin ~]# yum install -y mysql-community-server
3)启动MySQL服务器和MySQL的自动启动
[root@kevin ~]# systemctl start mysqld.service
[root@kevin ~]# systemctl enable mysqld.service
4)密码问题
由于MySQL从5.7开始不允许首次安装后使用空密码进行登录!为了加强安全性,系统会随机生成一个密码以供管理员首次登录使用,
这个密码记录在/var/log/mysqld.log文件中,使用下面的命令可以查看此密码:
[root@kevin ~]# cat /var/log/mysqld.log|grep 'A temporary password'
2018-01-24T02:32:20.210903Z 1 [Note] A temporary password is generated for root@localhost: DOqInortw9/<
最后一行冒号后面的部分DOqInortw9/<就是初始密码。
使用此密码登录MySQL:
[root@kevin ~]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.21
Copyright (c) 2000, 2018, 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.
使用随机生产的密码登录到服务端后,必须马上修改密码,不然会报如下错误:
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
有两种方法解决上面的报错(如下的123456是修改后的密码):
mysql> set password=password("123456");
或者
mysql> alter user 'root'@'localhost' identified by '123456';
刷新权限
mysql> flush privileges;
===============================================================================================
如果上面在执行set password=password("123456");命令后出现下面的报错:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
解决办法:
这个与Mysql 密码安全策略validate_password_policy的值有关,validate_password_policy可以取0、1、2三个值:
0 or LOW Length
1 or MEDIUM Length; numeric, lowercase/uppercase, and special characters
2 or STRONG Length; numeric, lowercase/uppercase, and special characters; dictionary
默认的数值是1,符合长度,且必须含有数字,小写或大写字母,特殊字符。
所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。
有时候,只是为了自己测试,不想密码设置得那么复杂,譬如说,我只想设置root的密码为123456。
必须修改两个全局参数:
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
修改上面两个参数后,就可以解决这个报错了。
=======================================================================================================
注意一点:
mysql5.7之后的数据库里mysql.user表里已经没有password这个字段了,password字段改成了authentication_string。
所以修改密码的命令如下:
mysql> update mysql.user set authentication_string=password('kevin@123') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
=======================================================================================================
查看mysql版本
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.21 |
+-----------+
1 row in set (0.00 sec)
mysql>
=======================================================================================================
修改mysql5.7的编码由latin1为utf8
默认编码:
mysql> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.01 sec)
调整操作:
[root@kevin ~]# cat /etc/my.cnf
......
[mysqld]
......
character-set-server=utf8 //注意这个不能写成default-character-set=utf8,否则会导致5.7版本mysql无法打开
[client]
default-character-set=utf8
[root@kevin~]# systemctl restart mysqld.service
[root@kevin~]# mysql -p
......
mysql> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
mysql>
二、RPM包方式安装
1)卸载系统自带的 mysql和mariadb-lib
[root@kevin ~]# /bin/rpm -e $(/bin/rpm -qa | grep mysql|xargs) --nodeps
[root@kevin ~]# /bin/rpm -e $(/bin/rpm -qa | grep mariadb|xargs) --nodeps
2)下载mysql5.7.21 rpm安装包
下载地址:http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.7/
[root@kevin ~]# wget http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.7/mysql-5.7.21-1.el7.x86_64.rpm-bundle.tar
[root@kevin ~]# tar -vxf mysql-5.7.21-1.el7.x86_64.rpm-bundle.tar
[root@kevin ~]# ll
总用量 1160052
-rw-------. 1 root root 2090 1月 24 02:35 anaconda-ks.cfg
-rw-r--r--. 1 root root 593940480 12月 28 21:03 mysql-5.7.21-1.el7.x86_64.rpm-bundle.tar
-rw-r--r--. 1 7155 31415 25107316 12月 28 20:53 mysql-community-client-5.7.21-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 278844 12月 28 20:53 mysql-community-common-5.7.21-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 3779988 12月 28 20:53 mysql-community-devel-5.7.21-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 46256768 12月 28 20:53 mysql-community-embedded-5.7.21-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 24078148 12月 28 20:53 mysql-community-embedded-compat-5.7.21-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 128571868 12月 28 20:53 mysql-community-embedded-devel-5.7.21-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 2238596 12月 28 20:53 mysql-community-libs-5.7.21-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 2115904 12月 28 20:54 mysql-community-libs-compat-5.7.21-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 55662616 12月 28 20:54 mysql-community-minimal-debuginfo-5.7.21-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 171890056 12月 28 20:54 mysql-community-server-5.7.21-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 15289580 12月 28 20:54 mysql-community-server-minimal-5.7.21-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 118654584 12月 28 20:54 mysql-community-test-5.7.21-1.el7.x86_64.rpm
依次执行(几个包有依赖关系,所以执行有先后)下面命令安装
[root@kevin ~]# rpm -ivh mysql-community-common-5.7.21-1.el7.x86_64.rpm --force
[root@kevin ~]# rpm -ivh mysql-community-libs-5.7.21-1.el7.x86_64.rpm --force
[root@kevin ~]# rpm -ivh mysql-community-client-5.7.21-1.el7.x86_64.rpm --force
[root@kevin ~]# rpm -ivh mysql-community-server-5.7.21-1.el7.x86_64.rpm --force
=============================================================================================================
可能在安装mysql-community-server-5.7.21-1.el7.x86_64.rpm的时候会有如下报错:
[root@kevin ~]# rpm -ivh mysql-community-server-5.7.21-1.el7.x86_64.rpm --force
warning: mysql-community-server-5.7.21-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
libaio.so.1()(64bit) is needed by mysql-community-server-5.7.21-1.el7.x86_64
libaio.so.1(LIBAIO_0.1)(64bit) is needed by mysql-community-server-5.7.21-1.el7.x86_64
libaio.so.1(LIBAIO_0.4)(64bit) is needed by mysql-community-server-5.7.21-1.el7.x86_64
net-tools is needed by mysql-community-server-5.7.21-1.el7.x86_64
这个报错的意思是需要安装libaio包和net-tools包:
安装libaio-0.3.107-10.el6.x86_64.rpm
[root@kevin ~]# wget http://mirror.centos.org/centos/6/os/x86_64/Packages/libaio-0.3.107-10.el6.x86_64.rpm
[root@kevin ~]# rpm -ivh libaio-0.3.107-10.el6.x86_64.rpm --force
安装net-tools
[root@kevin ~]# yum install net-tools
=============================================================================================================
使用rpm安装方式安装mysql,安装的路径如下:
数据库目录
/var/lib/mysql/
配置文件
/usr/share/mysql(mysql.server命令及配置文件)
/etc/my.cnf
相关命令
/usr/bin(mysqladmin mysqldump等命令)
启动脚本
/etc/rc.d/init.d/(启动脚本文件mysql的目录)
3)数据库初始化
为了保证数据库目录为与文件的所有者为 mysql 登陆用户,如果你是以 root 身份运行 mysql 服务,需要执行下面的命令初始化
[root@kevin ~]# mysql_install_db --datadir=/var/lib/mysql //必须指定datadir,执行后会生成~/.mysql_secret密码文件
[root@kevin ~]# mysqld --initialize --user=mysql //新版的推荐此方法,执行生会在/var/log/mysqld.log生成随机密码。如果是以mysql身份运行,则可以去掉--user选项。
4)更改mysql数据库目录的所属用户及其所属组,然后启动mysql数据库
[root@kevin ~]# chown mysql:mysql /var/lib/mysql -R
[root@kevin ~]# systemctl start mysqld.service //启动mysql数据库服务
5)根据第3步中的密码登录到mysql,更改root用户的密码,新版的mysql在第一次登录后更改密码前是不能执行任何命令的
另外--initialize 选项默认以“安全”模式来初始化,则会为 root 用户生成一个密码并将该密码标记为过期,登陆后你需要设置一个新的密码,
而使用--initialize-insecure命令则不使用安全模式,则不会为 root 用户生成一个密码。
这里演示使用的--initialize初始化的,会生成一个 root 账户密码,密码在log文件里,如下最后的")1r3gi,hjgQa"即为随即生成的root密码
[root@kevin ~]# cat /var/log/mysqld.log
.......
07T04:41:58.420558Z 1 [Note] A temporary password is generated for root@localhost: )1r3gi,hjgQa
[root@kevin ~]# mysql -uroot -p')1r3gi,hjgQa'
mysql> set password=password('kevin@123');
mysql> flush privileges
三、编译方式安装
1)卸载系统自带的 mysql和mariadb-lib
[root@kevin ~]# /bin/rpm -e $(/bin/rpm -qa | grep mysql|xargs) --nodeps
[root@kevin ~]# /bin/rpm -e $(/bin/rpm -qa | grep mariadb|xargs) --nodeps
2)安装编译代码需要的包
/usr/bin/yum -y install make gcc-c++ cmake bison-devel ncurses-devel
[root@kevin ~]#
3)安装boost
[root@kevin ~]# mkdir -p /usr/local/boost
[root@kevin ~]# cd /usr/local/boost
[root@kevin boost]# wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
[root@kevin boost]# tar -zvxf boost_1_59_0.tar.gz
4)编译安装mysql5.7.21
[root@kevin ~]# /usr/sbin/groupadd mysql
[root@kevin ~]# /usr/sbin/useradd -g mysql mysql -M -s /sbin/nologin
[root@kevin ~]# cd /usr/local/src
[root@kevin src]# wget -c http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.7/mysql-5.7.21.tar.gz
[root@kevin src]# /bin/tar -zxvf mysql-5.7.21.tar.gz
[root@kevin src]# cd mysql-5.7.21/
[root@kevin mysql-5.7.21]# /usr/bin/cmake -DCMAKE_INSTALL_PREFIX=/data/mysql -DMYSQL_DATADIR=/data/mysql/data -DSYSCONFDIR=/etc
-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 -DWITH_BOOST=/usr/local/boost
[root@kevin mysql-5.7.21]# make && make install
5)修改/data/mysql权限
[root@kevin mysql-5.7.21]# mkdir -p /data/mysql/data
[root@kevin mysql-5.7.21]# /bin/chown -R mysql:mysql /data/mysql
[root@kevin mysql-5.7.21]# /bin/chown -R mysql:mysql /data/mysql/data
6)执行初始化配置脚本,创建系统自带的数据库和表
[root@kevin mysql-5.7.21]# /data/mysql/bin/mysql_install_db --basedir=/data/mysql --datadir=/data/mysql/data --user=mysql
7)配置my.cnf
[root@kevin mysql-5.7.21]# cat /data/mysql/my.cnf
[client]
port = 3306
socket = /data/mysql/var/mysql.sock
[mysqld]
port = 3306
socket = /data/mysql/var/mysql.sock
basedir = /data/mysql/
datadir = /data/mysql/data
pid-file = /data/mysql/data/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
sync_binlog=1
log_bin = mysql-bin
skip-name-resolve
#skip-networking
back_log = 600
max_connections = 3000
max_connect_errors = 3000
##open_files_limit = 65535
table_open_cache = 512
max_allowed_packet = 16M
binlog_cache_size = 16M
max_heap_table_size = 16M
tmp_table_size = 256M
read_buffer_size = 1024M
read_rnd_buffer_size = 1024M
sort_buffer_size = 1024M
join_buffer_size = 1024M
key_buffer_size = 8192M
thread_cache_size = 8
query_cache_size = 512M
query_cache_limit = 1024M
ft_min_word_len = 4
binlog_format = mixed
expire_logs_days = 30
log_error = /data/mysql/data/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/data/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
##lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
##default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 40960M
innodb_write_io_threads = 1000
innodb_read_io_threads = 1000
innodb_thread_concurrency = 8
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 4M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
#myisam_sort_buffer_size = 8M
#myisam_max_sort_file_size = 1G
#myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
port = 3306
8) 启动mysql服务
[root@kevin mysql-5.7.21]# cd /data/mysql
[root@kevin mysql]# /bin/mkdir var
[root@kevin mysql]# /bin/chown -R mysql.mysql var
[root@kevin mysql]# cp support-files/mysql.server /etc/init.d/mysql
[root@kevin mysql]# /sbin/chkconfig mysql on
[root@kevin mysql]# service mysql start
9) 设置环境变量
[root@kevin mysql]# echo "export PATH=$PATH:/data/mysql/bin" >> /etc/profile
[root@kevin mysql]# source /etc/profile
10)设置mysql登陆密码,初始密码为nextcloud@123
[root@kevin mysql]# /bin/mkdir -p /var/lib/mysql
[root@kevin mysql]# ln -s /data/mysql/var/mysql.sock /var/lib/mysql/mysql.sock
11)修改密码
由于MySQL从5.7开始不允许首次安装后默认使用空密码进行登录!并且mysql5.7之后的数据库里mysql.user表里已经没有password这个字段了,
password字段改成了authentication_string。
所以修改密码的命令如下:
[root@kevin mysql]# vim /data/mysql/my.cnf
......
[mysqld]
......
skip-grant-tables //先设置无密码登陆
[root@kevin mysql]# service mysql restart
[root@kevin mysql]# mysql -p
mysql> update mysql.user set authentication_string=password('kevin@123') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
然后再将/data/mysql/my.cnf配置文件中的"skip-grant-tables"去掉,重启mysql服务,就可以使用上面重置后的新密码kevin@123登陆了!
四、yum安装MariaDB
[root@kevin ~]# yum -y install mariadb mariadb-server [root@kevin ~]# systemctl start mariadb [root@kevin ~]# systemctl enable mariadb 接下来进行MariaDB的相关简单配置,设置密码,会提示先输入密码 [root@kevin ~]# mysql_secure_installation 首先是设置密码,会提示先输入密码 Enter current password for root (enter for none):<–初次运行直接回车 设置密码 Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车 New password: <– 设置root用户的密码 Re-enter new password: <– 再输入一次你设置的密码 其他配置 Remove anonymous users? [Y/n] <– 是否删除匿名用户,回车 Disallow root login remotely? [Y/n] <–是否禁止root远程登录,回车, Remove test database and access to it? [Y/n] <– 是否删除test数据库,回车 Reload privilege tables now? [Y/n] <– 是否重新加载权限表,回车 [root@kevin ~]# mysql -p123456 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 5.5.56-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 4 rows in set (0.01 sec) MariaDB [(none)]> 接下来配置MariaDB的字符集: -> 首先是配置文件/etc/my.cnf,在[mysqld]标签下添加 init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8_unicode_ci skip-character-set-client-handshake -> 接着配置文件/etc/my.cnf.d/client.cnf,在[client]中添加 default-character-set=utf8 -> 然后配置文件/etc/my.cnf.d/mysql-clients.cnf,在[mysql]中添加 default-character-set=utf8 最后是重启MariaDB,并登陆MariaDB查看字符集 [root@test-vm001 my.cnf.d]# systemctl restart mariadb [root@kevin ~]# mysql -p123456 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 4 Server version: 5.5.56-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show variables like "%character%";show variables like "%collation%"; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_unicode_ci | | collation_database | utf8_unicode_ci | | collation_server | utf8_unicode_ci | +----------------------+-----------------+ 3 rows in set (0.00 sec) MariaDB [(none)]> 为Confluence创建对应的数据库、用户名和密码 MariaDB [(none)]> create database confluence default character set utf8 collate utf8_bin; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> grant all on confluence.* to 'confluence'@'%' identified by 'confluencepasswd'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)
******************************我也想难过的时候到海边走走,可是我的城市没有海。******************************

浙公网安备 33010602011771号