IP | hostname | cpu | Mem | disk | nic | vm version |
172.16.186.141 | test2 | 2 | 8G | 150G | 1 | vmware15 pro |
mysql數據庫部署在虛擬機上(離線安裝方式),cacti則使用docker部署
本文最終是將鏡像放到無網環境中,故mysql在采用離線安裝(無網環境安裝),docker免安裝方式自行查詢,導出鏡像文章后面會說到
MySQL離線安裝
https://dev.mysql.com/downloads/mysql/5.7.html#downloads
上圖點擊左下角 No thanks, just start my download. 即可下載
首先卸載系統中自帶的mysql並刪除配置文件
# 查看系統自帶的Mariadb和msql
rpm -qa|grep mariadb
rpm -qa | grep mysql
# 卸載系統自帶的Mariadb
rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
# 刪除etc目錄下的my.cnf
rm /etc/my.cnf
# 創建mysql用戶組
groupadd mysql
# 創建一個用戶名為mysql的用戶,並加入mysql用戶組
useradd -g mysql mysql
安裝
tar -zxvf mysql-5.7.31-el7-x86_64.tar.gz -C /usr/local
mv /usr/local/mysql-5.7.31-el7-x86_64 /usr/local/mysql
mkdir /usr/local/mysql/data
chown -R mysql.mysql /usr/local/mysql
cd /usr/local/mysql
vim my.cnf
[mysqld]
socket=/var/lib/mysql/mysql.sock
# set mysql server port
port = 3306
# set mysql install base dir
basedir=/usr/local/mysql
# set the data store dir
datadir=/usr/local/mysql/data
# set the number of allow max connnection
max_connections=300
# set server charactre default encoding
character-set-server=utf8
# the storage engine
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=30M
explicit_defaults_for_timestamp=true
# 取消密碼驗證
#skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
# skip-grant-tables
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysql.server]
user=mysql
basedir=/usr/local/mysql
初始化MySQL(不要使用mysql_install_db方法初始化)
yum -y install numactl libaio libaio-devel git #執行該命令需要掛載光盤
/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
# 將mysql加入服務
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
mkdir /var/lib/mysql && chown -R mysql.mysql /var/lib/mysql/
# 將MySQL加入開機自啟並啟動
chkconfig mysql on && service mysql start
做軟連接
ln -s /usr/local/mysql/bin/mysql /usr/bin/
ln -s /var/lib/mysql/mysql.sock /tmp/
登錄數據庫修改密碼
mysql -uroot -p
alter user 'root'@'localhost' identified by 'aa7788**';
grant all privileges on *.* to 'root'@'%' identified by 'aa7788**' with grant option;
#創建cacti庫
create database cacti;
grant all on cacti.* to cacti@'%' identified by '123456';
grant select on mysql.time_zone_name to cacti@'%' identified by '123456';
grant select on mysql.time_zone_name to cacti@localhost identified by '123456';
alter database cacti character set utf8mb4 collate utf8mb4_unicode_ci;
flush privileges;
exit;
從github上克隆cacti項目
git clone --depth=1 git@github.com:scline/docker-cacti.git
cd docker-cacti #修改下面的信息,下面除IP按需換之外,其他原樣寫
vim Dockerfile
....
....
ENV \
DB_NAME=cacti \
DB_USER=cacti \
DB_PASS=123456 \
DB_HOST=172.16.186.141 \ #虛擬機的IP(也就是宿主機),這里要寫docker0的IP有時會報錯
DB_PORT=3306 \
RDB_NAME=cacti \
RDB_USER=cacti \
RDB_PASS=123456 \
RDB_HOST=172.16.186.141 \
RDB_PORT=3306 \
CACTI_URL_PATH=cacti \
....
....
vim start.sh (注釋點下面的一段配置)
....
....
# remove php-snmp if asked, required for snmpv3 to function correctly. Disabled by default
#if [ ${PHP_SNMP} = 0 ]; then
# echo "$(date +%F_%R) [PHP-SNMP] Removing php-snmp since ENV variable 'PHP_SNMP' is set to 0"
# yum remove -y --noautoremove php-snmp
# yum clean all
# else
# echo "$(date +%F_%R) [PHP-SNMP] Insalling php-snmp since ENV variable 'PHP_SNMP' is set to 1"
# yum install -y php-snmp
# yum clean all
#fi
....
....
構建鏡像
docker build -t cacti:v1 .
構建容器(找個大點的磁盤分區進行專存儲日志,可自定義)
mkdir -p /var/cacti/log
構建容器
docker run -itd --name cacti -p 80:80 -p 443:443 -v /var/cacti/log:/cacti/log/ cacti:v1
查看容器啟動過程
docker logs -f cacti
進入cacti容器進行修改
docker exec -it cacti bash
sed -i "s/;date.timezone =/date.timezone = Asia\/Shanghai/" /etc/php.ini
退出容器
在宿主機上打開mysql的配置文件(下面配置文件中的值需要根據頁面提示的數值來定義)
vim /usr/local/mysql/my.cnf #在mysqld段中添加以下內容
[mysqld]
....
....
explicit_defaults_for_timestamp=true
innodb_flush_log_at_timeout = 3
innodb_read_io_threads = 32
innodb_write_io_threads = 16
tmp_table_size = 125M
innodb_buffer_pool_size = 2048M
max_heap_table_size = 125M
join_buffer_size = 300M
innodb_buffer_pool_instances = 22
innodb_io_capacity = 5000
innodb_io_capacity_max = 10000
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
max_allowed_packet = 16777216
....
....
保存退出並重啟容器
docker restart cacti
重啟數據庫
service mysql restart
填充time_zone數據庫
/usr/local/mysql/bin/mysql_tzinfo_to_sql /usr/share/zoneinfo/ | mysql -uroot -p mysql
瀏覽器訪問
https://172.16.186.141/cacti (默認帳密為admin/admin)
會顯示出來“您與該網站的連接不是私密連接,存在安全隱患”的提示,點擊“忽略警告,繼續訪問”
注:上圖中寫虛擬機的網段和掩碼
導出鏡像
格式:docker save images_name:tag >new_name.tar
docker save cacti:v3>docker_cactiv.tar
導入鏡像
格式:docker load <new_name.tar
docker load<docker_cactiv.tar
添加設備自行查閱其他文檔
歡迎加入QQ群一起學習Linux、開源等技術