酷酷娱乐网

站内广告

MYSQL5.7.22 源码安装 主从搭建

时间:2020-09-24 12:00:02   作者:酷酷娱乐网   来源:www.kukuyl.com  
内容摘要:
操作系统版本
CENTOS7.6

MYSQL5.7.22




啥?为啥我的操作系统木有iso的安装镜......

操作系统版本

CENTOS7.6

MYSQL5.7.22



啥?为啥我的操作系统木有iso的安装镜像,哎算了,不能弄本地yum源,那就弄个网络的吧。只不过默认的用不了,那就自己动手丰衣足食


cd /etc/yum.repos.d/

mkdir bak

cp *.repo  bak/


# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo

-bash: wget: 未找到命令


WTF?原来是

原本的CentOS-Base.repo已被删除,所以没办法安装wget命令,所以用curl -O来获取

#curl -O  http://mirrors.163.com/.help/CentOS7-Base-163.repo


vi CentOS7-Base-163.repo

替换一些东西,不然可能无法使用

:%s#$releasever#7#g



yum list


OK!YUM源搞定了,下面开始搭建MYSQL


一、MYSQL源码安装 
(两个节点都安装,步骤一样的) 

安装环境: 
操作系统CENTOS7.6

NODE1 主机名 tsdb1      IP地址 192.168.159.4

NODE2 主机名 tsdb2       IP地址 192.168.159.5


1、安装依赖 
yum -y install make gcc-c++ cmake bison-devel  ncurses-devel perl 
2、创建用户和组  
groupadd mysql 
useradd mysql -g mysql -M -s /sbin/nologin 
3、下载5.7.22源码包 
https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

4、解压tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

cd /opt/

tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C  /home/

cd /home/

mv mysql-5.7.22-linux-glibc2.12-x86_64 mysql5.7.22


5、安装

mkdir -p /home/mysql5.7.22/data/

cd /home/

ln -s mysql5.7.22 mysql

cd mysql

chown -R mysql:mysql ./

./bin/mysqld --initialize --user=mysql --basedir=/home/mysql5.7.22/ --datadir=/home/mysql5.7.22/data/ --lc_messages_dir=/home/mysql5.7.22/share --lc_messages=en_US


.

./bin/mysqld --initialize --user=mysql --basedir=/home/mysql5.7.22/ --datadir=/home/mysql5.7.22/data/ --lc_messages_dir=/home/mysql5.7.22/share --lc_messages=en_US

2019-05-25T13:01:50.716538Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2019-05-25T13:01:51.897953Z 0 [Warning] InnoDB: New log files created, LSN=45790

2019-05-25T13:01:52.039050Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2019-05-25T13:01:52.116959Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 43c9f84c-7eed-11e9-a241-fefcfe4975bb.

2019-05-25T13:01:52.118956Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2019-05-25T13:01:52.119626Z 1 [Note] A temporary password is generated for root@localhost: kar)*.OtV4F)


安装速度贼快,刷~~~的一下就完事了。

kar)*.OtV4F)

这个是自动生成的临时密码

(如果看到有报这个错的直接安装就好)
bin/mysqld: error while loading shared libraries:  libaio.so.1: cannot open shared object file: No such file or directory

解决方法: yum install -y libaio   //安装后再去初始化就OK了)

#将mysql/目录下除了data/目录的所有文件,改回root用户所有
chown -R root .
#mysql用户只需作为mysql-5.7.22/data/目录下所有文件的所有者
chown -R mysql data

5.复制启动文件

cd /home/mysql/

cp ./support-files/mysql.server /etc/init.d/mysqld

chmod 755 /etc/init.d/mysqld

cp ./bin/my_print_defaults /usr/bin/ 

6.修改启动脚本

vi /etc/init.d/mysqld
#修改项:
basedir= /home/mysql5.7.22/

datadir= /home/mysql5.7.22/data/
port=3306


#加入环境变量,编辑 /etc/profile,这样可以在任何地方用mysql命令了
vi /etc/profile
#添加mysql路径,加入下面内容,按ESC-->:wq保存
export PATH=$PATH:/home/mysql5.7.22/bin

source /etc/profile

7.修改mysql配置项

vi /etc/my.cnf

[mysqld]
basedir =  /home/mysql5.7.22

datadir =  /home/mysql5.7.22/data


 8.启动mysql

service mysqld start

有遇到无法启动的情况,第一是将配置文件修改成上面简单的方式,第二是删除data目录下的文件,重新初始化。

mysql -uroot -pkar)*.OtV4F)

遇到无法启动报错的情况,例如:

2019-12-31T07:49:42.258557Z 0 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.

2019-12-31T07:49:42.259636Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2019-12-31T07:49:42.259803Z 0 [Warning] Failed to set up SSL because of the following SSL library error: SSL context is not usable without certificate and private key

2019-12-31T07:49:42.260385Z 0 [Note] Server hostname (bind-address): '*'; port: 3306

2019-12-31T07:49:42.260446Z 0 [Note] IPv6 is available.

2019-12-31T07:49:42.260462Z 0 [Note]   - '::' resolves to '::';

2019-12-31T07:49:42.260487Z 0 [Note] Server socket created on IP: '::'.

