单机Ubuntu安装第二个Mysql服务
第一个Mysql
服务是按照官方的apt-get
下载下来的,唯一改的就是配置文件
安装第二个Mysql
服务的原因是因为需要部署主从同步+读写分离
一、安装配置第二个mysql
服务
-
新建目录
-
新建实例目录
mkdir /usr/mysql/mysql_3307
-
赋权
chown -R mysql.mysql /usr/mysql/mysql_3307
-
在
apparmor
中配置对目录文件的访问权限# 编辑配置 vim /etc/apparmor.d/usr.sbin.mysqld /usr/mysql/mysql_3307/ r, /usr/mysql/mysql_3307/** rwk, /opt/mysql/mysql_3307/mysqld_3307.pid rw, /opt/mysql/mysql_3307/data/ r, /opt/mysql/mysql_3307/data/** rwk, /opt/mysql/mysql_3307/log/ r, /opt/mysql/mysql_3307/log/** rw, # 刷新培训 service apparmor restart
-
-
复制多个
MySQL
配置文件mysql
启动时会读取配置文件my.cnf
,要配置多个实例需要复制多个my.cnf配置文件。在mysql5.7版本中
/etc/mysql/
目录下的my.cnf
文件是个软链接,关联的是/etc/mysql/mysql.conf.d
目录下的mysqld.cnf
文件,配置信息全部在mysqld.cnf
中,所以可以直接复制mysqld.cnf
命名为mysqld_3307.cnf
作为启动配置文件。## 修改mysqld_3307.cnf配置 ## [mysqld_safe] socket = /usr/mysql/mysql_3307/mysqld.sock # nice = 0 [mysqld] user = mysql pid-file = /usr/mysql/mysql_3307/mysqld.pid socket = /usr/mysql/mysql_3307/mysqld.sock port = 3307 basedir = /usr datadir = /usr/mysql/mysql_3307 tmpdir = /tmp lc-messages-dir = /usr/share/mysql skip-external-locking bind-address = 0.0.0.0 log_error = /usr/mysql/mysql_3307/error.log
-
初始化
Mysql
文件# 5.7版本 mysqld --initialize --user=mysql --basedir=/usr --datadir=/usr/mysql/mysql_3307 # 5.6版本会出现以下提示 mysql_install_db --defaults-file=/etc/mysql/mysql.conf.d/mysqld_3307.cnf --basedir=/usr/ --datadir=/usr/mysql/mysql_3307 --user=mysql # 2111:29:05[WARNING]mysql_install_db is deprecated. Please consider switching to mysqld --initialize
-
修改密码
-
启动实例
先通过安全模式启动实例,跳过安全认证登录后修改root密码
mysqld_safe --defaults-file=/etc/mysql/mysql.conf.d/mysqld_3307.cnf &
- 修改密码
# 无密码登录 mysql -S /usr/mysql/mysql_3307/mysqld.sock -P 3307 # 修改密码 update user set authentication_string=password('123456') where user='root'; flush privileges; quit;
- 关闭实例后再次启动实例
# 关闭实例 mysqladmin -u root -p -S /usr/mysql/mysql_3307/mysqld.sock shutdown
异常:登录后执行命令的时候提示错误
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this start # 解决方案:设置密码 set password="YOUR_PASSWORD";
异常:使用无密码登录时,出现异常,链接
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) # 在[mysqld]后添加skip-grant-tables(使用 set password for设置密码无效,且此后登录无需键入密码) skip-grant-tables
-
二、主从同步配置
- 主库配置(
/etc/mysql/mysql.conf.d/mysqld.cnf
)
# 设置master的serverid
server-id = 1
# 要生成的二进制日记文件名称
log_bin = /var/log/mysql/mysql-bin.log
# 要同步的数据库
binlog_do_db = marcosys
- 为主服务器注册新用户,用于从库链接
grant replication slave on *.* to 'replication'@'%' identified by '123456';
- 异常
# 若出现异常:
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
# 方案
flush privileges;
- 记录
File
和Position
的值
show master status;
- 从库配置(
/etc/mysql/mysql.conf.d/mysqld_3307.cnf
)
server-id = 2
replicate-do-db=marcosys
- 从库设置
# 设置主库信息
change master to master_host='127.0.0.1',master_port=3306,master_user='replication',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=767;
# 启动从机
start slave;
# 检查状态
show slave status\G;
# 状态标准
lave_IO_Running: Yes
Slave_SQL_Running: Yes
Master_Log_File: mysql-bin.000001
Relay_Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 767
Exec_master_log_pos: 767