1 第一种
vim /etc/yum.repos.d/mysql.repo
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
清华的MySQL源
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql57-community-el7
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
yum install mysql-community-server mysql-devel -y
systemctl enable mysqld --now
grep "password" /var/log/mysqld.log
如果没有密码, /var/log/mysqld.log是空的,可以重置mysql密码。
vim /etc/my.cnf
[mysqld]
skip-grant-tables
保存
systemctl restart mysqld
2第二种
rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
yum -y install mysql-community-server --nogpgcheck
systemctl enable mysqld
systemctl start mysqld
systemctl status mysqld
grep 'temporary password' /var/log/mysqld.log |awk '{print $11}'
mysql_secure_installation
mysql -uroot -p
use mysql;
update user set authentication_string=password('密码') where user='root' and host='localhost';
flush privileges;
MySQL 5.7修改密码
password字段 已从mysql.user表中删除,新的字段名是“authenticalion_string”.
use mysql;
update user set authentication_string=password('123') where user='root' and host='localhost';
flush privileges;
提示这个请用下面的命令修改密码
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
ALTER USER USER() IDENTIFIED BY '密码';
flush privileges;
mysql 8.0 安装加修改密码
grep 'temporary password' /var/log/mysqld.log
查看默认密码
wget https://repo.mysql.com//mysql80-community-release-el7-11.noarch.rpm
rpm -ivh mysql80-community-release-el7-11.noarch.rpm
yum -y install mysql-community-server --nogpgcheck
systemctl enable mysqld --now
vi /etc/my.cnf
[mysqld]
ship-grant-tables
service mysqld restart
mysql -uroot -p
use mysql;
update user set authentication_string='' where user='root';
flush privileges;
vi /etc/my.cnf
[mysqld]
#skip-grant-tables
service mysqld restart
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';
flush privileges;
mariadb 修改密码
use mysql;
update user set Password = password('123') where user ='root';重设密码
flush privileges;
alter user '用户名'@'%' identified by '密码';
alter user 'slave'@'%' identified by 'iUN[kQ2&[Nw}jRU{TUr}#JZbuULD}';
update mysql.user set password=password("密码") where user="root" and host='localhost';
flush privileges;
vim /etc/my.cnf
[mysqld]
#skip-grant-ables
systemctl restart mysqld
拿到初始密码
初始化mysql
mysql_secure_installation
修改密码
use mysql;
update user set authentication_string=password('123') where user='root' and host='localhost';
flush privileges;
ALTER USER USER() IDENTIFIED BY '123';
flush privileges;
编辑 vim /etc/my.cnf 主库
[client]
#default_character_set=utf8mb4
[mysqld]
server_id=62
port=3306
character_set_server=utf8mb4 #默认编码格式
default_storage_engine=innodb #默认引擎设置为innodb
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
#skip-grant-tables=1
symbolic-links=0
log_bin=/var/lib/mysql/mysql-bin
binlog_format=row
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysql]
prompt=db01
default_character_set=utf8mb4
编辑从库 vim /etc/my.cnf 从库
[client]
#default_character_set=utf8mb4
[mysqld]
server_id=193
port=3306
secure-file-priv=/tmp
character_set_server=utf8mb4 #默认编码格式
default_storage_engine=innodb #默认引擎设置为innodb
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
#skip-grant-tables=1
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log_bin=/var/lib/mysql/mysql-bin
binlog_format=row
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
[mysql]
prompt=db02
default_character_set=utf8mb4
重启mysql
service mysqld restart
主库创建同步账号
CREATE USER 'repl'@'%' IDENTIFIED with mysql_native_password BY '123';
GRANT replication slave on *.* TO 'repl'@'%';
查看主库log position
show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000006 | 1186 | | | b9479874-47f7-11ee-8680-fa163e0e6601:1-5 |
+------------------+----------+--------------+------------------+------------------------------------------+
从库
change master to master_host='192.168.1.10',master_port=3306,master_user='repl',master_password='123',master_auto_position=1186,MASTER_HEARTBEAT_PERIOD=2,MASTER_CONNECT_RETRY=1,MASTER_RETRY_COUNT=86400;
start slave;
show slave status\G
成功标志
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
在主库创建数据库,然后再从库查看是否同步过来。
create database test;
show databases;
ubuntu 18.04安装mysql5.7.42
删除旧的mysql
805 2023-09-23 20:40:23 root (192.168.1.37) sudo apt-get autoremove --purge mysql-server-5.7
806 2023-09-23 20:40:40 root (192.168.1.37) dpkg --list|grep mysql
807 2023-09-23 20:40:49 root (192.168.1.37) sudo apt update
808 2023-09-23 20:42:10 root (192.168.1.37) sudo apt upgrade
809 2023-09-23 20:42:30 root (192.168.1.37) dpkg --list|grep mysql
810 2023-09-23 20:42:44 root (192.168.1.37) sudo apt-get autoremove --purge mysql-apt-config
811 2023-09-23 20:43:27 root (192.168.1.37) sudo dpkg -i mysql-apt-config_0.8.26-1_all.deb
812 2023-09-23 20:48:00 root (192.168.1.37) dpkg --list|grep mysql
813 2023-09-23 20:48:06 root (192.168.1.37) sudo apt-get autoremove --purge mysql-apt-config
apt源
sudo vim /etc/apt/sources.list.d/mysql.list
deb http://repo.mysql.com/apt/ubuntu/ bionic mysql-apt-config
deb http://repo.mysql.com/apt/ubuntu/ bionic mysql-5.7
deb http://repo.mysql.com/apt/ubuntu/ bionic mysql-tools
deb-src http://repo.mysql.com/apt/ubuntu/ bionic mysql-5.7
sudo apt update
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 467B942D3A79BD29
sudo apt update
查看mysql版本
sudo apt-cache policy mysql-server
sudo apt install mysql-client=5.7.42-1ubuntu18.04
sudo apt install mysql-server=5.7.42-1ubuntu18.04
vim /etc/mysql/mysql.conf.d/mysqld.cnf
[client]
default_character_set=utf8mb4
[mysqld]
server_id=97
port=3306
character_set_server=utf8mb4 #默认编码格式
default_storage_engine=innodb #默认引擎设置为innodb
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log_bin=/var/lib/mysql/mysql-bin
binlog_format=row
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
#log-error=/var/log/mysqld.log
#pid-file=/var/run/mysqld/mysqld.pid
[mysql]
prompt=db97
default_character_set=utf8mb4
systemctl restart mysql
其他yucentos主从设置相同