正文
一、传统方式
1、下载好安装包(Linux - Generic (glibc 2.12) (x86, 64-bit), Compressed TAR Archive)上传到服务器解压缩
#执行之后是tar格式文件,再解压 [root@bogon ~]# xz -d mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz #解压 [root@bogon ~]# tar -xvf mysql-8.0.27-linux-glibc2.12-x86_64.tar #移动重命名 [root@bogon ~]# mv mysql-8.0.27-linux-glibc2.12-x86_64 /usr/local/mysql
或者
[root@localhost ~]# tar -Jxvf mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
2、在mysql目录下创建data文件存放数据
[root@bogon ~]# mkdir -p /usr/local/mysql/data
3、创建mysql用户组和用户
[root@bogon ~]# groupadd mysql [root@bogon ~]# useradd -g mysql mysql
4、授权
[root@bogon ~]# chown -R mysql.mysql /usr/local/mysql/
5、创建mariadb文件并授权给 mysql
[root@bogon mysql]# mkdir /var/log/mariadb [root@bogon mysql]# touch /var/log/mariadb/mariadb.log [root@bogon mysql]# chown -R mysql:mysql /var/log/mariadb/
6、配置mysql的my.cnf如下
[mysql] # 设置mysql客户端默认字符集 default-character-set=utf8mb4 [client] port = 3306 socket = /usr/local/mysql/mysql.sock [mysqld] port = 3306 user = mysql socket = /usr/local/mysql/mysql.sock # 设置mysql的安装目录 basedir = /usr/local/mysql # 设置mysql数据库的数据的存放目录 datadir = /usr/local/mysql/data #配置主节点信息 server_id=3306 log-bin = /usr/local/mysql/data/mysql-bin log-bin=mysql-bin #选择row模式 binlog-format=ROW #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行 binlog-do-db=test #不同步mysql系统数据库 binlog-ignore-db=mysql #配置主节点信息结束 #设置mysql数据库的日志及进程数据的存放目录 log-error =/usr/local/mysql/logs/mysql-error.log pid-file =/usr/local/mysql/mysql.pid # 服务端使用的字符集默认为8比特编码 character-set-server=utf8mb4 lower_case_table_names=1 autocommit =1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO ##################以上要修改的######################## skip-external-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 1024 sort_buffer_size = 4M net_buffer_length = 8K read_buffer_size = 4M read_rnd_buffer_size = 512K myisam_sort_buffer_size = 64M thread_cache_size = 128 #query_cache_size = 128M tmp_table_size = 128M explicit_defaults_for_timestamp = true max_connections = 500 max_connect_errors = 100 open_files_limit = 65535 binlog_format=mixed binlog_expire_logs_seconds =864000 # 创建新表时将使用的默认存储引擎 default_storage_engine = InnoDB innodb_data_file_path = ibdata1:10M:autoextend innodb_buffer_pool_size = 1024M innodb_log_file_size = 256M innodb_log_buffer_size = 8M innodb_flush_log_at_trx_commit = 1 innodb_lock_wait_timeout = 50 transaction-isolation=READ-COMMITTED [mysqldump] quick max_allowed_packet = 16M [myisamchk] key_buffer_size = 256M sort_buffer_size = 4M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid
7、初始化数据库
[root@bogon mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/
记住你的初始密码红框框冒号后面的 iBd5auJXgw+j
8、配置环境变量
[root@bogon mysql]# vim /etc/profile export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib export PATH #生效 [root@bogon mysql]# source /etc/profile
9、添加到服务设置开机启动
[root@bogon mysql]# cp support-files/mysql.server /etc/init.d/mysql #授权 [root@bogon mysql]# chmod +x /etc/init.d/mysql #添加到开机启动 [root@bogon mysql]# chkconfig --add mysql [root@bogon mysql]# chkconfig mysql on #检查是否成功 [root@bogon mysql]# chkconfig --list
10、启动
[root@bogon mysql]# service mysql start #相应的命令 {start|stop|restart|reload|force-reload|status}
11、登录数据库
1. #输入刚才的密码 2. [root@localhost mysql]# mysql -u root -p 3. Enter password:
12、修改密码设置远程连接
#修改密码 ALTER USER 'root'@'localhost' IDENTIFIED with mysql_native_password BY 'root'; flush privileges; #创建任意远程连接 CREATE USER 'root'@'%' IDENTIFIED BY 'root'; flush privileges; #为root账户授权所有数据库所有权限 grant all on *.* to root; flush privileges;
注意:log-error =/usr/local/mysql/logs/mysql-error.log
这个文件我是手动创建的,创建完成之后,重新授权给mysql用户,如果安装过程出错,请记得看错误日志!!!
navicat连接时请关闭防火墙或者开放3306端口。
#创建错误日志文件 [root@localhost mysql]# touch /usr/local/mysql/logs/mysql-error.log #重新授权 [root@bogon ~]# chown -R mysql.mysql /usr/local/mysql/
二、Docker方式
1、拉取镜像
[root@localhost mysql]# docker pull mysql
2、创建挂载文件
[root@localhost mysql]# mkdir -p /data/mysql/conf
3、配置文件my.cnf
[mysql] # 设置mysql客户端默认字符集 default-character-set=utf8mb4 [client] socket = /data/mysql/mysql.sock [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure-file-priv= NULL #主节点 #配置主节点信息 server_id=1001 log-bin=mysql-bin #选择row模式 binlog-format=ROW #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行 binlog-do-db=test #不同步mysql系统数据库 binlog-ignore-db=mysql #配置主节点信息结束 # 服务端使用的字符集默认为8比特编码 character-set-server=utf8mb4 lower_case_table_names=1 autocommit =1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO ##################以上要修改的######################## skip-external-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 1024 sort_buffer_size = 4M net_buffer_length = 8K read_buffer_size = 4M read_rnd_buffer_size = 512K myisam_sort_buffer_size = 64M thread_cache_size = 128 #query_cache_size = 128M tmp_table_size = 128M explicit_defaults_for_timestamp = true max_connections = 500 max_connect_errors = 100 open_files_limit = 65535 binlog_format=mixed binlog_expire_logs_seconds =864000 # 创建新表时将使用的默认存储引擎 default_storage_engine = InnoDB innodb_data_file_path = ibdata1:10M:autoextend innodb_buffer_pool_size = 1024M innodb_log_file_size = 256M innodb_log_buffer_size = 8M innodb_flush_log_at_trx_commit = 1 innodb_lock_wait_timeout = 50 transaction-isolation=READ-COMMITTED [mysqldump] quick max_allowed_packet = 16M [myisamchk] key_buffer_size = 256M sort_buffer_size = 4M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout
4、创建Docker容器
docker run \ --privileged=true \ --restart=always \ -p 3306:3306 --name mysql \ -v /data/mysql/data:/var/lib/mysql \ -v /data/mysql/conf/my.cnf:/etc/mysql/my.cnf \ -e MYSQL_ROOT_PASSWORD=root \ -d mysql:latest
此时已经可以navicat连接了,如果连不上请继续往下。
5、配置远程连接
#进入容器 [root@bogon log]# docker container exec -it mysql /bin/bash #配置远程访问 root@4d5306b674f7:/# mysql -uroot -p #然后输入密码root #创建任意远程连接 CREATE USER 'root'@'%' IDENTIFIED BY 'root'; flush privileges; #为root账户授权所有数据库所有权限 grant all on *.* to root; flush privileges;
三、主从复制
原理简单描述
master服务器将数据的改变记录二进制binlog日志,当master上的数据发生改变时,则将其改变写入二进制日志中。
slave服务器会定时对master二进制日志询问是否发生改变,如果发生改变,则开始一个I/O线程请求master二进制事件。
同时master节点为每个I/O线程启动一个dump线程,用于向其发送二进制事件,并保存至从节点本地的中继日志(relay-log)中,从节点将启动IO线程从中继日志中读取二进制日志,然后SQL线程执行解析后的sql语句。
以Docker安装方式为例
1、配置主服务器my.cnf的[mysqld]下添加
#主节点 server_id = 1001 log-bin = mysql-bin #选择row模式 binlog-format=ROW #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行 binlog-do-db=test #不同步mysql系统数据库 binlog-ignore-db=mysql
查看
show variables like '%server_id%';
2、 配置slave的配置文件
1. 2. #在/data/mysql下创建salve的配置文件文件 3. [root@bogon mysql]# mkdir -p /data/mysql/slave/conf
从节点my.cnf配置文件
[mysql] # 设置mysql客户端默认字符集 default-character-set=utf8mb4 [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure-file-priv= NULL #从节点 server_id = 1002 log-bin = mysql-bin #选择row模式 binlog-format=ROW #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行 replicate-do-db=test #不同步mysql系统数据库 replicate-ignore-db=mysql #只读 只对非root用户有效 read-only=1 # 服务端使用的字符集默认为8比特编码 character-set-server=utf8mb4 lower_case_table_names=1 autocommit =1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO ##################以上要修改的######################## skip-external-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 1024 sort_buffer_size = 4M net_buffer_length = 8K read_buffer_size = 4M read_rnd_buffer_size = 512K myisam_sort_buffer_size = 64M thread_cache_size = 128 #query_cache_size = 128M tmp_table_size = 128M explicit_defaults_for_timestamp = true max_connections = 500 max_connect_errors = 100 open_files_limit = 65535 binlog_format=mixed binlog_expire_logs_seconds =864000 # 创建新表时将使用的默认存储引擎 default_storage_engine = InnoDB innodb_data_file_path = ibdata1:10M:autoextend innodb_buffer_pool_size = 1024M innodb_log_file_size = 256M innodb_log_buffer_size = 8M innodb_flush_log_at_trx_commit = 1 innodb_lock_wait_timeout = 50 transaction-isolation=READ-COMMITTED [mysqldump] quick max_allowed_packet = 16M [myisamchk] key_buffer_size = 256M sort_buffer_size = 4M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout
3、启动从节点
docker run \ --privileged=true \ --restart=always \ -p 33060:3306 --name mysql-slave \ -v /data/mysql/slave/data:/var/lib/mysql \ -v /data/mysql/slave/conf/my.cnf:/etc/mysql/my.cnf \ -e MYSQL_ROOT_PASSWORD=root \ -d mysql:latest
4、开始同步数据
在主节点执行sql
show master status;
在从节点上执行如下sql
CHANGE MASTER TO master_host = '192.168.139.159', master_user = 'root', master_password = 'root', master_log_file = 'mysql-bin.000003', master_log_pos = 364;
然后从节点执行如下sql
#开始同步 start slave; #停止 STOP SLAVE; #查看同步状态 SHOW SLAVE STATUS;
到此结束
参考:https://blog.csdn.net/u010565545/article/details/104961184