yum install mariadb-server mariadb -y
1)安装MariaDB
yum install libaio # 安装支持异步IO的包 yum -y install numactl groupadd mysql useradd -r -g mysql mysql mkdir -pv /mydata/data # 创建数据目录 cd /usr/local tar zxvf /path/to/mysql-VERSION-OS.tar.gz ln -s full-path-to-mysql-VERSION-OS mysql cd mysql mkdir mysql-files # 注意这里要创建mysql-files文件夹 chmod 770 mysql-files chown -R mysql.mysql . # 这一步会产生临时的root@localhost密码,需要自己记下来 bin/mysqld --initialize --user=mysql --datadir=/mydata/data # 这一步就相当于数据库的初始化了,原本的install_db已经不存在了 bin/mysql_ssl_rsa_setup chown -R root . chown -R mysql /mydata/data mysql-files bin/mysqld_safe --user=mysql --datadir=/mydata/data & cp support-files/mysql.server /etc/init.d/mysqld # 然后要将/usr/local/mysql/bin加入到环境变量中 # 登录完之后要修改密码 set password = ‘123‘; # 最后用chkconfig配置开机自启动 chkconfig mysqld on
说明:
2)提供配置文件
cp support-files/my-large.cnf /etc/my.cnf # 并在这个配置文件中加三个选项: datadir = /mydata/data # 提供数据目录 innodb_file_per_table = ON # 每个表使用自己专用的表空间存储表的数据和索引 skip_name_resolve = ON # 禁止解析主机名
3)启动服务并加固
service mysqld start # 启动MySQL mysql_secure_installation # 对MySQL的一系列加固措施
4)安装后设定
# 为root用户设定密码 set password update mysql.user set password=PASSWORD(‘your_pass‘) where cluase # 删除所有匿名用户 drop user ‘‘@‘localhost‘; # 上述两步可以用mysql_secure_installation来替代 skip_name_resolve=1 # 关闭主机名反解
1)配置示例
# 配置文件my.cnf [client] user=david password=88888888 [mysqld] # 这个标签表示是整个MySQL的配置 ########basic settings######## server-id = 11 port = 3306 user = mysql bind_address = 10.166.224.32 # 根据实际情况修改,如果修改成0.0.0.0,即表示允许任何主机登陆 autocommit = 0 # 5.6.X安装时,需要注释掉,安装完成后再打开 character_set_server=utf8mb4 ### 设置好了字符集 skip_name_resolve = 1 max_connections = 800 max_connect_errors = 1000 datadir = /data/mysql_data # 数据库的数据存放目录,根据实际情况修改,建议和程序分离存放(这个文件夹要设置成mysql.mysql的权限) transaction_isolation = READ-COMMITTED explicit_defaults_for_timestamp = 1 join_buffer_size = 134217728 tmp_table_size = 67108864 tmpdir = /tmp max_allowed_packet = 16777216 sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER" interactive_timeout = 1800 wait_timeout = 1800 read_buffer_size = 16777216 read_rnd_buffer_size = 33554432 sort_buffer_size = 33554432 ########log settings######## log_error = error.log # 如果没有配置错误文件的位置和文件名,那么默认的是 机器名.error,配置了这个位置之后,一些输出信息都会被保存到这里 slow_query_log = 1 ### 开启了慢查询日志 slow_query_log_file = slow.log log_queries_not_using_indexes = 1 log_slow_admin_statements = 1 log_slow_slave_statements = 1 log_throttle_queries_not_using_indexes = 10 expire_logs_days = 90 long_query_time = 2 min_examined_row_limit = 100 # 不走索引,直接扫描而不计入慢查询日志的limit ########replication settings######## master_info_repository = TABLE relay_log_info_repository = TABLE log_bin = bin.log sync_binlog = 1 gtid_mode = on enforce_gtid_consistency = 1 log_slave_updates binlog_format = row relay_log = relay.log relay_log_recovery = 1 binlog_gtid_simple_recovery = 1 slave_skip_errors = ddl_exist_errors ########innodb settings######## innodb_page_size = 8192 innodb_buffer_pool_size = 6G #根据实际情况修改 innodb_buffer_pool_instances = 8 innodb_buffer_pool_load_at_startup = 1 innodb_buffer_pool_dump_at_shutdown = 1 innodb_lru_scan_depth = 2000 innodb_lock_wait_timeout = 5 innodb_io_capacity = 4000 innodb_io_capacity_max = 8000 innodb_flush_method = O_DIRECT innodb_file_format = Barracuda innodb_file_format_max = Barracuda innodb_log_group_home_dir = /redolog/ # 根据实际情况修改(可以后面改),这个文件夹要设置为mysql.mysql的权限 innodb_undo_directory = /undolog/ # 根据实际情况修改(设置之后就不能改),这个文件夹要设置为mysql.mysql的权限 innodb_undo_logs = 128 # 这个值建议一开始就确定,否则后面设置比较麻烦 innodb_undo_tablespaces = 3 # 这个值建议一开始就确定,否则后面设置比较麻烦 innodb_flush_neighbors = 1 innodb_log_file_size = 4G # 根据实际情况修改(这个参数非常重要,不能设置太小,初始化的安装是48M,非常不合适) innodb_log_buffer_size = 16777216 innodb_purge_threads = 4 innodb_large_prefix = 1 innodb_thread_concurrency = 64 innodb_print_all_deadlocks = 1 innodb_strict_mode = 1 innodb_sort_buffer_size = 67108864 ########semi sync replication settings######## plugin_dir=/usr/local/mysql/lib/plugin #根据实际情况修改 plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" loose_rpl_semi_sync_master_enabled = 1 loose_rpl_semi_sync_slave_enabled = 1 loose_rpl_semi_sync_master_timeout = 5000 [mysqld-5.7] # 这个标签是代表这是5.7的单独配置 innodb_buffer_pool_dump_pct = 40 innodb_page_cleaners = 4 innodb_undo_log_truncate = 1 innodb_max_undo_log_size = 2G innodb_purge_rseg_truncate_frequency = 128 binlog_gtid_simple_recovery=1 log_timestamps=system transaction_write_set_extraction=MURMUR32 show_compatibility_56=on
2)对几个重要参数的配置说明
3)my.cnf 的说明
1)Client端
2)Server端
在一台机器上开启多个不同的服务端口(如3306、3307),运行多个MySQL服务进程,这些服务进程通过不同的socket监听不同的服务端口来提供各自的服务。
这些MySQL多实例公用一套MySQL安装程序,使用不同的(也可以相同)的my.cnf配置文件、启动程序、数据文件。
在提供服务时,多实例MySQL在逻辑上看来是各自独立的。
1)my.cnf 配置示例
[mysqld_multi] mysqld = /usr/local/mysql/bin/mysqld_safe mysqladmin = /usr/local/mysql/bin/mysqladmin user = multi_admin # 这里需要创建一个关闭MySQL多实例的用户账号 [mysqld1] port = 3306 user = mysql socket = /var/lib/mysql/mysql.sock pid-file = /var/lib/mysql/mysql.pid datadir = /var/lib/mysql [mysqld2] port = 3307 user = mysql socket = /mnt/data/db1/mysql.sock pid-file = /mnt/data/db1/mysql.pid datadir = /mnt/data/db1 skip-name-resolve server-id = 2 default-storage-engine = innodb innodb_buffer_pool_size = 512M innodb_additional_mem_pool = 10M default_character_set = utf8 character_set_server = utf8 #read-only relay-log-space-limit = 3G expire_logs_day = 20 [mysqld3] port = 3308 user = mysql socket = /mnt/data/db2/mysql.sock pid-file = /mnt/data/db2/mysql.pid datadir = /mnt/data/db2 skip-name-resolve server-id = 3 master-connect-retry = 60 default-storage-engine = innodb innodb_buffer_pool_size = 1G innodb_additional_mem_pool = 10M default_character_set = utf8 character_set_server = utf8 read-only relay-log-space-limit = 3G expire_logs_day = 20
2)初始化数据库
/usr/local/mysql/scripts/mysql_install_db --datadir=/var/lib/mysql --basedir=/usr/local/mysql --user=mysql /usr/local/mysql/scripts/mysql_install_db --datadir=/mnt/data/db1 --basedir=/usr/local/mysql --user=mysql /usr/local/mysql/scripts/mysql_install_db --datadir=/mnt/data/db2 --basedir=/usr/local/mysql --user=mysql
3)启动、查看和关闭多实例
# 启动MySQL多实例 mysql_multi --defaults-extra-file=/etc/my_multi.cnf start 1,2,3 # 查看MySQL多实例启动状况 mysql_multi --defaults-extra-file=/etc/my_multi.cnf report 1,2,3 # 关闭MySQL多实例 # 一次关闭多个实例 mysql_multi --defaults-extra-file=/etc/my_multi.cnf stop 1,2,3 # 一次关闭指定的一个实例 mysqladmin -u root -S /mnt/mydata/3307/mysql.sock shutdown
4)登录多实例
# 本机登录多实例 mysql -u root -p -S /data/3308/mysql.sock --prot=3308 # 远程登录多实例 mysql -u root -p --port=3308 -h 10.0.0.202
1)创建目录结构
2)修改各个实例的 my.cnf 配置文件
# 只写出了每个实例之间有差异的地方 [client] port = 3307 socket = /multi_mysql/3307/mysql.sock [mysqld] port = 3307 socket = /multi_mysql/3307/mysql.sock datadir = /multi_mysql/3307/data basedir = /usr/local/mysql log-error = /multi_mysql/3307/mysql_3307.err pid-file = /multi_mysql/3307/mysqld.pid server-id = 2 # 这里id号每个实例都要不同
3)初始化多实例的数据库文件
./mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/3306/data --user=mysql
./mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/3307/data --user=mysql
4)启动、停止多实例
# 启动 mysql_safe --defaults-file=/mydata/3306/my.cnf 2>&1 >/dev/null & mysql_safe --defaults-file=/mydata/3307/my.cnf 2>&1 >/dev/null & # 停止 mysqladmin -u root -phgzero -S /mydata/3306/mysql.sock shutdown mysqladmin -u root -phgzero -S /mydata/3307/mysql.sock shutdown
5)登录多实例
# 为root增加密码 mysqladmin -u root -S /data/3306/mysql.sock password ‘woshiniba‘ # 本机登录 mysql -u root -pwoshiniba -S /data/3306/mysql.sock # 远程登录 mysql -u remote -p ‘woshiniba‘ -h 10.0.0.202 -P 3308
原文:https://www.cnblogs.com/hgzero/p/14054896.html