一、安装并启动:
# yum install mariadb-server mysql mysql-server mysql-libs -y
# systemctl start mariadb.service
设置密码:
# mysqladmin -u root -p password "123qwe.."
二、使用mysqld_multi安装多实例mysql:
# vim /etc/my.cnf
1、添加实例的配置信息
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
2、以上是默认的配置信息,在mysqld下添加如下配置:
#分配serverid,不要和下面的从服务器冲突即可
server-id=1
#开启 bin-log 日志,不用log-bin=xxx,试验时这样子无法启动mysql服务,不知为何
log-bin
#开启慢查询日志, 记录查询过长的 sql 语句,以便于优化
log-slow-queries=/var/log/mysql/mysql-slow.log
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
3、在文件最后方添加:
[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = root
password = 123qwe..
#第二个mysql实例的配置
[mysqld1] #必须是mysqld+数字
socket = /tmp/mysqld1.sock
port = 3311
pid-file = /var/run/mariadb/mysqld1.pid
log-slow-queries = /var/log/mysqld1/slow-query-log ## mysql slow log
relay-log = /var/spool/mysqld1/mysqld-relay-bin ## relay for replication
datadir = /var/lib/mysqld1 ## mysql data file folder
user = mysql
log-bin
server-id=2
4、初始化数据库:
# mysql_install_db --user=mysql --datadir=/var/lib/mysqld1
5、启动该实例服务的方式为:
mysqld_multi start 1
使用命令查看3311端口是否开启:
# netstat -tunlp
如果有则启动成功
6、登陆该实例:
# mysql -S /tmp/mysqld1.sock -uroot -p
修改root密码也要指定sock文件:
# mysqladmin -S /tmp/mysqld1.sock -u root -p password "123qwe.."
进去之后可以使用查看datadir是否是配置文件中的路径
# show variables like '%datadir%';
三、配置主从复制:
1、主数据库设置root用户可以从任何地方登陆访问任何表:
> grant all on *.* to root@'%' identified by 'root' with grant option;
2、主数据库上新建一个用户backup用来给从数据库登陆,获取主数据库信息:
> GRANT REPLICATION SLAVE ON *.* to 'backup'@'192.168.137.31' identified by '123qwe..';
3、查看二进制日志是否开启:
> show variables like "%log_%";
log_bin处为ON则开启
4、查询主数据库信息File和Position:
> show master status;
5、从数据库执行:
> change master to master_host='192.168.137.21',master_port=3311,master_user='backup',master_password='123qwe..',master_log_file='mysqld1-bin.000006',master_log_pos=245;
如果报错Could not initialize master info structure, more error messages can be found in the MySQL error log:> reset slave;
再次错误:
ERROR 29 (HY000): File ‘/var/spool/mysqld1/mysqld-relay-bin.index’ not found (Errcode: 13)
创建对应的文件并修改权限
# sudo mkdir /var/spool/mysqld1
# sudo touch /var/spool/mysqld1/mysqld-relay-bin.index
# sudo chown -R mysql:mysql /var/spool/mysqld1/
6、开启从库:
> start slave;
7、查看状态信息:
> show slave status\G;
下面的两个必须都为Yes才是成功:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
注:多实例配置后登录失败重置密码:
1、杀掉mysql后执行linux命令:
# mysqld_safe --skip-grant-tables &
就可以直接跳过密码进入mysql
多实例则需要加上相关实例的参数:
# mysqld_safe --datadir=/data/mysql/data1 --pid-file=/data/mysql/data1/mysql1.pid --socket=/tmp/mysql1.sock --port=3307 --skip-grant-tables &
2、无密码进入mysql:
# mysql -uroot -p -S /data/3306/mysql.sock
直接回车进入
3、查询密码:
查询密码
mysql> select user,host,authentication_string from mysql.user\G;
*************************** 1. row ***************************
user: root
host: localhost
authentication_string: *2A032F7C5BA932872F0F045E0CF6B53CF702F2C5
*************************** 2. row ***************************
user: mysql.sys
host: localhost
authentication_string: *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE
2 rows in set (0.00 sec)
4、重置密码:
mysql> update mysql.user set authentication_string=password("123456") where user='root';
# 修改使用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> exit
5、重新启动实例