2. Find all integers $n(n \geqslant 3)$, such that there exist real numbers $a_{1}, a_{2}, \cdots, a_{n+2}$, satisfying $a_{n+1}=a_{1}, a_{n+2}=a_{2}$, and for $i=1,2, \cdots, n$, we have $a_{i} a_{i+1}+1=a_{i+2}$.
2. The required $n$ is all multiples of 3.

On one hand, if $n$ is a multiple of 3, let $n=3k$, and take
$$
\begin{array}{l}
a_{3i-2}=a_{3i-1}=-1, a_{3i}=2(i=1,2, \cdots, k), \\
a_{n+1}=a_{n+2}=-1 .
\end{array}
$$

It is easy to verify that for $i=1,2, \cdots, n$, we have
$$
a_{i} a_{i+1}+1=a_{i+2}
$$

satisfying the requirement.
On the other hand, assume there exist $a_{1}, a_{2}, \cdots, a_{n+2}$ that meet the condition, extend them to a two-way infinite sequence with period $n$, then for any integer $i$, we have
$$
a_{i} a_{i+1}+1=a_{i+2} .
$$

We will prove the following conclusions sequentially.
(1) There does not exist $i$ such that $a_{i}>0, a_{i+1}>0$.

If there exists such $i$ that $a_{i}>0, a_{i+1}>0$, then
$$
a_{i+2}=a_{i} a_{i+1}+1>1 \text {. }
$$

By induction, it is easy to prove: for all $m \geqslant i+2$, we have
$$
a_{m}>1 \text {. }
$$

Thus, for $m \geqslant i+2$, we have
$$
a_{m+2}=a_{m} a_{m+1}+1>a_{m} a_{m+1}>a_{m+1} \text {. }
$$

Hence $a_{m+1}>0$, a contradiction.
(4) There does not exist $i$ such that $a_{i}>0, a_{i+1}<0$.
If there exists such $i$ that
$$
a_{i}>0, a_{i+1}<0,
$$

then by (1), we know $a_{i-1}<0$ and $a_{i+2}>1$,
thus $\left|a_{i+1}\right|>1 \Rightarrow a_{i+1}<0$.
By $a_{i+1}<0, a_{i+1}<0$, we deduce $a_{i-1}<0$, and $a_{i-1}<0, a_{i-2 k+1}<0, a_{i-1}>a_{i-3}>a_{i-5}>\cdots$.
In particular, $a_{i+1}>a_{i+1-2 n}$, which contradicts the sequence $\left\{a_{m}\right\}(m \in \mathbf{Z})$ defined with period $n$.

From (1) to (4), we know that in any three consecutive terms $a_{i}, a_{i+1}, a_{i+2}$, exactly one term is positive and two are negative. Thus, the sign of this sequence has a period of 3. Since there is at least one positive term, assume $a_{i}>0$, then $a_{i+n}>0$. Therefore, $3 \mid n$.