2019-12-31T07:49:42.261927Z 0 [Note] InnoDB: Loading buffer pool(s) from /data/mysql/mysql/ib_buffer_pool

2019-12-31T07:49:42.262277Z 0 [Note] InnoDB: Buffer pool(s) load completed at 191231 15:49:42

2019-12-31T07:49:42.264556Z 0 [Warning] Failed to open optimizer cost constant tables


2019-12-31T07:49:42.264839Z 0 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist

2019-12-31T07:49:42.264937Z 0 [ERROR] Aborting

(遇到这个报错问题,有可能是因为安装目录设置不对导致的,我试过自己重新安装

一个测试库,然后没有按笔记上的目录设置,自己设置了新的不同的目录如下,

[mysqld] 

basedir=/data/mysql/

datadir=/data/mysql/data/

[mysqld_safe]

log-error=/var/log/mysqld.log

然后重新安装的时候也重新设置下,如下:

./bin/mysqld --initialize --user=mysql --basedir=/data/mysql/ --datadir=/data/mysql/data/ --lc_messages_dir=/data/mysql-5.7.22-linux-glibc2.12-x86_64/share/ --lc_messages=en_US

然后安装目录的用户所属也要记得修改为mysql)


登陆后第一件事是先修改密码,否则敲啥命令都报错,老老实实改密码

进入mysql修改初始密码,修改远程连接的用户权限问题

alter user 'root'@'localhost' identified by 'TaB)*.Wtf4F)';   
flush privileges;    #刷新权限

mysql -uroot -pTaB)*.Wtf4F)

use mysql;

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

flush privileges;


CENTOS7.6版本开机不会自启动,需修改权限
chmod +x /etc/rc.d/rc.local



主从搭建

主库配置

vi /etc/my.cnf

[mysqld]

basedir=/home/mysql5.7.22/

datadir=/home/mysql5.7.22/data/

max_connections=3000

default-storage-engine=INNODB

character-set-server=utf8

slow_query_log_file = /home/mysql5.7.22/log/mysql-slow.log

slow_query_log=1

long_query_time = 5

log-queries-not-using-indexes = 1

bind-address=0.0.0.0

socket=/tmp/mysql.sock

user=mysql



#master

server-id=1

log-bin=mysql-bin

log-bin-index=mysql-bin.index

log-slave-updates=1

binlog-do-db=Master-slave

binlog-ignore-db=mysql

binlog_format=mixed

expire_logs_days=7



重启

service mysqld restart

主库配置同步用户

grant replication slave on *.* to 'Master-slave'@'192.168.159.5' identified by 'TaB)*.Wtf4F)';

flush privileges;


mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000002 |      601 | Master-slave | mysql            |                   |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)



从库配置

vi /etc/my.cnf

[mysqld]

basedir = /home/mysql5.7.22

datadir = /home/mysql5.7.22/data

max_connections=3000

default-storage-engine=INNODB

character-set-server=utf8

slow_query_log_file = /home/mysql5.7.22/log/mysql-slow.log

slow_query_log=1

long_query_time = 5

log-queries-not-using-indexes = 1

bind-address=0.0.0.0

socket=/tmp/mysql.sock

user=mysql



#slave

server-id=2

log-bin= mysql-bin

relay-log= mysql-relay-bin

read->

log-slave-updates=1

replicate-do-db=Master-slave

expire_logs_days=7

重启

service mysqld restart

在主库查询ID号和日志文件号

show master status;


再登陆从库

从库

mysql> CHANGE MASTER TO MASTER_HOST='192.168.159.4',MASTER_USER='Master-slave',MASTER_PASSWORD='TaB)*.Wtf4F)',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=601,MASTER_CONNECT_RETRY=10;


start slave;


查看

show slave status \G



这里有些地方还有疑惑,比如我在配置文件增加以下信息会无法启动数据库

[mysqld_safe]

log-error=/home/mysql5.7.22/log/tsdb1_error.log

pid-file=/home/mysql5.7.22/data/tsdb1.pid


启动会遇到报错

service mysqld start

Starting MySQL. ERROR! The server quit without updating PID file (/home/mysql5.7.22/data//tsdb1.pid).


一直查资料无法解决


以上都是我根据各种网上资料加上自己遇到的问题,总结修改后的,只作为写给自己的笔记,方便下次搭建的时候查看,只不过那个设置只给自己看的功能不见了。



注意一个问题,在生产环境,要注意时区问题

[mysqld]

default-time_zone = '+8:00'

使用北京时间的时区



error 1045密码错误

select * from mysql.user where user='root';


use mysql;

update user set authentication_string=password('123456') where user='root' and host='localhost';

update user set authentication_string=password('123456') where user='root' and host='%';

flush privileges;




error 1086密码过期


use mysql;

update user set password_expired="N" where user="root";

flush privileges;



主从切换

mysql主从关系中,如果master宕机了,则要提升slave为主,

    等原来的主库恢复之后,则要把老的主库变为从库,

    这样才能尽最大可能的保证应用层面的业务高可用性和数据的完整性。



## mysql主从切换为从主:

本文章url:https://www.kukuyl.com/new/30000911.html

相关文章

CopyRight 2018 - 2020 http://www.kukuyl.com 酷酷娱乐网 All Rights Reserved .