环境:
Ubuntu 22.04.1 LTS
Sonic版本:2.4.2
jdk:17+
SDK环境
1.安装mysql
更新列表
sudo apt-get update
安装MySQL服务
sudo apt-get install mysql-server
初始化MySQL配置
root@iZbp14v3igbgzqxc9il4vsZ:~# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
#是否使用验证密码组件
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: N
#是否修改root密码
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : N
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
#是否移除匿名用户
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
#是否禁止root远程登录
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
#是否删除test数据库
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
#是否重新加载权限
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
设置root用户远程访问权限
root@iZbp14v3igbgzqxc9il4vsZ:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.32-0ubuntu0.22.04.2 (Ubuntu)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select host,user from user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| localhost | debian-sys-maint |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
5 rows in set (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
创建sonic数据库
mysql> CREATE DATABASE sonic CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected, 2 warnings (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sonic |
| sys |
+--------------------+
5 rows in set (0.01 sec)
2.安装docker
安装必要的证书并允许 apt 包管理器使用以下命令通过 HTTPS 使用存储库
sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release
添加 Docker 的官方 GPG 密钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
添加 Docker 官方库
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
新 Ubuntu 源列表
sudo apt update
安装最新Docker CE
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
查看docker是否安装成功
root@iZbp14v3igbgzqxc9il4vsZ:~# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-05-05 17:24:48 CST; 26s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 29393 (dockerd)
Tasks: 10
Memory: 25.3M
CPU: 330ms
CGroup: /system.slice/docker.service
└─29393 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
May 05 17:24:47 iZbp14v3igbgzqxc9il4vsZ systemd[1]: Starting Docker Application Container Engine...
May 05 17:24:47 iZbp14v3igbgzqxc9il4vsZ dockerd[29393]: time="2023-05-05T17:24:47.626569902+08:00" level=info msg="Starting up"
May 05 17:24:47 iZbp14v3igbgzqxc9il4vsZ dockerd[29393]: time="2023-05-05T17:24:47.627571744+08:00" level=info msg="detected 127.0.>
May 05 17:24:47 iZbp14v3igbgzqxc9il4vsZ dockerd[29393]: time="2023-05-05T17:24:47.699261043+08:00" level=info msg="Loading contain>
May 05 17:24:47 iZbp14v3igbgzqxc9il4vsZ dockerd[29393]: time="2023-05-05T17:24:47.990848529+08:00" level=info msg="Loading contain>
May 05 17:24:48 iZbp14v3igbgzqxc9il4vsZ dockerd[29393]: time="2023-05-05T17:24:48.005182732+08:00" level=info msg="Docker daemon" >
May 05 17:24:48 iZbp14v3igbgzqxc9il4vsZ dockerd[29393]: time="2023-05-05T17:24:48.005332523+08:00" level=info msg="Daemon has comp>
May 05 17:24:48 iZbp14v3igbgzqxc9il4vsZ systemd[1]: Started Docker Application Container Engine.
May 05 17:24:48 iZbp14v3igbgzqxc9il4vsZ dockerd[29393]: time="2023-05-05T17:24:48.043637879+08:00" level=info msg="API listen on />
设置开机启动
sudo systemctl enable docker
3.安装docker compose
下载对应的二进制文件,使用xftp放到/usr/local/bin/目录下,重命名为docker-compose
Releases · docker/compose · GitHub
赋予二进制文件可执行权限
sudo chmod +x /usr/local/bin/docker-compose
检查安装的 Docker Compose 版本
root@iZbp14v3igbgzqxc9il4vsZ:~# docker-compose version
Docker Compose version v2.17.2
4.部署sonic server
下载sonic server
Releases · SonicCloudOrg/sonic-server · GitHub
修改.env文件
SONIC_SERVER_HOST修改为本地的ipv4地址,由于使用docker部署,因此不可使用localhost和127.0.0.1
MySQL Config修改为本地MySQL配置
具体参数可参考官方文档:
将修改好的文件使用xftp上传到服务器
进入sonic根目录下执行命令,首次会拉取镜像并启动
docker-compose up -d
启动成功
5.部署sonic agent
在平台Agent中心新增1个Agent,复制Agent Key
下载sonic agent:Releases · SonicCloudOrg/sonic-agent · GitHub
修改/config/application-sonic-agent.yml
在根目录下执行修改为utf-8编码
chcp 65001
再执行
java -Dfile.encoding=utf-8 -jar sonic-agent-windows-x86_64.jar
启动成功后刷新平台设备中心,可以看到设备在线,并可以正常使用
常见问题
1.初始化MySQL设置密码时报错
手动配置密码:
root@iZbp14v3igbgzqxc9il4vsZ:~# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.32-0ubuntu0.22.04.2 (Ubuntu)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by '123456';
Query OK, 0 rows affected (0.00 sec)
重新执行初始化命令
2.连接数据库失败
2023-05-05 17:55:25.963 -ERROR 1 --- [main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:894) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:819) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:237) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-5.0.1.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:359) ~[HikariCP-5.0.1.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201) ~[HikariCP-5.0.1.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:470) ~[HikariCP-5.0.1.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-5.0.1.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:100) ~[HikariCP-5.0.1.jar!/:na]
首先检查一下root用户的host是否已经修改为"%"
使用下面的命令看是否可以连接
mysql -h 你的ipv4地址 -u root -p
如果报错
ERROR 2003 (HY000): Can't connect to MySQL server on 'ip地址:3306' (111)
查看3306端口是否开放,如下图所示是没有开放
root@iZbp14v3igbgzqxc9il4vsZ:/home/sonic-server-v2.4.2# netstat -an| grep 3306
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:33060 0.0.0.0:* LISTEN
修改MySQL的配置文件
vim /etc/mysql/mysql.conf.d/mysqld.cnf
重启mysql服务
service mysql restart
再次查看3306端口,发现已经开放
root@iZbp14v3igbgzqxc9il4vsZ:/home/sonic-server-v2.4.2# netstat -an| grep 3306
tcp 0 0 127.0.0.1:33060 0.0.0.0:* LISTEN
tcp6 0 0 :::3306 :::* LISTEN
使用ipv4地址连接,已经可以连接
root@iZbp14v3igbgzqxc9il4vsZ:/home/sonic-server-v2.4.2# mysql -h ip地址 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.32-0ubuntu0.22.04.2 (Ubuntu)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
重新启动docker,启动正常