MYSQL第一节:Mysql5.7 通用二进制安装方法

操作系统:CentOS 7.6

参考文档:

https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html

实际安装过程中,有些地方会与文档写的不一样。

 

如果本地有安装mariadb-libs,先卸载掉

[root@iZ8vbaz7sozl7oh8vu9vrsZ ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@iZ8vbaz7sozl7oh8vu9vrsZ ~]# yum remove mariadb-libs-5.5.60-1.el7_5.x86_64 -y

  

 

 

 

警告:MySQL对libaio 库有依赖性如果未在本地安装该库,则数据目录初始化和随后的服务器启动步骤将失败。

1、

先在操作系统安装libaio 库。

yum install libaio

  

 

2、

https://downloads.mysql.com/archives/community/

获取下载地址

 

 下载命令:

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

  

 

 

2、

shell> groupadd mysql

shell> useradd -r -g mysql -s /bin/false mysql

shell> cd /usr/local

shell> tar zxvf /root/mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz

shell> ln -s /usr/local/mysql-5.7.29-linux-glibc2.12-x86_64 mysql

shell> cd mysql

shell> mkdir mysql-files

shell> chown mysql:mysql mysql-files

shell> chmod 750 mysql-files

shell> chown mysql.mysql /usr/local/mysql

shell> bin/mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql

  

/******* 

注意:这步执行完成后会显示初始化的root用户密码

2020-05-14T05:38:52.063819Z 1 [Note] A temporary password is generated for root@localhost: O(TsKbE/I9hw

密码是:O(TsKbE/I9hw

 

********/

 

 

shell> bin/mysql_ssl_rsa_setup

  

/********

这一步不执行也可以,执行之后会开启SSL

********/

 

shell> bin/mysqld_safe --user=mysql &  

  

会显示如下日志,11382是mysqld_safe进程号:

[1] 11382
[root@iZ8vbaz7sozl7oh8vu9vrsZ mysql]# Logging to '/usr/local/mysql/data/iZ8vbaz7sozl7oh8vu9vrsZ.err'.
2020-05-15T00:20:24.365985Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

 

shell>ps -ef|grep mysql

  

可以看下有没有mysql进程,这里会出现两个,一个mysqld_safe,一个mysqld,本质上是mysqld命令去启动数据库,使用mysqld_safe启动数据库,它也是调用了mysqld命令去启动数据库,它会对mysqld进程做一些监控,如果mysqld进程挂掉,它会把mysqld进程重新拉起来。mysqld_safe可以理解为守护进程。

 

[root@iZ8vbaz7sozl7oh8vu9vrsZ mysql]# ps -ef|grep mysql
root 11382 11302 0 08:20 pts/0 00:00:00 /bin/sh bin/mysqld_safe --user=mysql
mysql 11452 11382 0 08:20 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=iZ8vbaz7sozl7oh8vu9vrsZ.err --pid-file=iZ8vbaz7sozl7oh8vu9vrsZ.pid
root 11484 11302 0 08:20 pts/0 00:00:00 grep --color=auto mysql

  

 

shell> cp support-files/mysql.server /etc/init.d/mysql.server

 

bin/mysql -uroot -p

mysql> select user,host from mysql.user;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

需要先修改密码。

 

mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

 

mysql> select user,host from mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
3 rows in set (0.00 sec)

mysql> update mysql.user set host ='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

  

 

在操作系统杀掉myql进程。

kill 11382;

kill 11452;

  

 

使用mysql服务启动竟然报错了,

[root@iZ8vbaz7sozl7oh8vu9vrsZ mysql]# service mysql start
Redirecting to /bin/systemctl start mysql.service
Failed to start mysql.service: Unit not found.

  

 

在一篇博客中找到了答案,

https://www.cnblogs.com/yuanchaoyong/p/9749060.html

参考解决方法二。

 

在 /etc/systemd/system/mysqld.service 添加Unit,既然错误提示找不到Unit那我们添加一个就好了。在 /etc/systemd/system/mysqld.service下添加如下内容

[Unit]
Description=MySQL Server
After=network.target
 
[Service]
ExecStart=/usr/bin/mysqld --defaults-file=/etc/mysql/my.cnf --datadir=/var/lib/mysql --socket=/var/run/mysqld/mysqld.sock User=mysql
Group=mysql
WorkingDirectory=/usr
 
[Install]
WantedBy=multi-user.target

 再操作系统执行:systemctl daemon-reload

  

 

 

再启动就可以成功了。

[root@iZ8vbaz7sozl7oh8vu9vrsZ mysql]# service mysql start
Redirecting to /bin/systemctl start mysql.service

  

 

vi ~/.bash_profile

在【export PATH】前加入

PATH=$PATH:/usr/local/mysql/bin

source ~/.bash_profile

  

在任意目录下执行都可以调用到mysql命令。

至此,mysql已经完成完成,并且可以正常使用。但所有参数都是默认值。

 

停掉数据库,修改一些参数。

service mysql stop

[root@iZ8vbaz7sozl7oh8vu9vrsZ data]# touch /usr/local/mysql/mysqld.log

[root@iZ8vbaz7sozl7oh8vu9vrsZ data]# chown mysql.mysql /usr/local/mysql/mysqld.log

  

 

https://www.jianshu.com/p/5f39c486561b

配置文件说明比较详细

 

修改一些参数,方便后期主备复制使用。

vi /etc/my.cnf

[client]
port = 3306
socket = /tmp/mysql.sock

[mysqld]
#基本设置
server-id=1
port=3306
skip-name-resolve
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
tmpdir=/tmp
log-error=/usr/local/mysql/mysqld.log
pid-file=/usr/local/mysql/mysqld.pid

#不区分大小写
lower_case_table_names = 1
sql_mode=''


default-time_zone = '+8:00'

transaction_isolation = READ-COMMITTED

max_connections=1000

interactive_timeout = 7200
wait_timeout = 7200

max_allowed_packet = 1024M
tmp_table_size = 64M
max_heap_table_size = 64M
read_rnd_buffer_size = 1M
sort_buffer_size = 1M
join_buffer_size = 1M
table_open_cache = 2000
thread_cache_size = 16
thread_stack = 512k

slow_query_log = 1
long_query_time = 3

log_bin = mysql-bin.log
max_binlog_size = 1024M
binlog_format = row
binlog_row_image=full
log_slave_updates

sync_binlog = 1
innodb_flush_log_at_trx_commit=1
innodb_buffer_pool_size = 512M
innodb_buffer_pool_instances = 1
innodb_lock_wait_timeout = 60
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_flush_method = O_DIRECT

 

 启动数据库

service mysql start

posted on 2020-05-15 09:38  柴米油盐酱醋  阅读(856)  评论(0编辑  收藏  举报

导航